newbie GUI/ event handling problem
I'm new to java programming and self learning this programming language. I venture into the field of java GUI and event handling and i met some problem which i don't get it. The exit handling event went well for me, but the click to count is not working as expected. And finally my combobox problem, i suppose to choose 2 values from the comboboxes and the value suppose to appear in the label and additionally it will do a sum up and show a result. However, by trying on the "1" value, i don't even see the value appearing in the label, why is this so??
and heres my coding
Quote:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class javaguitest extends JPanel
{
private static int numClick = 0, num1 = 0, num2= 0, numadd = 0;
private static JLabel label1;
private static JComboBox bbox1;
public static void main(String[] args)
{
JFrame frame = new JFrame("Testing GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
frame.setResizable(false);
JPanel main = new JPanel();
main.setLayout(new GridLayout(4, 1, 8, 8));
main.setBorder(BorderFactory.createLineBorder(Colo r.black));
JPanel p1 = new JPanel();
JButton Exit = new JButton("Exit");
Exit.setPreferredSize(new Dimension(150, 30));
Exit.setBorder(BorderFactory.createLineBorder(Colo r.black));
p1.setBorder(BorderFactory.createLineBorder(Color. black));
Exit.addActionListener(new exitButtonListener());
p1.add(Exit);
JPanel p2 = new JPanel();
p2.setBorder(BorderFactory.createLineBorder(Color. black));
JButton Count = new JButton("Click to count");
JLabel label1 = new JLabel("Number of clicks: " + numClick);
label1.setBorder(BorderFactory.createLineBorder(Co lor.black));
Count.addActionListener(new countButtonListener());
p2.add(Count);
p2.add(label1);
p2.setBorder(BorderFactory.createLineBorder(Color. black));
JPanel p3 = new JPanel();
String[] box1 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
JComboBox bbox1 = new JComboBox(box1);
String[] box2 = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
JComboBox bbox2 = new JComboBox(box2);
JLabel label2 = new JLabel("Box 1");
JLabel label3 = new JLabel("Box 2");
JLabel label4 = new JLabel("You have chosen " + num1 + " from Box 1 and chosen " + num2 + " from box 2");
JLabel label5 = new JLabel("The total of box 1 + box 2 is: " + numadd);
p3.add(label2);
p3.add(bbox1);
p3.add(label3);
p3.add(bbox2);
p3.add(label4);
p3.add(label5);
p3.setBorder(BorderFactory.createLineBorder(Color. black));
main.add(p1);
main.add(p2);
main.add(p3);
frame.add(main);
frame.pack();
frame.setSize(600, 530);
frame.setVisible(true);
}
public static class exitButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
static class countButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
numClick++;
label1.setText("Number of Clicks: " + numClick);
}
}
static class dish2r implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int Selection;
Selection = bbox1.getSelectedIndex();
if (Selection == 0)
{
num1 = 0;
}
else if (Selection == 1)
{
num1 = 1;
numadd = numadd + num1;
}
else if (Selection == 2)
{
num1 = 2;
numadd = numadd + num1;
}
}
}
}
Re: newbie GUI/ event handling problem
Quote:
Originally Posted by
zyspt
but the click to count is not working as expected.
make this small modification to your code:
1. Count.addActionListener(new countButtonListener(label1)); // this will pass the label1 to the your listener
2. Define a constructor for your countButtonListener class as follows and then inside the actionPerformed method you can modify the jlabel.
Code :
static class countButtonListener implements ActionListener {
JLabel label;
public countButtonListener(JLabel l) {
label = l;
}
public void actionPerformed(ActionEvent event) {
numClick++;
// the following line will change the jlabel in the jpanel
label.setText("Number of clicks: " + numClick);
}
}
for the others listener you can do the same. have fun.