Java swing GUI freezes

java gui frozen
java program freezes
java application hangs windows 10

I am writing a Java client/server GUI application using sockets and here is the problem:

I have a button to start listening for a specified port:

button actionPerformed method

private void listenButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    int port = Integer.parseInt(portTextfield.getText(), 10);

    try {
    } catch (IOException ex) {

Here is the socket.listen method

public static void listen() throws IOException {
    ServerSocket ss = new ServerSocket(port);

    while (true)
        new socket(ss.accept());

"socket" class extends "Thread" So after ss.accept() returns a value it creates new socket instance in separate thread.

After clicking the button the GUI freezes because inside the socket.listen method there is an infinite loop. How can I avoid that?

Swing GUI Freezes When JButton Pressed, I am making a GUI in Swing and I have a button that starts a jar program but it freezes the rest of the GUI program when the button is pressed. This  Like Swing is often having trouble with "dead" Listeners holding some references which can not be garbage collected (resulting in very slow responses or freezing of UI or even memory leaks). Profiler will help you to investigate memory needs of specific situations when using your application and therefore you might be able to do fine tuning of your app.

as long as your

new socket(ss.accept());

returns immediately, you only need to change your

while (true)

this puts the EDT (Event Dispatch Thread) into an infinite loop and your GUI becomes irresponsive. So, delete this line.

If you can't then use the SwingWorker class ( Create a nested class that extents SwingWorker. Just call a swingWoker.execute(); (after you have created its object) in your listenButtonActionPerformed(java.awt.event.ActionEvent evt) method.

See the tutorial:

Never create a new thread and run it from from the Swing EDT

Swing appears to Freeze (Swing / AWT / SWT forum at Coderanch), Swing appears to Freeze I'm writing a gui for a program i wrote for an assignment at uni. Swing Problem with Head First Java example. Swing GUI freezes when JButton pressed: Hi, I am making a GUI in Swing and I have a button that starts a jar program but it freezes the rest of the GUI program when the button is pressed.

You will need to use Multi-Threading. If I where you, I would separate the GUI code and the server code and when the button is pressed, I simply launch the Server code as a new Thread.

Your code is freezing the GUI basically because all events are executed on the Event Dispatcher Thread (EDT) which is the thread which takes care of all your GUI stuff and respective events. If you either block it, stop it or throw in loops it will affect on its performance.

Java application randomly freezes. [SOLVED], I've never worked on Swing/AWT but there are a couple of universal things you should know about single threaded UI toolkit (and Swing is one of  Free download Java for Swing (GUI) Development. This tutorial/course is created by Saran V Balachandar. Learn how to program in Java and build beautiful desktop applications with it..

Check this out:

1) If you are writing GUI application may be in Swing never call blocking method in Event dispatcher thread or in the event handler. for example if you are reading a file or opening a network connection when a button is clicked don't do that on actionPerformed() method, instead just create another worker thread to do that job and return from actionPerformed(). this will keep your GUI responsive, but again it depends upon design if the operation is something which requires user to wait than consider using invokeAndWait() for synchronous update.

Using multiple threads:

Freeze « Thread « Java Swing Q&A, My GUI is frozen I have something I can't understand: my Swing GUI contains a 'play' and 'pause' button. I have also a static variable that  3. Java GUI, need to pause a method without freezing GUI aswell I know that this problem is caused by the sleep or wait calling on the main thread and that the answer on how to solve this will be to put the

Try these...

1. During getting the initial connection delay can occur, so first create and empty socket,then try to connect to the server.

   `Socket s = new Socket();`

   `s.connect(new InetSocketAddress("ip_addr",port_nos),1000);`

2. And Secondly always keep the Non-UI work out of Your UI thread..

Here is my Example of Server - Client Communication..

Client side code:

public class ClientWala {

    public static void main(String[] args) throws Exception{

        Boolean b = true;
    Socket s = new Socket();
    s.connect(new InetSocketAddress("", 4444),1000);

    System.out.println("connected: "+s.isConnected());

    OutputStream output = s.getOutputStream();
    PrintWriter pw = new PrintWriter(output,true);

    // to write data to server

        if (!b){


        else {
            pw.write(new Scanner(;

    // to read data from server
    InputStream input   = s.getInputStream();
    InputStreamReader isr = new InputStreamReader(input);
    BufferedReader br = new BufferedReader(isr);
    String data = null;

    while ((data = br.readLine())!=null){

        // Print it using sysout, or do whatever you want with the incoming data from server



Server side code:


public class ServerTest {

    ServerSocket s;

    public void go() {

        try {
            s = new ServerSocket(44457);

            while (true) {

                Socket incoming = s.accept();
                Thread t = new Thread(new MyCon(incoming));
        } catch (IOException e) {



    class MyCon implements Runnable {

        Socket incoming;

        public MyCon(Socket incoming) {

            this.incoming = incoming;

        public void run() {

            try {
                PrintWriter pw = new PrintWriter(incoming.getOutputStream(),
                InputStreamReader isr = new InputStreamReader(
                BufferedReader br = new BufferedReader(isr);
                String inp = null;

                boolean isDone = true;

                System.out.println("TYPE : BYE");
                while (isDone && ((inp = br.readLine()) != null)) {

                    if (inp.trim().equals("BYE")) {
                                .println("THANKS FOR CONNECTING...Bye for now");
                        isDone = false;

            } catch (IOException e) {
                // TODO Auto-generated catch block
                try {
                } catch (IOException e1) {
                    // TODO Auto-generated catch block



    public static void main(String[] args) {

        new ServerTest().go();



java: Swing GUI freezes, Now, after I click on the button,nothing happens (gui freezes) and after I JFrame; import javax.swing.SwingUtilities; import java.awt.event. This is the reason you are getting a freeze or technically speaking a "deadlock". I'll also show you how to find out such deadlocks. Assuming you are running JDK 6, it already comes bundled with a profiling tool called Visual VM in the bin directory.

GUI freeze - Foxtrot - Easy API for JFC/Swing, Logo Logo The GUI freeze problem. When you write Swing applications, you show a GUI to a user; the user clicks on some  What is Swing? Java Swing is a lightweight Graphical User Interface (GUI) toolkit that includes a rich set of widgets. It includes package lets you make GUI components for your Java applications, and It is platform independent. The Swing library is built on top of the Java Abstract Widget Toolkit (AWT), an older, platform dependent GUI toolkit. You can use the Java GUI components like button, textbox, etc. from the library and do not have to create the components from scratch.

How come a Java Swing program freezes when we put a while (true , ActionListeners are executed in Swing on the Event Dispatching Thread or EDT. This is the thread that handles all user interaction with the GUI. So if a particular  Careful use of concurrency is particularly important to the Swing programmer. A well-written Swing program uses concurrency to create a user interface that never "freezes" — the program is always responsive to user interaction, no matter what it's doing.

[JDK-8202146] Java AWT thread freeze when java.awt.Component , Re: Server Socket GUI Freezes. You will want to read/study this tutorial on Concurrency in Swing. It will explain what your problem is, and how to  The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

  • and what your question please ..., for better help sooner post an SSCCE
  • Read about Concurrency in Swing.
  • For reference, there's a working example here.