Results 1 to 9 of 9
  1. #1
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default Listener question

    Does each style of button require its own listener? i.e. if I have a multi-panel JPane with radio buttons in one panel, check boxes in another etc do I have to make a listener for each type be it radio, checkboxes or whatever?

    I have a listener set for both JRadioButtons, JCheckBoxes and a JButton and they are not working properly. Here is an example (only partially done) of my code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    import java.text.NumberFormat;
    
    public class ExtrasTab extends JPanel
    {
    	public JRadioButton salad1, salad2, salad3;
    	public JCheckBox gb, cs, cw8, cw16, sb;
    	public JButton order;
    
    
    
        public ExtrasTab()
        {
    		setLayout (new BorderLayout());
    		ImageIcon icon5 = new ImageIcon ("salad.jpg");
    		ImageIcon icon6 = new ImageIcon ("sbuns.jpg");
    
    		MyButtonListener listener = new MyButtonListener();
    		JPanel logo2 = new JPanel();
    		logo2.setBorder (BorderFactory.createRaisedBevelBorder ());
    		logo2.add (new JLabel (icon5));
    		logo2.add (new JLabel ("                 Clausings Pizza Factory                "));
    		logo2.add (new JLabel (icon6));
    		order = new JButton ("                     Place Order                        ");
    		logo2.add (order);
    		order.addActionListener (listener);
    
    
    		add (logo2, BorderLayout.CENTER);
    
    		JPanel salads = new JPanel();
    		TitledBorder tb6 = BorderFactory.createTitledBorder
    		("Side Salads:");
    		tb6.setTitleJustification (TitledBorder.CENTER);
    		salads.setBorder (tb6);
    
    
    
    		salad1 = new JRadioButton ("Small");
    		salad1.addActionListener (listener);
    		salad2 = new JRadioButton ("Large");
    		salad2.addActionListener (listener);
    		salad3 = new JRadioButton ("All you can eat salad bar");
    		salad3.addActionListener (listener);
    		ButtonGroup saltype = new ButtonGroup();
    
    		salads.setLayout (new GridLayout (3, 1));
    		saltype.add (salad1);
    		saltype.add (salad2);
    		saltype.add (salad3);
    
    		salads.add (salad1);
    		salads.add (salad2);
    		salads.add (salad3);
    
    		add (salads, BorderLayout.WEST);
    
    		JPanel snacks = new JPanel();
    		TitledBorder tb7 = BorderFactory.createTitledBorder
    		("Desserts & Snacks:");
    		tb7.setTitleJustification (TitledBorder.CENTER);
    		snacks.setBorder (tb7);
    
    		JCheckBox gb = new JCheckBox ("Garlic Bread");
    		JCheckBox cs = new JCheckBox ("Cheesy Sticks");
    		JCheckBox cw8 = new JCheckBox ("Chicken Wings - 8 wings");
    		JCheckBox cw16 = new JCheckBox ("Chicken Wings - 16 wings");
    		JCheckBox sb = new JCheckBox ("Sticky Buns");
    
    		snacks.setLayout (new GridLayout (5, 1));
    
    		snacks.add (gb);
    		snacks.add (cs);
    		snacks.add (cw8);
    		snacks.add (cw16);
    		snacks.add (sb);
    
    		add (snacks, BorderLayout.EAST);
    
    		JPanel TOTAL = new JPanel();
    		TitledBorder tb8 = BorderFactory.createTitledBorder
    		("Your Order Total:");
    		tb8.setTitleJustification (TitledBorder.CENTER);
    		TOTAL.setBorder (tb8);
    
    		NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
    		TOTAL.add (new JLabel ("Your order total is:  " + fmt1.format(totals.o_total)));
    
    		add (TOTAL, BorderLayout.SOUTH);
    
    
    	}// end constructor
    
    	public class MyButtonListener implements ActionListener
    	{
    
    		public void actionPerformed (ActionEvent event)
    		{
    			Object source = event.getSource();
    			if (source == salad1)
    			{
    				totals.cost += 2.0;
    				System.out.print (totals.cost);
    			}
    			if (source == salad2)
    				totals.cost += 3.0;
    			if (source == salad3)
    				totals.cost += 5.0;
    
    			if (source == gb)
    				totals.cost += 3.0;
    			if (source == cs)
    				totals.cost += 3.0;
    			if (source == cw8)
    				totals.cost += 5.0;
    			if 	(source == cw16)
    				totals.cost += 8.0;
    			if (source == sb)
    				totals.cost += 5.0;
    
    
    			if (source == order)
    				totals.o_total += totals.cost;
    
    		}
    
    
    	}// end class MyButton Listener
    
    } // end class ExtrasTab
    My variable totals.cost seems to be working properly (looking at my sys.out.print) but totals.0_total does not update in my (TOTAL) panel. Is it because it is a JButton in a listener for JRadioButtons?

    If I do a sys.out.prt for totals.o_total in my if statement of the listener, it appears to output the correct number, so it seems as if that panel (TOTAL) is not refreshing so therefore not displaying the current value of my variable... but if that is the case, I seem to be stuck on how to call the value of the updated variable.

    Can someone give me a hint of where or how I went wrong?

    Thanks,
    Rob

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Have you tried revalidating or repainting the JPanel so that it updates the variable?

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    TOTAL.add (new JLabel ("Your order total is:  " + fmt1.format(totals.o_total)));
    I would guess the above line of code is your problem. You create a label and assign it some text.

    Changing the totals.o_total variable later in your code will not change the text of the label.

    You need to declare a label like:

    Java Code:
    totalLabel = new JLabel();
    TOTAL.add( totalLabel );
    Then when you recaculate the total you update the label by using:

    Java Code:
    totalLabel.setText("some new text here");
    Also, learn how to use Java variables. "TOTAL" is NOT a valid variable name. Java variable should NOT be uppercased.
    Last edited by camickr; 02-09-2010 at 12:46 AM.

  4. #4
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post

    Also, learn how to use Java variables. "TOTAL" is NOT a valid variable name. Java variable should NOT be uppercased (sic).
    This reads as rather condescending and/or pejorative.

    I hope that I did not offend your delicate sensitivities by naming my JPanel in caps. Is a JPanel really a variable??? I realize that only constants should be in caps, and I named that panel in all caps because it exists identically in several different tab classes. I will endeavor not to make that mistake again.

    Thanks Collin, will try to figure out how to repaint that panel upon the 'order' button click. That seems like the answer.

    I guess I do not need a listener class for each type of button then?
    Last edited by rclausing; 02-09-2010 at 03:01 AM.

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I give you the answer to your question and provide additional information about Java programming standards and I get no thanks.

    Good luck with your future questions, I won't bother wasting my time in the future.

  6. #6
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    I have to call them like I see them. If your intent was not to insult me, then I am mistaken and I apologize.
    Java Code:
    public static final int TOTAL;
    looks like a valid variable name to me...

    Can't you see why I interpreted your post as I did? Perhaps I will just find another board on which to find assistance.

    Thank you for the help everyone.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I have to call them like I see them.
    No you don't. All you have to do is read the suggestions, understand them and move on. You apparently took no time to understand either of my suggestions. If you did you would know that both suggestions where made in good faith and correct to the best of my knowledge.

    looks like a valid variable name to me...
    Well, obviously its a "valid" variable name, since the program compiled. I was commenting on the uppercased characters. Uppercased variable names have a special meaning. I did not think I needed any more clarification to my suggestion, since any text book I've ever seen does use proper naming standards.

    Java Code:
    public static final int TOTAL;
    Even in the above example, that is generally NOT the way "final static" variables are used. final, static variables are used to identify variables with a "constant value" that don't change anywhere in the program. In fact the compiler doesn't allow them to be changed once they've been initialized.

    Here is a link to Sun Documentation on Naming Conventions

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rclausing View Post
    This reads as rather condescending and/or pejorative. ...
    I have to agree with camickr that you may be seeming bit thin-skinned here and biting the proverbial hand that feeds you. If you follow his suggestion about appropriate naming convention you will make it easier for others (such as us!) to follow your code and be able to help you. If you criticize him for trying to help you, you risk offending not just him, but others who might have helped you in the future. It's your call of course, but I'd have played this one a bit differently. Much luck.

  9. #9
    rclausing is offline Member
    Join Date
    Oct 2009
    Location
    Oregon
    Posts
    22
    Rep Power
    0

    Default

    Apparently my English skills lack nearly as much as do my Java coding skills.

    Just so we are clear, there is no "final static int TOTALS" in my program, it was just used as a poor example. The only "TOTALS" I have is the name I gave to a JPanel. A JPanel that exists/is constant across several other JPanels. Because of that it seemed like a logical name at the time. I have edited the name in my code, as I defer to your experience (both).

Similar Threads

  1. TreeViewer Listener
    By darie20 in forum SWT / JFace
    Replies: 0
    Last Post: 01-25-2010, 12:23 PM
  2. Tab Listener
    By teckno101 in forum AWT / Swing
    Replies: 2
    Last Post: 09-29-2009, 09:40 PM
  3. question on listener interface
    By Minu in forum Java Servlet
    Replies: 1
    Last Post: 01-16-2009, 10:33 AM
  4. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-20-2008, 11:07 PM
  5. Regarding Listener
    By adeeb in forum AWT / Swing
    Replies: 2
    Last Post: 06-10-2008, 02:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •