Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By zweibieren

Thread: Append to JTextArea instantly Problem

  1. #1
    jrdncchr is offline Member
    Join Date
    Mar 2011
    Posts
    14
    Rep Power
    0

    Default Append to JTextArea instantly Problem

    I got this GUI, and when I click the button, this is its action:

    Java Code:
     public void startServer() {
            ServerSocket ss = null;
            
            if(jButton1.getText().equals("Start")){
                try{
                    jButton1.setText("Stop");
                    taMessages.append("Starting the server..\r\n");
                    ss = new ServerSocket(50000);
                    taMessages.append("The server started at port: " + ss.getLocalPort() + "\r\n");
                    System.out.println("Starting the server..");
                    while(true){
                        try{
                            Socket s = ss.accept();
                            taMessages.append("A client has connected!");
                            ClientThread ct = new ClientThread(s);
                            ct.start();
                        }catch(Exception e){
                            System.out.println(e);
                        }
                    }
                }catch(Exception e){
                    System.out.println(e);
                }
            }else{
                try{
                    jButton1.setText("Start");
                    taMessages.append("Server Stopped!\r\n");
                    ss.close();
                }catch(Exception e){
                    System.out.println(e);
                }
            }
        }
    The server waits until a client connect, before it prints the texts..

    I read about EDT and other issues related to this, but I can't simply understand them.. Can someone help me?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Append to JTextArea instantly Problem

    Have you gone through Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing) ? If you have, and still don't understand what you need to do, it's rather doubtful that anybody here would be able to help you either.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default Re: Append to JTextArea instantly Problem

    startServer contains an infinite loop: while(true) {}.
    If it is called on the EDT, that thread will be blocked, preventing updates.

    The ss.accept() needs to be in its own thread.
    Fubarable likes this.

Similar Threads

  1. Problem in JtextArea
    By justbeller in forum AWT / Swing
    Replies: 11
    Last Post: 07-25-2011, 09:20 AM
  2. Problem with JTEXTAREA
    By javanew in forum New To Java
    Replies: 6
    Last Post: 05-07-2010, 02:14 PM
  3. JTextArea problem
    By javanew in forum AWT / Swing
    Replies: 2
    Last Post: 05-04-2010, 10:31 PM
  4. Replies: 13
    Last Post: 12-11-2009, 08:43 AM
  5. Replies: 4
    Last Post: 01-05-2008, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •