I am having trouble updating a jlabel in a method. here is my code:
JLabel curStatus = new JLabel("");
JButton jbtnSubmit;
public static void main(String[] args) {
test gui = new test();
gui.startGUI();
// gui.setCurStatus("testing!"); << seems to work here,
//but when i call it from another class, it doesn't want to run.
}
// Set up the GUI end for the user
public void startGUI() {
// These are all essential GUI pieces
new JTextArea("");
final JFrame jfrm = new JFrame("my program");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbtnSubmit = new JButton("Submit");
jfrm.add(jbtnSubmit);
jfrm.add(curStatus);
jfrm.setVisible(true);
}
public void setCurStatus(String inCurStatus) {
curStatus.setText(inCurStatus);
curStatus.setVisible(true);
}
what is happening, is that the label, curStatus is not appearing. for example, here is a call:
gui1.setCurStatus("Now running diagnostics... Please wait!");
Your problem appears to be one of misplaced references.
Here is how you create your GUI:
You create your “test” object (which should be named “Test” by the way to conform to Java naming conventions) inside of your main method. Since it is declared inside of main, this variable has scope only inside of main and is visible no where else.
You then tell us that you are calling the method like so:
The gui1 variable refers to a test class object but it likely refers to a different object than the test object that is being displayed since the original displayed test object is only refered to by a variable local to the main method.
To solve this, you must make sure to call setCurStatus on the currently displayed test object. How to do this depends on the rest of your code, something you’ve refused to show us despite our requests for you to do so.
Edit: Based on your latest bit of posted code (which still won’t compile for me since it is missing a method,
createTasksFile(), my assumptions are correct, you are callingsetCurStatus(...)on a gui object that is not the displayed one:On line (A) you create a new gui object and call setCurStatus on it, but it is not the GUI object that is being displayed but a completely different and unrelated object. It’s only relation is that it is an object of the same class as the one being displayed but that’s it. The solution is to get a reference to the displayed GUI and call this method on that object, and that object only.
Also, Robin’s assumptions are correct, in that even if you fix this, you’re going to be stuck with a Swing concurrency issue. The JLabel won’t update because the Swing thread is trying to open a file:
So we’re both right. The solution to this is to open your file on a background thread, a SwingWorker would work nicely here.
Edit 2
So to fix the reference problem, pass a reference of the gui into the runDiagnostics method using a gui parameter. Then call the setCurStatus method on this parameter. For example:
You would have to pass the GUI in when calling the method:
Also, please edit all your code so that it follows Java naming conventions. All class names should begin with a capital letter. This makes it much easier for others to understand what your code is doing.