Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2006

Tip: Looking for answers? Try searching our database.

How java handle GUI updates

Thread view: 
si - 23 Mar 2006 16:10 GMT
Hello

I'm experienced c++ programmer making first steps in java.
Does swing (or awt) has some automatic GUI update engine.

I'm thinking about something similar to ON_COMMAND_COMMAND_UI handlers
(vc++) which get fired every time the application is idle.

Thanks
si
Oliver Wong - 23 Mar 2006 18:45 GMT
> Hello
>
[quoted text clipped - 3 lines]
> I'm thinking about something similar to ON_COMMAND_COMMAND_UI handlers
> (vc++) which get fired every time the application is idle.

   I have no idea what ON_COMMAND_COMMAND_UI does, but the way Swing for
GUI updating works is that every component as a paint() method which takes
care of painting that component (and recursively painting all of its
children components). Most of the item, the Swing API will take care of
calling paint() when it is needed.

   You can also create objects that implement various Listener interfaces
(e.g. DragGestureListener, DropTargetListener, FocusListener, etc.). When
the appropriate event occurs, methods on your objects which implement the
listener interfaces are called (akin to the callback mechanism in C++, I
think), where you can do whatever processing you need to do, then call the
repaint() method to indicate that Swing should, "at some point in the
future", schedule a paint of the GUI to reflect any changes that might have
occured.

   Usually, when the application is idle (as in no events are occuring),
there is no need to update the GUI. If you want to make periodic updates
(e.g. every 0.5 seconds), you can create a Timer object, which triggers
events.

   - Oliver
Monique Y. Mudama - 23 Mar 2006 19:02 GMT
>     Usually, when the application is idle (as in no events are
>     occuring), there is no need to update the GUI. If you want to
>     make periodic updates (e.g. every 0.5 seconds), you can create a
>     Timer object, which triggers events.

You can also call repaint() explicitly if something outside of the GUI
world requires an update (ie, new data has arrived and needs to be
represented).

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

si - 23 Mar 2006 20:30 GMT
>>    Usually, when the application is idle (as in no events are
>>    occuring), there is no need to update the GUI. If you want to
[quoted text clipped - 4 lines]
> world requires an update (ie, new data has arrived and needs to be
> represented).

I try to describe what I want to achive. Let's say I have a boolean
variable that specifies whether one of the toolbar buttons should be
enabled or disabled.
I can change value of this variable in any place and any time.
I would like the toolbar to check the state of the vairable periodically
and enable/disable appropriate button.

I understand I need to write a timer that will do that for me or
enable/disable button in the toolbar's paint method. Is that correct?

si
Monique Y. Mudama - 23 Mar 2006 20:45 GMT
> I try to describe what I want to achive. Let's say I have a boolean
> variable that specifies whether one of the toolbar buttons should be
[quoted text clipped - 5 lines]
> enable/disable button in the toolbar's paint method. Is that
> correct?

I think a much better approach would be this:

Instead of allowing direct access to the variable, create a method
that sets the variable.

Within the method, call repaint() for the toolbar (you may also have
to call invalidate() and revalidate() ... I can never quite remember
when you need which).

Or you could set up some sort of Listener interface (PropertyListener
comes to mind), and you could fire an event within the setter method I
mentioned.  The toolbar could be one of the listeners to that event.

I'm having a hard time imagining a situation in which the polling you
describe is the best approach.

Signature

monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html

Oliver Wong - 23 Mar 2006 21:05 GMT
>>>    Usually, when the application is idle (as in no events are
>>>    occuring), there is no need to update the GUI. If you want to
[quoted text clipped - 14 lines]
> I understand I need to write a timer that will do that for me or
> enable/disable button in the toolbar's paint method. Is that correct?

   You would create a new class which subclasses the AbstractAction class.
This AbstractAction already provides an implementation for the
setEnabled(boolean) method. You only need to supply an implementation for
the run() method, which is called whenever the action should be executed.

   Then, you take create an instance of the action, and pass it to a
JButton. The JButton will query the Action to find out what its label,
icons, etc. should be ,and render itself appropriately. When you wish to
disable the button, you call setEnabled(false) on the action (not on the
button), and the button will be notified automatically, and grey itself out.

   The advantage of this approach is that you can share the action across
multiple components. For example, you could add your same instance of Action
to a toolbar button, a drop down menu, and a normal click-able button, and
just called setEnabled(false) once on the action, and all three controls
will disable themselves in the appropriate manner.

   - Oliver
Thomas Hawtin - 23 Mar 2006 22:45 GMT
> I try to describe what I want to achive. Let's say I have a boolean
> variable that specifies whether one of the toolbar buttons should be
> enabled or disabled.
> I can change value of this variable in any place and any time.
> I would like the toolbar to check the state of the vairable periodically
> and enable/disable appropriate button.

Polling is rarely a good idea. A better approach is to encapsulate the
variable. When a method that changes it is invoked, it should also have
the states of the toolbar buttons updated.

If you have more than one thread (the one thread you will have is the
AWT Event Dispatch Thread (EDT), which all repaint and events from the
user are fired on), then you will need to wrap any changes to the Swing
GUI in invokeLater. So:

public class MyClass {
    private final AbstractButton button;
    private boolean myVariable;
    public MyClass(AbstractButton button) {
        this.button = button;
    }
    public synchronized boolean isSet() {
        return myVariable;
    }
    public void set(boolean myVariable) {
        synchronized (this) {
            if (this.myVariable == myVariable) {
                return;
           }
           this.myVariable = myVariable;
        }
        java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    assert java.awt.EventQueue.isDispatchThread();
                    // NB: isSet is synchronized.
                    button.setEnabled(isSet());
                }
        });
    }
}

(but use better identifier names)

You can go a little more sophisticated. For instance, if a thread
rapidly switches the flag, then loads of events will be queued up making
the program very slow even if it doesn't run out of memory.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.