0

I want to be able to change the content of a some JLabel objects from another class. I have a GUI class containing all GUI objects (JFrame, JPanel, etc.). Let's say I have only one JLabel:

public class GUI {
    private JLabel label;

    public GUI() {
        initialize();//initializes all objects
    }

    public void update(String s) {
        this.label.setText(s);
    }
}

GUI has a public function update(String) that I hope to call from another class.

My main class is called App:

public class App {
    private static GUI window;

    public static void main( String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    window = new GUI();
                    App.updateTxt("some string");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
         });
    }

    public  static void updateTxt(String s) {
        window.update(s);
    }
}

This solution does not work, however, GUI.update(String) is working perfectly when called inside the GUI class. I checked the solution proposed by Paul in: Access GUI components from another class but I did not understand it. So how can I invoke a method on GUI from another class to change the UI?

  • 1
    Please provide a [mcve] so we can easier understand your problem. The code posted here give us neither the information about how you GUI is built, nor how you call the method `updateTxt`. – Sergiy Medvynskyy Feb 13 '20 at 15:45
  • thank you for your reply Janus, the idea is to call App.updateTxt() from any class since App is where window (the instance of GUI) is saved. Let's say we call App.updateTxt("some string") just after the Instantiation of GUI => result: Unfortunately nothing changed in window – mohamed cherkaoui Feb 14 '20 at 09:19
  • Swing components have a repaint(), revalidate(), and doLayout() method. One of those should probably be able to redraw whichever pieces you want. However, doLayout is not something that you should be taking responsibility for, that's the layout engines responsibility. – clearlight Feb 14 '20 at 18:51

2 Answers2

0

Judging from the code posted, GUI.update(String) is never invoked as it is called from within App.updateText(String) which you never invoke as part of App.main(String[]).

Try the following changes to App:

public class App {
    private static GUI window;

    public static void main( String[] args) throws InterruptedException {
        window = new GUI();
        Thread.sleep(1000); // Pause for 1 second.       
        updateText("Hello, world!"); // Update the label with new text.
    }

    public  static void updateTxt(String s) {
        window.update(s);
    }
}
Janus Varmarken
  • 2,306
  • 3
  • 20
  • 42
  • thanks Janus, I tried this, Unfortunately still no changes on the window. In fact,GUI.update() is called and executed indeed, but the changes on the GUI aren't displayed. – mohamed cherkaoui Feb 14 '20 at 09:28
0

You should take care of threading, see: Concurrency in Swing

That said, try something like this:

public void update(final String s) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            this.label.setText(s);
        }
    });
}
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33