Search code examples
javaswingactionlistenerjlabelsettext

Changing Text Value of JLabel In Real Time


I am trying to change the text of this JLabel (searchedstock) in real time. Here is my code:

public void gamegui() {

    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridwidth = 2;
    JLabel welcome = new JLabel("Welcome " + playername + "!");
    welcome.setFont(new Font("Arial", 1, 30));
    gamepanel.add(welcome, c);
    c.gridx = 0;
    c.gridy = 1;
    gamepanel.add(new JPanel(), c);
    c.gridwidth = 1;
    c.gridy = 2;
    gamepanel.add(new JLabel("Symbol Search:"), c);
    c.gridx = 1;
    final JTextField symbolsearch = new JTextField(10);
    gamepanel.add(symbolsearch, c);
    c.gridx = 0;
    c.gridy = 3;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 2;
    JButton search = new JButton("Search");
    search.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e){

            stockvalue = Double.toString(getstocks(symbolsearch.getText()));
        }
    });
    gamepanel.add(search, c);
    c.gridx = 0;
    c.gridy = 5;
    gamepanel.add(new JPanel(), c);
    c.gridx = 0;
    c.gridy = 6;
    c.gridwidth = 2;
    JLabel searchedstock = new JLabel(stockvalue);
    gamepanel.add(searchedstock, c);
    add(gamepanel);
}

How I can change the value in realtime from pressing that button because its listener is in an inner class? It does not have direct access to my JLabel, so I cannot use setText(); and this is my issue.


Solution

  • You have at least two options...

    Option #1

    Make the JLabel final, but you have to do this BEFORE the inner listener...

    public void gamegui() {
        /*...*/
        final JLabel searchedstock = new JLabel(stockvalue);
        JButton search = new JButton("Search");
        search.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e){
    
                stockvalue = Double.toString(getstocks(symbolsearch.getText()));
                searchedstock.setText(NumberFormat.getNumberInstance().format(stockValue));
            }
        });
        gamepanel.add(search, c);
        c.gridx = 0;
        c.gridy = 5;
        gamepanel.add(new JPanel(), c);
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 2;
        gamepanel.add(searchedstock, c);
        add(gamepanel);
    }
    

    Option 2

    Make the label a class instance field...

    private JLabel searchedstock;
    
    public void gamegui() {
        /*...*/
        final JLabel searchedstock = new JLabel(stockvalue);
        JButton search = new JButton("Search");
        search.addActionListener(new ActionListener() {
    
            public void actionPerformed(ActionEvent e){
    
                stockvalue = Double.toString(getstocks(symbolsearch.getText()));
                searchedstock.setText(NumberFormat.getNumberInstance().format(stockValue));
            }
        });
        gamepanel.add(search, c);
        c.gridx = 0;
        c.gridy = 5;
        gamepanel.add(new JPanel(), c);
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 2;
        searchedstock = new JLabel(stockvalue);
        gamepanel.add(searchedstock, c);
        add(gamepanel);
    }