I apologize if the solution to this question is obvious, I am sure the solution is simple, I just cant seem to get it right in my head.
I have created my JFrame as shown in the code below. I am looking to change the text of a label ( lblStatus ) from within my main method and I cant seem to get it working. Do I need to create a new instance of the label or something along those lines ?
Can anyone advise me on what approach to take ?
Regards, Dan.
NB- I have removed some content to highlight the relevant code.
public class server {
private JFrame frmCorbaServer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
server window = new server();
window.frmCorbaServer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
// SERVER CONTENT
// If connection made
System.out.println("Server connected"); // I want this to display in lblStatus!
}
/**
* Create the application.
*/
public server() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmCorbaServer = new JFrame();
frmCorbaServer.setTitle("server 0.1");
frmCorbaServer.setResizable(false);
frmCorbaServer.setBounds(100, 100, 257, 153);
frmCorbaServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCorbaServer.getContentPane().setLayout(null);
JLabel lblStatus = new JLabel("...");
lblServant.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblServant.setBounds(10, 36, 231, 14);
frmCorbaServer.getContentPane().add(lblServant);
}
}
JLabel should be an instance member. An handler will help you modify the Label's text in instance methods.
In main:
window.changeLabel("In main") ;
This has to be done because in your initialize method, there reference to
lblStatus
is lost