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 5 of 5

Thread: revalidate() JPanel outside of class it resides

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Location
    Oregon
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default revalidate() JPanel outside of class it resides

    Can this be done? I have a JPanel whose contents (JLabel) have a variable that changes as the program executes, however I cannot figure out how to refresh the panel/label to show the current value. I made the label in question public static so that it could be accessed outside of the class, but I get a run time error no matter how I try to redraw the JLabel.

    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 static JLabel totalLabel;
     
     
     
        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);
     
     
     
    		JLabel totalLabel = new JLabel ();
    		Total.add (totalLabel);
     
    		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 + "\n" + totals.o_total + "\n");
    			}
    			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;
    					//totalLabel.getRootPane().revalidate();
    					//totalLable.updateUI();
    					//totalLabel.repaint();
    					NumberFormat fmt1 = NumberFormat.getCurrencyInstance();
    					totalLabel.setText("Your order total is:  " + fmt1.format(totals.o_total));
    					System.out.println("totals \t" + totals.o_total + "\n cost \t" + totals.cost);
    				}
     
    		}
     
     
    	}// end class MyButton Listener
     
     
    } // end class ExtrasTab

    revalidate, updateUI, repaint, and setText all cause a run time error:

    - Exception in thread "AWT-EventQueue-0" java.lang.NullPointException at ExtrasTab$MyButtonListeneer.actionPerformed(Extras Tab.java:135)

    Line 135 is the setText line. Is there a way to refresh the panel that shows my variable from inside the listener? If not, is there a way to accomplish what I am trying to do here?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: revalidate() JPanel outside of class it resides

    The following lines...

    JLabel totalLabel = new JLabel ();
    Total.add (totalLabel);
    ...create a local JLabel named totalLabel and does not use your static variable, so every time you try to access your static variable (which hasn't been constructed) you will get a null pointer exception. It shouldn't be necessary to make the variable static, just make sure you instantiate it

    totalLabel = new JLabel ();
    Total.add (totalLabel);

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Location
    Oregon
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: revalidate() JPanel outside of class it resides

    I'm sorry, but I am lost on that. That was how I instantiated the label, then used setText after the listener adjusted the variable, but that is what causes the run time error. So I am as clear as mud as to what you are suggesting.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: revalidate() JPanel outside of class it resides

    Quote Originally Posted by Deadbob View Post
    I'm sorry, but I am lost on that. That was how I instantiated the label, then used setText after the listener adjusted the variable, but that is what causes the run time error. So I am as clear as mud as to what you are suggesting.
    To break the problem down, the following code demonstates (at least hopefully)
    import java.awt.event.*;
    import javax.swing.*;
     
    public class Test  extends JFrame implements ActionListener{
     
    	private JLabel textLabel;
     
    	public Test(){
    		JPanel main = new JPanel();
    		JLabel textLabel = new JLabel("TextTextText");/**Problem Lies Here*/
    		JButton button = new JButton("Action");
    		button.addActionListener(this);
    		main.add(button);
    		main.add(textLabel);
    		getContentPane().add(main);
    		pack();
    		setVisible(true);
    	}
    	public void actionPerformed(ActionEvent e){
    		textLabel.setText("testing...");
    	}
    	public static void main(String[] args){
    		new Test();
    	}
    }

    Compile, run, and hit the button and you will get a null pointer exception. Now, change the line where it says problem lies here to:
    textLabel = new JLabel("TextTextText");
    The difference between the two being that in the first case you mask the instance variable textLabel by creating a local variable of the same name - the instance variable never gets assigned anything and its value will therefore be null (the reason you get a NullPointerException when you try to access it later on). In the second case, you assign a value (instantiate) to the instance variable, so when you access its value later on you are not accessing something which is null

  5. The Following User Says Thank You to copeg For This Useful Post:

    Deadbob (February 9th, 2010)

  6. #5
    Junior Member
    Join Date
    Feb 2010
    Location
    Oregon
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: revalidate() JPanel outside of class it resides

    That definitely did the trick. And in all that code, it sure is easy to get lost on such syntax errors. Thanks much for that.

Similar Threads

  1. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  2. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  3. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 06:16 PM
  4. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM
  5. Replies: 4
    Last Post: May 27th, 2008, 01:20 PM