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?