Question

I have a button and Label on JFRAME. Clicking on button try to connect linux Server and this takes few second. I want to set the label text Connecting.. and Connected.But this doesn't set the label text Connecting .. during the button operation executing. Any Idea how to do that

private void buttonLConnectActionPerformed(java.awt.event.ActionEvent evt) {
    labelLOut.setText("Connecting");
    String ip="";
    String userid="username";
    String password="password";
Main_Window.LBoxconnect=new SshConnection(ip,userid,password);
    int x=LBoxconnect.checkConnection();
    if(x==1){
    labelLOut.setText("Connected");
    if(dx==1)
        buttonBTTInsert.setEnabled(true);
    else
        lx=1;
    }
    else
    labelLOut.setText("Connection Failed");
}  
Was it helpful?

Solution

You should do long actions in a background thread, not in a button listener, otherwise your UI will seem frozen.

In your listener, set the text to "Connecting" and then launch a thread wrapping your connection stuff. At the end of the runnable, update the text again once the long operation has finished.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top