Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: newbie GUI/ event handling problem

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
    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;
    }
    }
    }
    }


  2. #2
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: newbie GUI/ event handling problem

    Quote Originally Posted by zyspt View Post
    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.

    	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.

  3. The Following User Says Thank You to j2me64 For This Useful Post:

    zyspt (May 13th, 2010)

Similar Threads

  1. [SOLVED] JRadioButton Event Handling Help?
    By CS313e in forum AWT / Java Swing
    Replies: 2
    Last Post: March 27th, 2010, 11:56 AM
  2. JButton event handling.
    By Prof in forum AWT / Java Swing
    Replies: 6
    Last Post: February 3rd, 2010, 10:29 AM
  3. Newbie Programming problem
    By Pingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:10 AM
  4. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM