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");
}  
有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top