Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Container.validate() not working for GridBagLayout  RSS feed

 
sreenath reddy
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have a scenario where i build a panel using GridBagLayout . Later i remove certain components and wanted the components to be realigned so that empty gaps are vanished.

I tried using .validate() ,.updateUI() but its not working as expected.

Am i doing something wrong or is there any other altenate way of doing this ?

Here is my Code

public class Test
{

/**
* @param args
*/
public static void main (String[] args)
{
JDialog dlg = new JDialog();

dlg.setContentPane(new TestPanel());
dlg.setSize(400,400);
dlg.show();
}

}

class TestPanel extends JPanel
{
public TestPanel()
{
init();
}

void init()
{
setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
cons.weightx = 1;
cons.weighty = 1;
//cons.gridx = 0;
cons.gridy = 0;
cons.fill = GridBagConstraints.BOTH;

add(new JLabel("Test"), cons);
add(new JLabel("Test1"), cons);
//cons.gridx = 0;
cons.gridy = 1;
add(new JLabel("Test"), cons);
add(new JLabel("Test1"), cons);
cons.gridy = 2;
cons.weightx = 2;
JButton test = new JButton("Click to Repaint");
add(test,cons);
test.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
TestPanel.this.remove(1);
//TestPanel.this.invalidate();
TestPanel.this.validate();
TestPanel.this.updateUI();
}
});

}

}
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridBagLayout has methods you can use to investigate its state and to get and set the constraints of its child components.

edit: Changed erroneous comment.
[ April 10, 2007: Message edited by: Craig Wood ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!