views:

299

answers:

2

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?

+1  A: 

How about the pack method ???

Update:

Now I understand what you are trying to do. I am sorry to disappoint you but 90% of standalone applications are written with

  1. Dialog windows that have a fixed size (non-resizable) and a perfectly tuned to content

  2. Main Windows that are resizable but have a minimum size (e.g. 800x600)

What you want (a resizable window with no hard-coded values) is possible but very difficult to achieve (this is the other 10% of applications)

Since Java Swing has notorious difficulties with layout (this is the main reason of the existance of so many Layout managers) I suggest you follow the easy way.

You can play with the setPrefferedSize and setMinimum size methods of various component but it will be hard to get it perfect.

kazanaki
That *sets* the size, but it does nothing to keep the user from resizing.
Daniel Straight
+2  A: 

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.

Software Monkey
@SM: Off topic: Where have you been Software Monkey? I haven't see you around for a while. Is nice to see you again. Bye bye!
OscarRyz
@Oscar: I've been around, checking SO a couple times a week, throwing out and answer here and there where I think I can help. But mostly, I'm putting in long hours on a product deadline - the usual: X amount of work and X/10 time to do it.
Software Monkey