Reputation: 1178
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);
}
}
Upvotes: 1
Views: 3760
Reputation: 10995
JLabel should be an instance member. An handler will help you modify the Label's text in instance methods.
public class server
{
private JLabel lblStatus= new JLabel("Text") ;
public void changeLabel(String text)
{
lblStatus.setText(text) ;
}
}
In main: window.changeLabel("In main") ;
This has to be done because in your initialize method, there reference to lblStatus
is lost
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("..."); // Forgotten when method finishes
lblServant.setFont(new Font("Tahoma", Font.PLAIN, 10));
lblServant.setBounds(10, 36, 231, 14);
frmCorbaServer.getContentPane().add(lblServant);
}
Upvotes: 4
Reputation: 83527
I'm not familiar with CORBA programming, so I can only answer with some general advice. First you need to know when the server connects. Most likely this doesn't occur in your main()
method. Is there a listener or some other callback method which is called to notify you that the server is connected?
Once you figure out that part, changing the text of a JLabel is fairly easy. First you need to declare an instance variable:
private JLabel lblStatus;
When you create the JLabel in initialize()
, you don't need the declaration:
lblStatus = new JLabel("...");
Finally, when you know that the server has connected, simply call the setText()
method:
lblStatus.setText("Server connected");
Upvotes: 0