Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:15:43+00:00 2026-06-12T12:15:43+00:00

I have this Java Swing application that starts a new thread that uses a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:15:44+00:00Added an answer on June 12, 2026 at 12:15 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Swing application that uses a Java Thread to constantly perform some
I have implemented an Java Swing application that uses an embedded JavaDB database. The
We have a desktop application written in Java Swing that currently uses Java Web
I have this java swing application that I intend to sell over the internet.
So I have this nice spiffy MVC-architected application in Java Swing, and now I
I have developed a Java Swing application, which uses the SwingWorker class to perform
I am working in a java swing application. In that application i have to
The scenario is this: I have Java swing application with JFrame. There is textarea
I have a Java Swing application, that has many JTextFields and a datamodel. When
I have a Java swing application with a panel that contains three JComboBoxe s

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.