Sharon Watinsan
Sharon Watinsan

Reputation: 9850

Change JLabel text from another class

I have the following class that draws a Label. (I have only given part of the code here). Everyhting works fine, the label gets displayed.

Now, i have another class called Caller Class. I have a method in that where i will use to change the value of this label. how can i do that

public class MyClass{

    private JLabel label;

    MyClass(){

       run();
    }

   public void editTheLabelsValue (String text) {
      label.setText(text);
      frame.repaint(); 
    }


    run(){
            .... // there were more code here, i removed it as it's not relevant to the problem
        label = new JLabel("Whooo");
        label.setBounds(0, 0, 50, 100);
        frame.getContentPane().add(label);
            .....
    }

later on, i will be using the following class to change the text of the above label. How can i do this.

public class Caller {

void methodA(){
MyClass mc = new MyClass();
mc.editTheLabelsValue("Hello");
}

}

1.) When the methodA() is executed, the text Hello is not getting displayed on the Label field. it still remains as Whooo. How can i correct this. I want the label text to be Hello once that method has been executed.

Upvotes: 1

Views: 4929

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

The immeditate problem I can see is to appears that you are either using a null layout or your don't understand how layout managers work.

The following code updates the label from the main class in a sub class via a setText method call. This method is called every second

enter image description here

public class PaintMyLabel {

    private int counter = 0;

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

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

                final MasterPane master = new MasterPane();

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(master);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                Timer timer = new Timer(1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        counter++;
                        master.setText("Now updated " + counter + " times");
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();

            }
        });
    }

    public class MasterPane extends JPanel {

        private JLabel label;

        public MasterPane() {
            label = new JLabel("Original text");
            setLayout(new GridBagLayout());
            add(label);
        }

        public void setText(String text) {
            label.setText(text);
        }

    }

}

If you're using a null layout, then stop it. Just don't. There are only a very small number of times you would ever use a null layout and I suspect this isn't one of them.

Upvotes: 2

Related Questions