Results 1 to 4 of 4

Thread: Problem refreshing element of JAVA swing

  1. #1
    Join Date
    Aug 2009
    Posts
    63

    Problem refreshing element of JAVA swing

    I'm coding an interface Java swing and I'm facing a problem of refresh of an element, here's the relevant code:

    A button swing associated with a method:
    Code:
    validateButton.addActionListener(new java.awt.event.ActionListener () {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
     validateButtonPA ();
                }
            });
    This is the method:
    Code:
    private void validateButtonPA (){
    SwingUtilities.invokeLater(new Runnable () {
              public void run() {
                informationArea.setText("Search the Naming Service is in progress." );
              }
            });
    ..................(rest of processing code) ..........................
    }
    The problem is that with or without invokeLater the setText takes effect only after the processing code that follows. I'm trying to add repaint, revalidate, .... nothing works.

    I do not understand what's wrong, I tried to follow an online tutorial related to threads but it does not seem to work as described.

    If someone sees what's wrong, I thank you for the report

  2. #2
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Problem refreshing element of JAVA swing

    Apparently you've never heard of graphic thread and you do not know what is InvokeLater.

    So it's simple, when you click on the button, the Swing thread that handles drawing the windows and react to your method calls validateButtonPA (). In this method, you can change the GUI and start treatment. So no need to InvokeLater here since we are already in the Swing thread.

    Then you have a large processing code. The treatment itself, you can not do so in the thread if your Swing GUI freeze since, instead of drawing your windows, Swing will be busy doing your calculations (this is the problem that you observe). You must then start the treatment in a new thread, and at the end of it, you call a function using InvokeLater to update your GUI using Swing thread.

    At the code level it gives something like this:
    Code:
    private void validateButtonPA(){
       informationArea.setText("Search the Naming Service is in progress." );
       new Thread() {
          public void run() {
             // Heavy treatment 
             // ...
             SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                   informationArea.setText("Search the Naming Service is completed." );
                }
             });
          }
       }.start();
    }

  3. #3
    Join Date
    Aug 2009
    Posts
    63

    Re: Problem refreshing element of JAVA swing

    In fact I know what the EDT is, but a mistake on my part is an explanation of the tutorial made me think that the actionPerformed executed in a thread dedicated to listening.

    I used the class SwingWorker finally, and it works, but there is something that I do not understand:

    If I'm in the EDT:

    Code:
    informationArea.setText("Search the Naming Service is completed." );
    informationArea.repaint()
    //Heavy treatment //
    The treatment is heavy demand after updating interface, so why the EDT does it not update the interface before working on processing heavy?

  4. #4
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Problem refreshing element of JAVA swing

    Because the EDT is the work of repainting the window at the end of the performance of your block event processing. For the sake of optimization, it does not start from procedure to repaint every time you change a property of the GUI, as in this case the text of a Label. When you change the text of the Label or even when you call repaint (), you're just asking a repaint later. Swing works like a queue, waiting so late in your code to start the next drawing operation is also why invokeLater () is called invokeLater () and not invokeNow (). That is also why you should never block the EDT thread, it must turn constantly to the GUI responds correctly.

Similar Threads

  1. Print in java using Swing
    By Sheenas in forum Software Development
    Replies: 3
    Last Post: 18-11-2009, 09:50 AM
  2. Java Swing and MySQL
    By shelton141 in forum Software Development
    Replies: 1
    Last Post: 22-09-2009, 08:50 AM
  3. drawPolygon in java Swing?
    By JagdishP in forum Software Development
    Replies: 3
    Last Post: 07-08-2009, 11:33 PM
  4. Message Box example for Java Swing
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 31-07-2009, 05:30 PM
  5. Java swing help with repaint()
    By grudge in forum Software Development
    Replies: 4
    Last Post: 23-07-2009, 03:41 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,577,835.25263 seconds with 16 queries