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

  • Home
  • SEARCH
  • 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 8038411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:13:40+00:00 2026-06-05T03:13:40+00:00

I’m writing a client application with an accompanying GUI (Swing). My two classes, ClientClass

  • 0

I’m writing a client application with an accompanying GUI (Swing). My two classes, ClientClass and MainFrame, are running different threads yet need to call methods in one another. ClientClass instantiates the GUI (MainFrame) at some point during the applications life cycle on the EventQueue thread (displayGUI()). ClientClass contains many methods like recv(), called from the client classes thread, that updates MainFrame. In turn, MainFrame has methods that are triggered by events like the push of a button which call methods in ClientClass. I’m assuming the annoymous method that handles a button press in the example is being called by the EventQueue thread?

I’m pretty sure this kind of application is very common and I’d love the insight of others. I have a feeling that what I’m doing isn’t thread safe, so how can I fix/improve the current model of this application?

Example code:

MainFrame.java:

public MainFrame(ClientClass c) {
    client = c;

    // <Misc init code here>

    btnSend = new JButton("Send");
    btnSend.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            client.send("Hello!");
        }
    });
    btnSend.setBounds(171, 120, 89, 23);
    contentPane.add(btnSend);
}

public void updateElement() {
    // Update of some element here, called from ClientClass
}

ClientClass.java:

private MainFrame mainFrame;

public ClientClass() {
}

public void displayGUI() {
    final ClientClass c = this;

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                mainFrame = new MainFrame(c);
                mainFrame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void send(String msg) {
    // Socket send operations here
    // Currently called by the GUI's EventQueue thread?
}

public void recv() {
    // Socket recv operations here
    mainFrame.updateElement();
}
  • 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-05T03:13:42+00:00Added an answer on June 5, 2026 at 3:13 am

    You can use SwingUtilities to invoke code on EDT (on UI thread).

    Mouse clicked is invoked on UI thread – so you should invoke send in the background (to not block UI if it takes long).

    On your recv method you should invoke mainFrame.updateElement(); on UI thread if you change GUI state (JLabel text, etc.) – you do this by:

    SwingUtilities.invokeLater(new Runnable() {... //
    

    Generally speaking – anything you do that may impact GUI elements (changing text, invalidating, adding components, etc.) you should do on EDT.

    And everything that may block user interface you should do in background – spawning new Thread.

    If you need to block any events and wait to background – you can show modal JDialog (remember that you should hide it in finally block – just in case)

    You should also look at the SwingWorker class and at the tutorial.

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

Sidebar

Related Questions

I am writing a Client/Server application need to communicate with each other via RPC.
I am writing a client-side Swing application (graphical font designer) on Java 5 .
I'm writing some client-server-application where I have to deal with multiple threads. I've got
I'm writing client-server application and I need my server to find all clients in
Writing a client application that sends images to a server via a webservice. As
I'm writing a client application to communicate with a server program via UDP. The
I am writing a client-side .NET application which is expected to use a lot
I am currently writing an application having a client server architecture. The client is
We are writing an ASP.NET MVC application. By default, if the client browser has
I am writing a client-server application using TCP Sockets. The server is written in

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.