Reputation: 6362
I have to force my program to validate and repaint my gui (or part of it) the moment it adds a new component to it. There must be absolutley nothing done until the component is painted on the screen, then the code can continue.
I'm creating a layered JPanel structure. A series of JPanels that each continas a series of JPanels that each contains a series of JPanels.
As I create 3rd level JPanels, I need to take data from 1st level JPanels which aren't drawn yet. That causes them to return wrong data and mess up the whole setup.
1st level JPanels only get painted after the master for loop (one which goes trough 1st level JPanels) is completed.
How can I force my program to draw it as soon as it's created and added to its parent? Everything here is done inside EDT, I checked. Repaint and validate didn't work at all.
EDIT: Okay, I used Robins solution.
However, is there a way to force instant GUI updates, as soon as the line in the code gets executed (oposed to aiting for loop to finins and similar stuff). It would be so much easier to debug some thing if that would be possible. Even if insta-update means pausing all the other threads.
Upvotes: 1
Views: 156
Reputation: 51565
You should have a GUI data model that contains the information you want to put on the panels.
You would generate the information, then create your GUI components.
Upvotes: 1
Reputation: 36621
As I create 3rd level JPanels, I need to take data from 1st level JPanels which aren't drawn yet. That causes them to return wrong data and mess up the whole setup.
I would first suggest to change this setup to something more safe. Having to rely on paint order of your UI elements sounds like a good source for nasty problems.
1st level JPanels only get painted after the master for loop (one which goes trough 1st level JPanels) is completed.
How can I force my program to draw it as soon as it's created and added to its parent? Everything here is done inside EDT, I checked. Repaint and validate didn't work at all.
It does not work because you are blocking the EDT. Calling repaint
and/or validate
does not actually triggers a repaint. Instead, it schedules one. You could attempt to break up your master loop in several separate Runnable
s which you each schedule on the EDT. That might allow the scheduled Runnable
by the repaint
call to slip in between. However, there is no guarantee for that.
Upvotes: 3