1 Replies - 72 Views - Last Post: 08 February 2012 - 08:35 AM Rate Topic: -----

Topic Sponsor:

#1 javaHeyy  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 12
  • Joined: 08-February 12

Removing ArrayList objects in GUI.

Posted 08 February 2012 - 05:26 AM

Hey guys, I'm are currently studying my programming diploma, our java lecturer has set us a task to complete by monday, but I'm just not sure on how I can do it. The program is a Calculator divided up into several different sources, the Main file, the Calculator Frame, the Data Panel and the Button Panel. My task is to "link" the Data Panel and Button Panel without extending them together, so that I can add two buttons, "Undo" and "Clear" into the Button Panel with their Action Listeners, but for the listeners to be grabbing the methods from the Data Panel source, so far I'm unsure on how to link them, so I'm unsure on how to call the methods for the action listeners. If anyone is able to help me, all files are located below.
//Main.java
public class Main {
	public static void main(String[] args) {
		CalculatorFrame f = new CalculatorFrame();
			f.setTitle("Calculator");
			f.setLocation(100, 100);
			f.setSize(225, 200);
			f.setVisible(true);
	}
}

//CalculatorFrame.java
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CalculatorFrame extends JFrame {
	public CalculatorFrame() {
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowCloser());

		DataPanel dp = new DataPanel();
		add(dp, BorderLayout.CENTER);

		ButtonPanel bf = new ButtonPanel();
			bf.setLocation(100, 100);
			bf.setSize(50, 50);
			bf.setVisible(true);
		add(bf, BorderLayout.PAGE_END);
	}

	private class WindowCloser extends WindowAdapter {
		public void windowClosing(WindowEvent e) {
			setVisible(false);
			dispose();
			System.exit(0);
		}
	}	
}

//DataPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;

public class DataPanel extends JPanel {
	ArrayList<Double> totalAL = new ArrayList<Double>();
	ArrayList<Double> averageAL = new ArrayList<Double>();
	ArrayList<Double> minAL = new ArrayList<Double>();
	ArrayList<Double> maxAL = new ArrayList<Double>();

    private double _number;
    private double _total;
    private double _count;
    private double _average;
    private double _min;
    private double _max = 0;

    private JTextField _inField = new JTextField();
    private JTextField _totalField = new JTextField(); 
    private JTextField _averageField = new JTextField(); 
    private JTextField _minField = new JTextField(); 
    private JTextField _maxField = new JTextField(); 
    private JTextField _countField = new JTextField(); 

	public DataPanel() {
		setLayout(new GridLayout(6, 2, 1, 1));
		add(new JLabel("Number"));
		add(_inField);
		_inField.addActionListener(new InFieldListener());

		add(new JLabel("Total"));
		add(_totalField);	
		_totalField.setEditable(false);

		add(new JLabel("Count"));
		add(_countField);
		_countField.setEditable(false);

		add(new JLabel("Average"));
		add(_averageField);
		_averageField.setEditable(false);

		add(new JLabel("Maximum"));
		add(_maxField);
		_maxField.setEditable(false);

		add(new JLabel("Minimum"));
		add(_minField);
		_minField.setEditable(false);
	}

	public void clearFunction() {
		_inField.setText("");
		_totalField.setText("");
		_countField.setText("");
		_averageField.setText("");
		_maxField.setText("");
		_minField.setText("");
	}

	public void undoFunction() {
		// Implement file scanner, deleting last entry line.
	   double sizeTotal = totalAL.get(totalAL.size() - 1);

		_count--;
		totalAL.remove(sizeTotal);
		
	}
	
	private class InFieldListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
				// Implement file-writer for all JTextFields, not including _inField. Also create non-visible "id" to find line for deletion.
			if(_inField.getText().equals("exit")) {
				System.exit(0);
			}
			try {
			  _count++;
		     	  _number = Double.parseDouble(_inField.getText());
		     	  _inField.setText("");

			  if(_count == 1) {
			    _min = _number;
			    _max = _number;
			  } else if(_number >= _max) {
			    _max = _number;
			  } else if(_number <= _min) {
			    _min = _number;
			  }

		          _total += _number;
		          _average = _total/_count;
		        
			  _totalField.setText(String.valueOf(_total));
				totalAL.add(_total);
		          _countField.setText(String.valueOf(_count));
		          _averageField.setText(String.valueOf(_average));
				averageAL.add(_average);
		          _maxField.setText(String.valueOf(_max));
				maxAL.add(_max);
		          _minField.setText(String.valueOf(_min));
				minAL.add(_min);

			} catch(NumberFormatException exc) {
			  _inField.setText("");
			  System.out.println("There was an error: " + exc);
			}
		}
	}
}

//ButtonPanel.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ButtonPanel extends JPanel {

	public interface Listener {
		public void clear();
		public void undo();
	}

	private Listener _listener;
	
	public void setListener(Listener lis) {
		_listener = lis;
	}

	private JButton _undoButton;
	private JButton _clearButton;
	
	public ButtonPanel() {
		setLayout(new GridLayout(1, 2));

		_undoButton = new JButton("Undo");
		add(_undoButton);

		_clearButton = new JButton("Clear");
		add(_clearButton);
		_clearButton.addActionListener(new ClearButtonListener());
	}

	private class ClearButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//setInField("");
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Removing ArrayList objects in GUI.

#2 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,337
  • Joined: 25-December 08

Re: Removing ArrayList objects in GUI.

Posted 08 February 2012 - 08:35 AM

You will need to pass a reference of your array list to the button panel class, so when you call your action-performed, it can make the necessary modifications to the data. Or a reference to the GUI object, and get the data through that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1