Change the component weight dynamically in GridBagLayout
Asked Answered
C

1

6

Suppose now there are some components inside a JPanel and the layout is arranged using GridBagLayout. Is it possible to change the weight(weightx or weighty) of the components dynamically (e.g. after pressing a button)? Thank you.

Cluster answered 7/2, 2011 at 10:30 Comment(0)
D
7

Remove and add the component with a new GridBagConstraint. After that call

panel.revalidate();
panel.repaint();

Use the method to get current constraints public GridBagConstraints getConstraints(Component comp)

Davide answered 7/2, 2011 at 10:43 Comment(2)
You should be able to replace the remove and add with GridBagLayout.setConstraints.Brigandage
& Tom: Both of your solutions are fine. Thank you.Cluster

© 2022 - 2024 — McMap. All rights reserved.