Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3732

Re: (newbie) How to get t

Path csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!news.glorb.com!news-out.readnews.com!transit3.readnews.com!news-out.news.tds.net!newsreading01.news.tds.net!86597e80!not-for-mail
From "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this>
Subject Re: (newbie) How to get t
Message-ID <Q6idndodCMXs1_jVnZ2dneKdnZzinZ2d@bt.com> (permalink)
X-Comment-To comp.lang.java.gui
Newsgroups comp.lang.java.gui
In-Reply-To <4865092d$0$4054$b9f67a60@news.newsdemon.com>
References <4865092d$0$4054$b9f67a60@news.newsdemon.com>
Content-Type text/plain; charset=IBM437
Content-Transfer-Encoding 8bit
X-Gateway time.synchro.net [Synchronet 3.15a-Win32 NewsLink 1.92]
Lines 64
Date Wed, 27 Apr 2011 15:46:36 GMT
NNTP-Posting-Host 96.60.20.240
X-Complaints-To news@tds.net
X-Trace newsreading01.news.tds.net 1303919196 96.60.20.240 (Wed, 27 Apr 2011 10:46:36 CDT)
NNTP-Posting-Date Wed, 27 Apr 2011 10:46:36 CDT
Organization TDS.net
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.gui:3732

Show key headers only | View raw


  To: comp.lang.java.gui
Knute Johnson wrote:
> 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.
> 

The SwingWorker class is well worth using IMO.


-- 
RGB

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | Next | Find similar | Unroll thread


Thread

Re: (newbie) How to get t "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000

csiph-web