Hello,
I am starting Java recently and I am learning, so if someone can help
me with the following would be great.
I have a JFrame, which has gridlayout. One of the grid has 2 check
boxes. What I want is, if a check box is clicked, I want to expand the
JFrame dynamically and add another JPanel to the GridLayout at the
bottom and when the check box is unchecked, I want the JFrame to go
back to the original state (with out the added JPanel). How can I do
it? I want the transition of the panel adding and removing smooth.
Is there a way to do it?
Thanks.
Homer - 24 Apr 2006 20:47 GMT
I am not %100 sure about this (I should test it) but what you can do
is:
- define your grid with reserved size for your JPanel.
- By clicking CheckBox Add JPanel object to the Grid.
- Update JFrame to be resized. (setPreferdSize, validate?)
Or try setVisible() for your JPanel.
Is this in SetSwing Example coming with JDK?
Josh Falter - 25 Apr 2006 14:41 GMT
The JFrame can change size during execution using the setSize()
function.
There are a few routes you can go with dynamically adding the JPanel to
your JFrame.
I would not recommend adding it using GridLayout though, which does not
respect the sizes you request.
GridLayout is great for keeping components like JCheckBoxes or JButtons
organized, but is not good for adding containers that probably dont
need to be the same size as other components such as a JPanel, although
you may be envisioning adding a smaller JPanel than what I am
picturing.
After changing the contents of the JFrame, you can update it using
invalidate()
validate()
repaint()