1

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.

I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.

Here is the code Netbeans created: http://pastebin.com/ERwY4rBC It's not that the code is completely unusable but I wanted to try it manually.

The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?

Thanks!

Cw39
  • 45
  • 1
  • 6
  • The better way is to hand code and to use the more coder-friendly layout managers, often in nested JPanels. For example, please have a look at [this answer to a similar question](http://stackoverflow.com/a/7852158/522444). – Hovercraft Full Of Eels Jul 22 '12 at 16:55
  • What exactly do you mean by auto-resize? Resize when window is resized? – Mohayemin Jul 22 '12 at 16:57
  • As 'Hovercraft' says, hand-coded use of layout managers is probably the best choice. I find BorderLayout to be very flexible - you could put a panel with the 'left side' content in the center of a BorderLayout, and the 'right side' in the East. For more control, look at MigLayout. Stay away from GridLayout - it's too complex for most uses. – GreyBeardedGeek Jul 22 '12 at 17:00
  • 1
    A better linked example: [EastProgressList](http://stackoverflow.com/a/6802870/522444). @GreyBearded: I think you mean "GridBagLayout", but even it is easier to hand code than GroupLayout. – Hovercraft Full Of Eels Jul 22 '12 at 17:00
  • An example of using [GridBagLayout](http://stackoverflow.com/a/9852059/522444). – Hovercraft Full Of Eels Jul 22 '12 at 17:06
  • @Mohayemin: this is what I wanted: http://i48.tinypic.com/ern0xv.png The JTextField and JTree shall resize horizontally when I resize the window. Otherwise I'd get an ugly whitespace between the buttons and the JTree. – Cw39 Jul 22 '12 at 18:08

2 Answers2

3

So, using GroupLayout would be not "Java like" or how do I have to understand it

GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.

What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts'. If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.

Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.

Community
  • 1
  • 1
Robin
  • 36,233
  • 5
  • 47
  • 99
  • This is exactly what I was looking for. Thanks! :) But is there any way to set a minimum width to the left side? I tried it with my JPanel on the left side to set a minimum size using "setMinimumSize(new Dimension(120, 0));" but it didn't work. – Cw39 Jul 22 '12 at 18:27
  • OK, fixed it. :) I read it's not possible to set the size of the CENTER element but the size of the side elements with setPreferredSize(dimension). – Cw39 Jul 22 '12 at 18:50
3

Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.

There is a WebStart application on their site that demos different layout situations with code samples provided.

01es
  • 5,362
  • 1
  • 31
  • 40