简体   繁体   中英

JFrame resize to visible components

I have an application where I want that the user is able to choose between normal and advanced settings. Now if the user checks a JCheckBox and the advanced settings should disapper the problem starts. My idea was to set all unnessecary swing components (JScrollPane, JLabel...) invisible and then find a method of JFrame which fits the window to the VISIBLE components. My question is if there is such a method?

... and then find a method of JFrame which fits the window to the VISIBLE components. My question is if there is such a method?

Yes, there is such a method, and it is called pack() .

This will cascade through the layout managers of all the containers held by the top-level window, asking them to re-lay out their visible components, resizing components to their preferred sizes as based on the components and the layout manager requirements, and eventually resizes the top-level window to fit the containers and their components.

1. You can use setVisible(boolean b) , to make the component visible and invisible.

2. You can check that if the component is visible or not using isVisible()

3. You can then use the pack() method, pack() method gives sets the frame size as per need

I think you can wrap the advanced content in a panel (if possible) and remove that panel from frame using this.remove(component) then use this.pack()
and you can do the opposite on showing them, this.add(...) then this.pack() again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM