Why is a GUI display stuck on the first actionperformed state


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Why is a GUI display stuck on the first actionperformed state

  1. #1
    Join Date
    Jul 2005
    Posts
    28

    Why is a GUI display stuck on the first actionperformed state

    Could somebody please suggest me how a GUI interface displays only the first and last program running states.

    The programe is running ok and the variables for the GUI display updating ok but it doesn't display it.

    When I press one button, it seems stuck on the pressed states and doesn't display how the program runs(I want it) and the pressed button is kept pressed state so I coudn't do any other button pressed action. It will keep like this until the last program running step and then it shows the last state ok.

    Thanks.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    The process you want to monitor has to be running in a separate and normal-priority
    thread. If you run in in the main program thread (the same as the GUI) then the
    program won't have any "spare time" to update the GUI.
    eschew obfuscation

  3. #3
    Join Date
    Jul 2005
    Posts
    28
    Thanks a lot, sjalle.

    My mind is a mess now and cannot figure out exactly how to change it.

    I made my program like this:

    main()
    {
    ....

    initialize GUI
    }

    OneThread()
    {
    ....
    updateGUI()
    }

    GUI()
    {
    ....
    wait user action;
    run, pause, resume and stop thread
    }

    The progarm would take most of the time in the thread.

    Could you please suggest me how I should change it. Thanks very much.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Just create and initiate the GUI as usual, no threading.
    Then make your GUI (frame) implements Runnable like
    Code:
    public class MyAppFrame extends JFrame implements Runnable {
      Thread myThread=null;
      boolean doSleep=false;
      boolean doStop=false;
    .
    .
      // the "Start" button event handler
      public void actionPerformed (ActionEvent ae) {
        myThread=new Thread(this);
        myThread.start();  
      }
      // the thread
      public void run() {
         // invoke the method that starts the processing
         myProcess();
      }
    }
    I assume your process is controlled by a loop. This loop must check the two
    flags, doSleep and doStop at each iteration and the flags must be controlled
    by two buttons, "Pause/Resume" and "Stop". When doSleep == true you
    put the thread to sleep for a second and keep on doing that until doSleep==false.
    When doStop==true the myProcess method shall return and the program
    control will leave the run method, and the thread will die.
    eschew obfuscation

  5. #5
    Join Date
    Jul 2005
    Posts
    28
    Thanks very much, sjalle.

    Now it could keep updating and the pause and stop action work.

    It seems that I can only do the thread operations after button pressed, otherwise, the program will be stuck. But I would need something like reset the system and also the GUI.

    The same with the "stop" action part I can only make it work when
    I use myThread.stop(); Otherwise it will be the same stuck.

    And also I will be appreciated if you could explain a bit why to make a GUI a thread will keep updating? (a high priority? or what?)

    Thanks a lot.


    Sorry I seem stuck on every step and need suggestions on every process.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Smile Don't be sorry

    Banging brick walls down with your forehead is just a part of the learning
    process

    You could use an approach like myThread.interrupt(). This action will be
    invoked by a button click on a reset, pause or stop button. Each of
    these eventshandlers could set a value: stopType, that could be
    0: reset, 1: pause, 2: stop. The main loop in your process could then be
    in a try/catch for InterruptedException. In the catch you could then check
    the value of stopType and perform accordingly.

    If your process was run in a max-priority thread then that would suck up
    (nearly) all the cpu-time and leave nada for the rest, like the GUI update
    that runs in the programs main thread.
    If your processing thread runs in normal priority, it could, after each GUI
    updating (like setting text on textComponents) issue a myTread.yield(),
    then that would ensure that the GUI was allowed to update.

    I have attached java file here that has a GUI that receives display data from
    multiple processes, these are simultaneusly running http requests. You may
    find something you can use there.

    If this doesn't work. then zip the code and post it. We'll get this done.
    Attached Files Attached Files
    Last edited by sjalle; 07-28-2005 at 03:47 AM.
    eschew obfuscation

  7. #7
    Join Date
    Jul 2005
    Posts
    28
    Thank you very much, sjalle. It's very kind of you.

    The thread.interrupt() helps it work. I've got those button actions work finally.

    I tried to use the try/catch in the process, (I think it makes everything clear)but it said there is no interruptException throw in the process. I used "if" to check the flags in every iterator and made them work. (looks very inefficient, though)

    I used the thread.stop() operation (though it is said Deprecated)

    Thanks again. It's good to have you here.

Similar Threads

  1. Gui Freeze after re-connect
    By lstwil in forum Java
    Replies: 0
    Last Post: 05-29-2005, 08:54 AM
  2. Java applet to VB
    By Maurice in forum Java
    Replies: 0
    Last Post: 02-11-2003, 01:10 AM
  3. Display XML element attributes in CSS or XSL
    By Allen Pitts in forum XML
    Replies: 0
    Last Post: 10-31-2002, 03:02 PM
  4. ASP+ session state management
    By Ariel Azia in forum ASP.NET
    Replies: 3
    Last Post: 08-02-2000, 01:10 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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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