• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Swing, Adding components to a jpanel on the fly

Hello

I have a loop in a class extending JPanel , i need components to be added to the panel right away, not after the loop finishes

....
while(true) {
    ...
    ...heavy code...
    ....
     this.add( new JLabel("im want to show up right away. inside this nice loop.") )
     this.revalidate();
}

every addition in a new thread? help needed thank you
0
inudaci
Asked:
inudaci
  • 4
  • 2
1 Solution
 
CEHJCommented:
You need to add components on the event dispatch thread. Call EventQueue.invokeAndWait from a non-gui thread
0
 
inudaciAuthor Commented:
This seems to be the case, thanks, but...
..how do i from my listener in the gui thread, jump to another thread, so im allowed to call the EventQueue.invokeAndWait ?
0
 
CEHJCommented:
The best way would be to not do this intensive processing in the gui thread in the first place. Then you're free to call it
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
You should be using SwingWorker, its designed to handle exactly this situation.
Its part of 1.6 or can be downloaded for earlier verwsions

https://swingworker.dev.java.net/
0
 
CEHJCommented:
>>its designed to handle exactly this situation.

It isn't actually, it's designed for single invocations.

What you need to do is simply to create a new Thread for your processing and call EventQueue.invokeLater from it.
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
> It isn't actually, it's designed for single invocations.

rotfl, it can be used for as many invocations as you like
If you're going to mislead people, at least provide some facts to back up ridiculous claims.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now