I have a JPanel with a GridBagLayout. And I would like to give the user the possibility to switch two components. I tried it like that, but it doesn't work, what is wron开发者_运维问答g?
public void switchSites( boolean b )
{
this.remove( blueSite );
this.remove( whiteSite );
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.CENTER;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 3;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1.0;
c.weighty = 1.0;
if( b )
{
this.add( whiteSite, c );
c.gridx = 2;
this.add( blueSite, c );
}
else
{
this.add( blueSite, c );
c.gridx = 2;
this.add( whiteSite, c );
}
this.repaint();
this.validate();
}
Call
invalidate();
validate();
repaint();
Or it's better to use CardLayout and a subpanel to switch them.
Regards, Stas
I have two suggestions that may solve your problem.
I believe that you are meaning to call this.revalidate();
.
If that does not work, trying calling this.doLayout();
directly.
精彩评论