0

I can't seem to change a JLabel's text from another tab.

For example: Tab 1 has:

    JLabel headerLbl = new JLabel("Original Title");
    headerLbl.setSize(275, 40);
    headerLbl.setLocation(75, 10);
    headerLbl.setFont(new Font("Serif", Font.PLAIN, 28));
    headerLbl.setForeground(Color.BLUE);
    firstPanel.add(headerLbl);

Then Tab 2 would be an options tab where you can change the text of headerLbl. My code so far is:

private void initializeOptionsTab()
  {
    JPanel optionsPanel = new JPanel();
    optionsPanel.setLayout(null);

    JLabel headerLbl = new JLabel("Change Name To:");
    headerLbl.setSize(150, 25);
    headerLbl.setLocation(150, 15);
    optionsPanel.add(headerLbl);

    this.compTxtFld = new JTextField();
    this.compTxtFld.setSize(200, 20);
    this.compTxtFld.setLocation(120, 45);
    optionsPanel.add(this.compTxtFld);

    JButton setNewNameButton = new JButton("Set New Name");
    setNewNameButton.setSize(130, 25);
    setNewNameButton.setLocation(150, 85);
    optionsPanel.add(setNewNameButton);


    setNewNameButton.addActionListener(new ActionListener() 
    { 
        public void actionPerformed(ActionEvent e) 
        { 
            FrameMain.this.setNewNameButtonClick(); 
        }

    });
    this.tabbedPane.addTab("Options", optionsPanel);
  }

Then the code for the Button is:

private void setNewNameButtonClick()
  {
    setTitle(this.compTxtFld.getText());

    headerLbl.setText(this.compTxtFld.getText());
  }

So this gives me a Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException for lines

My guess is I cannot access the headerLbl in tab 1 from tab 2. What do I need to do to access it?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SS113
  • 548
  • 1
  • 11
  • 21
  • 1
    Can you post the entire stacktrace? And where does the exception occur? – Paul Samsotha Dec 13 '13 at 16:22
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Always copy/paste error & exception output. 3) `optionsPanel.setLayout(null);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Dec 13 '13 at 16:22
  • This is what I get:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at FrameMain.setNewNameButtonClick(FrameMain.java:615) at FrameMain.access$7(FrameMain.java:611) at FrameMain$13.actionPerformed(FrameMain.java:593) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) Line 615 is the headerLbl.setText(this.compTxtFld.getText()); Line 611 is the private void setNewNameButtonClick() Line 593 is the FrameMain.this.setNewNameButtonClick(); – SS113 Dec 13 '13 at 16:26
  • 1
    In your code you have two different `headerLbl`s. I wouldn't doubt there's a third declared globally which hasn't been initialized. – Paul Samsotha Dec 13 '13 at 16:29

1 Answers1

0

First of all it's a bad idea to have 2 labels both named headerLbl even though they are in different methods because it can cause confusion as you are coding.

The easiest solution I can think of is to declare headerLbl as a class variable

So like this:

public class FrameMain{
     JLabel headerLbl = new JLabel();
   //The rest is the same

}

and then inside your second method, change the name of headerLbl to something like changeNameLbl or something... so they don't conflict

XQEWR
  • 638
  • 3
  • 11
  • 24