• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Container.validate() not working for GridBagLayout

 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
});

}

}
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
You have to be odd to be #1 - Seuss. An odd little ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic