1

I am trying to update an empty Jlabel every time I click the 'Load' Jbutton. I've added an actionlistener to the Jbutton, but for some reason the label just doesn't update or make any text appear with the setText(String) method.

JLabel stupidLabel = new JLabel();
    stupidLabel.setForeground(SystemColor.infoText);
    stupidLabel.setFont(new Font("Arial", Font.PLAIN, 11));
    stupidLabel.setBounds(71, 46, 167, 14);
    panel.add(stupidLabel);'

JButton load = new JButton("Load");
    load.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

           stupidLabel.setText("Update please");
        }
    });
    load.setFont(new Font("Arial", Font.PLAIN, 11));
    load.setBounds(189, 92, 89, 22);
    contentPane.add(load);

Doesn't seem to work.

What seems to be the problem? Is there a way to make the label update automatically every second or so, effectively removing the need for a button altogether?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
J. Rad
  • 11
  • 2
  • Sounds like you're shadowing your variables, but the possible use of a `null` layout isn't going to help. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Aug 13 '15 at 02:04
  • `new Font("Arial", Font.PLAIN, 11)` For both compile time checking and cross-platform robustness, that should best be: `new Font(Font.SANS_SERIF, Font.PLAIN, 11)` – Andrew Thompson Aug 13 '15 at 03:21

1 Answers1

2

Works fine for me...

Show me the label

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            JLabel stupidLabel = new JLabel(" ");
            stupidLabel.setForeground(SystemColor.infoText);
            stupidLabel.setFont(new Font("Arial", Font.PLAIN, 11));
            add(stupidLabel, gbc);

            JButton load = new JButton("Load");
            load.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    stupidLabel.setText("Update please");
                }
            });
            load.setFont(new Font("Arial", Font.PLAIN, 11));
            add(load, gbc);
        }

    }

}

Consider providing a runnable example which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hmm, strange... I have the exact same thing almost. Does it make a difference that my load button is in a different JPanel than the label? – J. Rad Aug 13 '15 at 02:34
  • Weird. I guess I will have to rewrite the program, I'm at my ends trying to figure this out. Thank you for your help. – J. Rad Aug 13 '15 at 02:43
  • *"I have the exact same thing almost."* Nope. Not using layouts is 'miles' from this code. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 13 '15 at 03:19