Is there any thing I can do to make my JPanel pack
like a JFrame, or do something like that. I want to do this instead of giving it dimensions. Please comment if any addition info is needed. Thanks.
Please try JPanel.updateUI and let me know if that helps.
You should make GUI calls in the Event Dispatcher Thread:
EventQueue.invokeLater(() -> {
someJPanel.updateUI();
});
updateUI
has no effect on resizing a JPanel. –
Kibe If I understand you correctly, your JPanel is not being automatically (re)sized, correct? In that case, you can use Component.validate (or JComponent.revalidate())
My JPanel does originally fit its components
- which is why you should be using a layout manager. If you add components to a visible GUI then you simple revalidate() the panel. Post your SSCCE that demonstrates your problem. You were asked to do this as well in your last question. –
Blancablanch Let me explain my use case, so that my answer will make sense.
I have a game board G on which I am absolutely positioning tiles Ts and some other property panels say P, Q and R.
Only the Game board G doesn't use a layout. However, for all the tiles and other panels, I do want to use layout management. For the
tiles, I want to calculate and set both position and size based on how many tiles are there. So, the setBounds
method works great.
The problem is with the other panels like P where I know the x and y where I want to place them, however, I would like it if they figured out their own preferred sizes. This becomes a problem because setBounds
insists on setting both position and size, and setLocation
doesn't seem to work.
The solution which worked for me is putting a slightly oversized panel Outer-P and setting opaque
to false
and then placing P inside it. Note that Outer-P has a layout manager which allows P to be its preferred size. Personally, I used new MigLayout("insets 0","[]","[]")
which lets the child take its preferred width and height.
As far as the user is concerned, P is in the top left, and looks pack
ed. The wrapper panel is invisible.
© 2022 - 2024 — McMap. All rights reserved.
pack
uses the preferred size of the frames content to determine the best size for the frame...so, you need to have the size of the components in order to make it work. The best choice is to use appropriate layout managers...see Laying Out Components Within a Container – Calicutnull
layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. Why is it frowned upon to use a null layout in SWING? – CalicutGridBagLayout
or maybe evenMigLayout
– CalicutWhat about a GroupLayout
- We have no idea what you requirement is so how do you expect us to answer that? The GroupLayout may be the most complicated to learn. Also, there is no reason to use a single layout manager since you can nest panels with different layout. – Blancablanch