DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005

    Question Swing Update Thread- Question

    I have a problem.

    I have one function to create and display a GUI and another method to read Strings from a socket.

    My problem is that whilst the Strings are being read from the socket , the GUI hangs until all the Strings have been retrieved from the socket before displaying the GUI( albeit with all of the retrieved Strings) and I can't really understand why this is.

    I have modified my code to include the Swing EventDispatcher Thread and have also placed the method to read Strings from the socket in a new Runnable (anonymous) class but to no avail.

    Could anyone help?

    I would want a situation where, as the Strings are being obtained they are being displayed in a JTextArea available within the GUI.

    This is the code of the constructors of the GUI class:

    public ShipLocatorGUI(Socket s, String sName) {
            setTitle("Real Time Marine Locator for "  + sName);
    		this.socket =s;
            if (socket.isClosed()) {
    		else {
    	javax.swing.SwingUtilities.invokeLater(new Runnable() {
    	        public void run() {
    		            createAndShowGUI();// method to diaply GUI
    	Runnable readLocations = new Runnable() {
        public void run() {
    		readFromSocket();// method to read from Socket

    If anyone needs a better explanation/more code , please ask. I really need a solution to this, asap.
    Any Help in this problem would be greatly appreciated.

  2. #2
    Join Date
    Nov 2004

    I'm old fashioned maybe...

    ...so I don't fancy the way you have implemented this. If the problem is
    a hanging gui caused by some other tight process, then you should implement
    the Runnable interface for that GUI, create and start a thread that does
    the socket reading, create the GUI and let the socket thread access the
    display components of your GUI if they are available (not null).

  3. #3
    Join Date
    Apr 2005
    Thanks for the reply, I'd like to be old-fashioned some day too ...;-)

    So let me see if i fully understand you..

    >If the problem is a hanging gui caused by some other tight process, then you should >implement the Runnable interface for that GUI

    Isn't that the same thing as the code does, sorry I'M very new to threading within GUI applications and I felt that the idea was to create a GUI within an instannce of a 'Runnable' object. Please advice.

    >create and start a thread that does the socket reading

    Should this be done in a seperate class? Doesn't the creation of the Runnable anonymous class handle this?

    Again thanks for your reply and my most sincere apologies for my irritating code

    I will be more than willing to hear your irritations with my code

  4. #4
    Join Date
    Nov 2004

    Anonymity is suspicious...

    ...jokes aside... I never use that method. Don't fancy it...

    Here is an old fashioned way of doing it. This app starts downloading from
    threee different urls at the same time, from the web web before it creates its
    gui, a JTextArea.
    You can write in the textarea while it downloads, and start new downloads
    wile others downloads are working, and a completed download queues up and
    waits int turn if another completed download is writing its data to the GUI

    Isn't this something that does what you wanted to achieve ?
    Attached Files Attached Files
    Last edited by sjalle; 04-28-2005 at 12:40 PM.

  5. #5
    Join Date
    Apr 2005

    Wonderful 2 say d least...

    First things first, that app you gave me was quite good and very helpful.

    I might not end up doing it the same way ( probably cos I can't) but it has given me some very bright ideas for the system
    I'm currently thinking of creating a seperate class which implements the Runnable interface which reads the objects from a sockets and calls a static method in the class the creates the GUI to update the GUI, well something like that.

    Do you think that would work?

    Well I'll tell you if it does (or most probably when)

    Thanks for all the help, you are a life saver and at the end of this (my dissertation i.e.) we will swap code and compare notes.

    Thanks again


  6. #6
    Join Date
    Nov 2004

    Good luck !, but one final remark...

    I see what you mean, but I can't see your methods benefits, apart from
    the fact that it is of your own making. As for the static GUI method,
    well, then you will have to make all the display components (that your
    thread will access) static too. Static methods can be accesses via the
    class name, no initialization required, and thats it. If the reason for
    the "staticness" is to be able to access the gui from a class that has no
    pointer to the GUI then be warned; If you have more than one
    socket thread running then there is no guarantee that one thread, while
    its updating the gui
    , is halted, and one of the other threads are allowed to
    do a little gui updating, - messy messy.

    I might not end up doing it the same way ( probably cos I can't)
    Why not ?

    Anyway, how about this: You make your socket class like you have said,
    but also make your own thread extension like the code below, and make
    your socket class spawn smartThreads ..

     * A thread with luggage and vision
     * @author sjalle
     * @version 3.14
    public class SmartThread extends Thread {
      private StringBuffer content=null;
      private boolean loaded=false;
      private ThreadedGUI display=null;
      public SmartThread(Runnable r, String name, ThreadedGUI display) {
        super (r, name);
        // if you need to use other Thread constructors than the above,
        // just duplicate them in this class, and add the ThreadedGUI pointer
        // to the parameter list
      public void setContent (StringBuffer content) {
        if (this.content!=null && this.content.length() > 0) {
        } else {
      public ThreadedGUI getDisplay() {
        return this.display;
      // all SmartThreads that return true here will want to access the display
      public boolean hasLoaded() {
        return loaded;
      public StringBuffer getContent() {
        return content;
      public boolean equals (Object name) {
        return this.getName().equals(name);
    Last edited by sjalle; 04-29-2005 at 04:05 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.