# Java GUI building help

• Java
Build a GUI to accept integers (when the ENTER key is depressed) into an arr
ay of 15 int; have a button called AVERAGE; when AVERAGE is selected, there
is a display of the count and the current average; have a button called CLEA
R; when CLEAR is selected, count is set to 0 and so are all the array elemen
ts and then there is another display: count is 0 and average is 0.0.

package argui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
* <p>Title: </p>
*
* <p>Description: </p>
*
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class ArrayGUIPanel extends JPanel
{
private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageL
abel;

private JTextField nums;
private JButton average, clear;
private int count;
private double avg;

//-----------------------------------------------------------------
// Constructor: Sets up the main GUI components.
//-----------------------------------------------------------------
public ArrayGUIPanel()
{

count = 0;
avg=0.0;

inputLabel = new JLabel ("Enter integers: " );
resultLabel = new JLabel ("---");
countLabel = new JLabel ("Count: " + count);
averageLabel = new JLabel ("Average: " + avg);

average = new JButton ("AVERAGE");
clear = new JButton ("CLEAR");

AvgListener listener = new AvgListener();

setPreferredSize (new Dimension(300, 75));
setBackground (Color.white);

}

am i going right direction here?

I would say yes as a general consensus, but tis hard for me to understand what you plan on adding. I would ask, Shouldn't this class contain an instance variable that is a size 15 Array. Then each instance of your Gui has its own array to store its values into. Also I assume you are planning on defining your own class AverageListener that will extend ActionListener, Ususlly when I have writen simle Gui's I tended to just have my Gui class extend Jpanel as well as Actionlistener, pass "this" as the listener for the buttons (that is to say the gui acts as its own listener) and override the public void actionperformed within the code of your adding the listener. Other than that It looks like your planning is going fine, I'd be interested to see how this turns out

You need not the count variable. You can get the length of an Array from the Array itself, note an array is an object in Java.

You can just make one JTextField and parse the results. e.g.

"1,2,3,4,5" -> written in the JTextField

then
Code:
String input = jTextField.getText();

String[] valuesAsStrings = input.split(","); //will return {"1","2","3","4","5"}
You can get the number of values as valuesAsStrings.length // returns 5 in the above example. All what is left is to convert the Strings to ints and you are pretty much done.

You need not to define your own class for action listening but you can use an anonymous inner class, this might sound confusing but
Code:
JButton button = new JButton("Click me!");
public void actionPerformed(ActionEvent e) {
System.out.println("You clicked me:)");

}

});

panel.add(button);
this way might be a bit confusing at fist but after a few months it will be clear to you what you have done, since if you can create a new Class anonymously - that is - it is not assigned to a variable and put it as a parameter - then you can also override some methods.

Last edited:
P.S.: In Java it is not customary to do

private JLabel inputLabel, outputLabel, resultLabel, countLabel, averageL
abel;

but
private JLable inputLabel;
private JLabel outputLabel;
....

you need not to splecify this as field of the class, you can define this inside your constructor. There is a difference between defining a JLabel outside any methods - it is a field of a Class, or inside a method - it used just locally inside the metod.