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 558343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:07:18+00:00 2026-05-13T12:07:18+00:00

these are my classes.when i run my MainClient class,one frame will be shown that

  • 0

these are my classes.when i run my MainClient class,one frame will be shown that get text from client and then send it to the server.at the first time when i click on the Send button,it will work correctly and send data to the server but suddenly the frame will confused and I can not do any thing like writing or clicking on the button!!please help me.

MainClient class:

//these are in the main(String[] args)

MainFrame farme = new MainFrame();
farme.setVisible(true);
c = new Socket("localhost", 5050);

os = new PrintWriter(c.getOutputStream(), true);
is = new BufferedReader(new InputStreamReader(c.getInputStream()));

//this method is not in the main(String[] args)
public static void active() {

    String teXt = MainClient.getText();
    System.out.println(teXt);
    os.println(teXt);
    String line = is.readLine();
    System.out.println("Text received: " + line);
}

my gui class which get text from client and by clicking on the send button will send that text to the server:(Send button action performed)

 public ChatFrame(InformationClass client) {
    initComponents();


    jButton1.setEnabled(false);
    this.client = client;
    jTextArea2.setText("");

}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    submit();
    clear();
}
private void submit() {
    String text = jTextArea1.getText();

    jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
    MainClient.setText(client.getCurrentName() + " : " + text + "\n");
    MainClient.active();
}

private void clear() {
    jTextArea1.setText("");
}
  • 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-05-13T12:07:19+00:00Added an answer on May 13, 2026 at 12:07 pm

    The MainClient.active() call is blocking waiting for the Socket to respond. Because it is called in the ActionListener it is blocking the Swing event dispatch thread on which all UI events are triggered. This is causing the UI to be unresponsive.

    If you can you should use SwingWorker to perform the blocking task in a background thread. See the documentation here:

    http://java.sun.com/javase/6/docs/api/javax/swing/SwingWorker.html

    If SwingWorker is not available then you can do it with something like this:

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // This gets run in a background thread
                String text = jTextArea1.getText();
                jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
                MainClient.setText(client.getCurrentName() + " : " + text + "\n");
                MainClient.active();
            }
        }).start();
    }
    

    That is an ugly way to do it but it is also the shortest and will work on any version of Java that has Swing.

    Edit: The clear method has to be called on the event dispatch thread as it accesses a UI component – jTextArea1.setText(“”). To make it run on the EDT do this:

    SwingUtilities.invokeLater(new Runnable() { 
        @Override 
        public void run() { 
            clear();
        } 
    });
    

    Anonymous inner classes are ugly especially when they are inside other anonymous inner classes but they are convenient for code samples. In your actual implementation you should create a class that implements Runnable to do the original background task to make the code more readable.

    Closures in Java 7 should make this sort of task a lot less cluttered.

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

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: [urlbase stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]] The last part of your… May 14, 2026 at 9:51 pm
  • Editorial Team
    Editorial Team added an answer There's a lot of solutions to this problem. A simple… May 14, 2026 at 9:51 pm
  • Editorial Team
    Editorial Team added an answer I'm assuming ActiveRecord opens a dedicated database connection for each… May 14, 2026 at 9:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.