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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:26:33+00:00 2026-05-30T05:26:33+00:00

Why is it that when I execute my Program when I click Enter my

  • 0

Why is it that when I execute my Program when I click Enter my Application freezes?

import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import java.io.*;
import java.net.*;

public class KnockKnockUI{

      private Socket kkSocket = null;
      private PrintWriter out = null;
      private BufferedReader in = null;
      JTextArea clientArea;
      JTextArea serverArea;
    public KnockKnockUI(){

        establishConnection();
        initComponents();

    }

    public void initComponents(){

        JFrame frame = new JFrame("Chat");
        frame.setVisible(true);
        frame.setSize(320,189);
        frame.getContentPane().setLayout(null);

        serverArea = new JTextArea();
        serverArea.setLineWrap(true);
        serverArea.setBounds(10, 11, 284, 45);
        frame.getContentPane().add(serverArea);

        clientArea = new JTextArea();
        clientArea.setBounds(10, 67, 284, 46);
        frame.getContentPane().add(clientArea);

        JButton btnEnter = new JButton("Enter");
        btnEnter.setBounds(96, 124, 89, 23);
        frame.getContentPane().add(btnEnter);

        btnEnter.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try{
                    in.readLine();
                }catch(Exception e){

                }
                String fromUser = clientArea.getText();
                try {
                    fromUser = in.readLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                serverArea.setText(fromUser);
            }
        });

    }

    public void establishConnection(){

         //Establishes Connection
         try {
            kkSocket = new Socket("localhost", 4444);
            //
            out = new PrintWriter(kkSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: taranis.");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to: taranis.");
            System.exit(1);
        }

    }
    public static void main(String[] args){
        KnockKnockUI ui = new KnockKnockUI();
    }
}

This is a simple chat client using Java, there are other classes that I didn’t include. like The Server class and the Protocol class

  • 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-30T05:26:34+00:00Added an answer on May 30, 2026 at 5:26 am

    When you hit enter, the following is executed:

    try {
        in.readLine();
    } catch (Exception e) {
    }
    
    String fromUser = clientArea.getText();
    
    try {
        fromUser = in.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    serverArea.setText(fromUser);
    

    This chunk of code is being performed on the Event Dispatch Thread, which handles all Swing events. Your calls to in.readLine() will block until a line of input is available from your Socket, causing unhandled events to back up and thus causing your program to become unresponsive. If you want to perform a time expensive task, take a look at a SwingWorker (SwingWorker tutorial).

    Side note: Catching an Exception without printing its stack trace is almost always a bad idea. It can make problems with your code harder, if not impossible, to catch.

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

Sidebar

Related Questions

I have this program that should execute a piece of code base on the
I have a Python script that needs to execute an external program, but for
I have a Java program that executes from Spring Qquartz every 20 seconds. Sometimes
I'm writing a ruby program that executes some external command-line utilities. How could I
there is a method called viewDidLoad that execute a code when the view is
i have a jQuery line that execute 2 animations, what i want is to
Is there a good way to write F# scripts that execute stand-alone without compiling?
I have a query that I need to execute that I do not know
I have a repeatable business process that I execute every week as part of
I have a read query that I execute within a transaction so that I

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.