I am testing out MigLayout for a project, and I can’t seem to figure out the MigLayout way of controlling the size of the whole panel which is being layed out. I am adapting the example in the MigLayout whitepaper. Also, I am writing in Python and using Jython rather than writing in Java. That said, here is my current code.
layout = MigLayout("fillx",
# Column constraints
"[right]rel[grow,fill]",
# Row constraints
"[]10[]10[]")
panel = JPanel(layout)
panel.add(JLabel("Enter size:"),
"")
panel.add(JTextField(""),
"wrap, width 150:250")
panel.add(JLabel("Enter weight:"),
"")
panel.add(JTextField(""),
"wrap, width 150:250")
panel.add(JButton("Ok"),
"span 2, align center, width 100:150")
The panel then goes into a JFrame for display. When I resize the frame, the controls resize nicely and obey their minimum sizes. However, there is no minimum size on the frame, nor does there seem to be any way to get to the real minimum size of the panel to set it for the frame. Asking for the panel’s minimum size returns a size which, when set on the frame (plus insets), cuts off the button and half of the text fields.
What I want is to be able to set the minimum size for the frame (not hard-coded!) so that the panel fits and none of its controls are cut off. What is the best way to do this?
Personally, I wouldn’t stress too much about the minimum size as long as the preferred size (what you get after pack() is appropriate. If for some reason the user wants to reduce the window down to a size where the components in it get clipped, then let them. Maybe they just want to get the bottom of your window out the way so they can see what remains and something else on their monitor at the same time.
I would argue that the true minimum size of any window is the sum of it’s border and title bar and room for a few characters of the title text and an ellipsis. If the user want something smaller than what will allow all the components to show unclipped who are you to enforce otherwise?
Using any of the several table-based layout managers available on the web, like MIG Layout, you should be able to easily get the resizing behavior you want.
Shameless self plug: If you find MIG Layout hard to use, my MatrixLayout is, I think, easier to use. But MIG layout is more powerful. I deliberately chose to code MatrixLayout to not inhibit panels from reducing below their calculated minimum size.