tags:

views:

65

answers:

2

Hello,

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 wrong?

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();

}
+1  A: 

Call

invalidate();
validate();
repaint();

Or it's better to use CardLayout and a subpanel to switch them.

Regards, Stas

StanislavL
can you specify reasons for same ?
YoK
Sorry, this doesn't work. And no I need handy features from the GridBagLayout. Maybe I could do it with CardLayout, too. But I don't know.
Christian
@Christian: Each panel in the `CardLayout` can have it's own layout.
trashgod
A: 

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.

Zéychin