How to update a java swing gui witha server from another thread?

▼魔方 西西 提交于 2019-12-12 13:09:48

问题


I have this Java Swing application that starts a new thread that uses a executor pool to open a socket server every time a incomming client tries to establish a connection.

The application need two buttons, one to start and another to stop the server. What I want is to show the server status, and disable the opposed button until its status changes.

This is what I have by now, but I don't know how could I communicate with the EDT when the thread stops. What I can do is just check the isRunning() method.

Would it be better to use a SwingWorker?

public class ServerManager implements Runnable {

    private Executor mExecutor          = Executors.newSingleThreadExecutor();
    private ServerSocket mServerSocket  = null;
    private int mDefaultPort            = 43012;    
    private volatile boolean isRunning  = false;

    public ServerManager (int port){
        mDefaultPort = port;        
    }

    @Override
    public void run() {
        try {           
            mServerSocket = new ServerSocket(mDefaultPort);
            isRunning = true;
            while (isRunning){
                mExecutor.execute(new IncomingClientThread(mServerSocket.accept()));
            }
        } catch (IOException e) {           
            e.printStackTrace();
        } finally {
            if(mServerSocket != null){
                stop();
                System.out.println("Server closed");
            }
        }
    }

    public void stop(){
        try {
            mServerSocket.close();
            isRunning = false;
        } catch (IOException e) {
            throw new RuntimeException("Error closing server", e);
        }
    }

    public synchronized boolean isRunning() {
        return isRunning;
    }

    public int getServerPort (){
        return mDefaultPort;
    }
}

And this is what I have in the GUI thread: I'm using just one button and changing its text everytime it's pressed, but if the server disconnects for some reason, the button stays the same.

connectButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(mServer.isStopped()){
                    new Thread (mServer).start();
                    connectButton.setText("Desconectar");
                    infoLabel.setText("Servidor online en IP: " + NetworkUtils.getLocalIpAddress()
                            + " puerto: " + mServer.getServerPort());
                    System.out.println(mServer.getIpAddress());
                }else{
                    mServer.stop();
                    connectButton.setText("Conectar");
                    infoLabel.setText("Offline");
                }
            }
        });

Any help is wellcome! Thanks.


回答1:


One possible solution is to give it part of the functionality of a SwingWorker -- give it a SwingPropertyChangeSupport object, and allow your GUI to listen for and respond to state changes.

e.g.,

public class ServerManager implements Runnable {
  public static final String IS_RUNNING = "is running"; // for the Event's name
  private SwingPropertyChangeSupport propChngSupport = new SwingPropertyChangeSupport(this);
  private volatile boolean isRunning  = false;
  // other variables

  // addPropertyChangeListener(...) {...} goes here
  // removePropertyChangeListener(...)  {...} goes here

  public void setIsRunning(boolean isRunning) {
    boolean newValue = isRunning;
    boolean oldValue = this.isRunning;
    this.isRunning = isRunning;
    propChngSupport.firePropertyChange(IS_RUNNING, oldValue, newValue);    
  }

  public void run() {
    // ....
  }

  // other methods
}

The key being to never change the isRunning property outside of its setter method.




回答2:


So, what I've done as was recommnded by @Hovercraft Full Of Eels was:

In the threaded class:

    public class ServerManager implements Runnable {

...     
        public static final String IS_RUNNING = "IS_RUNNING";

        private SwingPropertyChangeSupport pChange = new SwingPropertyChangeSupport(this);


        public void setIsRunning (boolean isRunning){
            boolean newValue = isRunning;
            boolean oldValue = this.isRunning;
            this.isRunning = isRunning;
            pChange.firePropertyChange(IS_RUNNING, oldValue, newValue);         
        }   

        public void addPropertyChangeListener (PropertyChangeListener listener){
            pChange.addPropertyChangeListener(IS_RUNNING, listener);
        }

        public void removePropertyChangeListener(PropertyChangeListener listener){
            pChange.removePropertyChangeListener(IS_RUNNING, listener);
        }
...
    }

And in the GUI class:

public class StatusPane extends JPanel{

    private ServerManager mServer;

    public StatusPane() {       
    ...
        mServer = new ServerManager();

        mServer.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent evt) {
                System.out.println(evt.getPropertyName() + " cambia su valor de " 
                        + evt.getOldValue() + " a " + evt.getNewValue());
            }
        });
    ... 
    }
}


来源:https://stackoverflow.com/questions/12790528/how-to-update-a-java-swing-gui-witha-server-from-another-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!