@hellbinder and others: borderlayout validate problem
Hello hellbinder!
You wrote under this topic
http://forum.java.sun.com/thread.jspa?threadID=5114605
Personally I try avoiding BorderLayout because I've experienced many problems with that when the content of the container is revalidated periodically (e.g. with Timer).
But for applicaions without that (revalidating) BorderLayout with nested panels is ok.
I wonder if I am having such a problem. Take a look at this topic (http://forum.java.sun.com/thread.jspa?threadID=5114687) and get the Dukes! Thank you in advance!
Annette
Message was edited by:
Annette
[623 byte] By [
Annettea] at [2007-11-26 12:25:59]

# 1
I've not read through all that lot yet but the one thing I can say before I do is that BorderLayout is, by a long way, the layout manager I use most frequently, and I've never had a problem with it revalidating.
# 2
And the problem's fairly obvious...
Try it and add just one or more TextBlocks and remove then. If the last one is removed, validate does not paint the empty panel
Indeed. That is as designed: The validate() method does not explicitly repaint, it simply ensures the layout is valid. You want to be calling repaint() immediately after it, or replace the validate() call with revalidate().
If you want a visual example of what's going on behind the scenes, run your app, add a single text block, shrink the window vertically so that the vertical scroll bar appears, and then remove the block. The repaint occurs.
It is simply that if the recalculation of the layout causes a change to the scroll bar model, then an event will be fired that will cause a repaint of the scroll pane. This is, effectively, a 'chance' occurrence - the repaint is called by the scroll pane due to a separate change; without a repaint() call you are not doing anything to invoke it explicitly.
# 3
OK itchy. There's a sample. Check out what happens. Sometimes it comes out when repainting and sometimes simillar effect when combined with revalidating. After changing to eg. BoxLayout problem does not exist
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
public class Test extends JFrame implements ActionListener{
private JLabel label;
private Timer timer;
private PaintPanel ppanel;
public Test() {
label = new JLabel("",0);
ppanel = new PaintPanel();
timer = new Timer(1000,this);
getContentPane().add(label,BorderLayout.NORTH);
getContentPane().add(ppanel,BorderLayout.CENTER);
pack();
setDefaultCloseOperation(3);
setVisible(true);
timer.start();
}
public void actionPerformed(ActionEvent e){
label.setText(new Date(System.currentTimeMillis()).toString());
ppanel.setValues(400,200);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Test();
}
});
}
}
class PaintPanel extends JPanel {
private int index=0;
private int value=0;
public PaintPanel(){
setPreferredSize(new Dimension(400,400));
}
public void setValues(int i,int val){
index = i;
value = val;
repaint();
}
public void paintComponent(Graphics g){
for (int i=0;i<index;i++){
g.drawRect(i,value,1,1);
}
}
}
>
# 4
@AnetteThis whole business is not about the dukes. Keep them for other issues :)
# 5
Your new example is completely different from the old one.
The issue you have there is nothing to do with layouts. You're not removing anything or validating anything so I've no idea what this has to do with your original question.
In any case, the problem I see when running that code is that things are painted in the wrong place. This is easily fixed by adding this line (which should always be there) at the start of your paintComponent() method:
super.paintComponent(g);
# 6
Thank's itchy!
That does the trick. :)
And the issue indeed didn't have relation to this
but I merely wanted to show you the case of wrong repainting() I had.
BTW Do You know why BoxLayout paints everyting ok even without super.paintComponent(g)
# 7
Acheeee! Sorry, I need my sight tested, I thought that second lump of code was from Annette :o)Whoops.Anyway, no, off the top of my head I couldn't say why BoxLayout behaves differently.