Re: (newbie) How to get the UI to update right away



mrstephengross wrote:
So here's my next swing question: I've got a combo box with an action
listener. When you make a selection from the combo box, the action
listener fires off and does a bunch of stuff that takes a few seconds
to execute.

However! While the action listener is executing, the combo box does
NOT collapse. That is, I would expect it to first collapse (with the
new selection highlighted) and THEN fully executed the action
listener's code.

Is there a way to do this? I've been trying out various
implementations of the action listener to get it work, such as:

public void actionPerformed(ActionEvent e)
{ comboBox.setPopupVisible(false); /* do other stuff */; }
public void actionPerformed(ActionEvent e) { comboBox.validate(); /
* do other stuff */; }
public void actionPerformed(ActionEvent e)
{ applicationFrame.validate(); /* do other stuff */; }

But nothing seems to work. I've even tried the:
SwingUtilities.invokeLater(new Runnable() { comboBox.validate(); /
*...*/ });

trick, to no avail. Any ideas? Or am I just misunderstanding the UI's
built-in behavior?

Thanks,
--Steve

You are blocking the Event Dispatch Thread. ActionListener (and most other listeners) perform their event handling on the EDT. So while your code is off for a few seconds it is preventing your GUI from updating. Just wrap your time consuming code in a Runnable and start another thread.

public void actionPeformed(ActionEvent ae) {
Runnable r = new Runnable() {
public void run() {
// time consuming task
}
};
new Thread(r).start();
}

If you have to update the GUI with the results of your time consuming task you may or may not have to wrap that code in EventQueue.invokeLater() to get it to execute on the EDT. Most Swing methods are not thread safe and need to be executed on the EDT. Check the docs for which are which.

--

Knute Johnson
email s/nospam/knute2008/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
.



Relevant Pages

  • Re: (newbie) How to get the UI to update right away
    ... When you make a selection from the combo box, ... listener fires off and does a bunch of stuff that takes a few seconds ... public void actionPerformed{comboBox.validate; ... If you have to update the GUI with the results of your time consuming task you may or may not have to wrap that code in EventQueue.invokeLaterto get it to execute on the EDT. ...
    (comp.lang.java.gui)
  • Re: (newbie) How to get the UI to update right away
    ... When you make a selection from the combo box, ... listener fires off and does a bunch of stuff that takes a few seconds ... public void actionPerformed{comboBox.validate; ... If you have to update the GUI with the results of your time consuming task you may or may not have to wrap that code in EventQueue.invokeLaterto get it to execute on the EDT. ...
    (comp.lang.java.gui)
  • (newbie) How to get the UI to update right away
    ... When you make a selection from the combo box, ... listener fires off and does a bunch of stuff that takes a few seconds ... to execute. ... public void actionPerformed{comboBox.validate; ...
    (comp.lang.java.gui)
  • Re: (newbie) How to get the UI to update right away
    ... When you make a selection from the combo box, ... listener fires off and does a bunch of stuff that takes a few seconds ... to execute. ... I would expect it to first collapse (with the ...
    (comp.lang.java.gui)
  • Re: Yet another generics question: Needs unchecked conversion to conform to ...
    ... This is a surprisingly common generics question, and the answer, incidentally, is "no". ... Consider a pipeline of some kind in which objects are put into a Pipeline object and Pipelines notify their Listeners with these objects, allowing loose coupling and runtime rearrangement of the plumbing. ... public void handleObject; ... public void addListener { ...
    (comp.lang.java.programmer)