1

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.

Sharon Watinsan
  • 9,620
  • 31
  • 96
  • 140
  • 1
    Your code seems to work as specified. What about it doesn't work? – PearsonArtPhoto Nov 15 '12 at 20:49
  • When the `methodA()` is executed, the text `Hello` is not getting displayed on the Label field. it still remains as `Whooo ` – Sharon Watinsan Nov 15 '12 at 20:50
  • Are you calling `label.setBounds(0, 0, 50, 100);`? – Reimeus Nov 15 '12 at 20:59
  • 1
    Unless you are blocking the EDT, this code should work. Also, the frame.repaint() is unnecessary and overly unefficient. If you can't figure out a solution, try to post an [SSCCE](http://sscce.org). Doing that will lead you to either find the problem yourself or post an example on this forum and having people answering your problem directly with clear explanations. I strongly recommend that you try that. – Guillaume Polet Nov 15 '12 at 20:59
  • @Reimeus typo, I'll correct it – Sharon Watinsan Nov 15 '12 at 21:05

1 Answers1

2

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366