3

I'm doing a prototype for showing some basic functions of a painting program. Now, here is how the window looks like:

enter image description here

The window is compound of a panel with a vertical BoxLayout, inside it, there are a custom image control and another panel with a FlowLayout containing all the buttons.

The problem is that I have to specify the height for the buttons panel (in 100px). If I delete that line, the window looks like this:

enter image description here

But, if I only specify the width, and I write a 0 for the height, I get this:

enter image description here

I want the layouts determine the correct height of the buttons panel by themselves. Here is the code that produces the first image:

public void createAndShowGui(){
    JFrame.setDefaultLookAndFeelDecorated(true);

    setTitle("SimplePaint");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);

    //Main container layout.
    BoxLayout mainLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
    getContentPane().setLayout(mainLayout);

    drawingPanel = new JPanel();
    drawingPanel.setLayout(null);
    drawingPanel.setPreferredSize(new Dimension(width, height));

    TextBox editor = new TextBox();     
    drawingPanel.add(editor);
    editor.setVisible(false);

    canvasControl = new DrawableCanvas(editor);
    drawingPanel.add(canvasControl);
    canvasControl.setBounds(0, 0, width, height);

    getContentPane().add(drawingPanel);

    drawingSurface = canvasControl.getDrawingSurface();

    //Buttons layout.
    FlowLayout buttonsLayout = new FlowLayout();
    JPanel buttonsPanel = new JPanel(buttonsLayout);
    buttonsPanel.setPreferredSize(new Dimension(width, 100)); // <-- Not in this way

    //Buttons creation...

    pack();
}

Thanks in advance.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alex Text
  • 269
  • 1
  • 4
  • 11
  • you may try adding several panels with flowlayout with max four buttons each and add it to the buttonsPanel with gridlayout –  May 06 '14 at 11:58
  • I think the GUI would work better with the buttons in a tool bar, and the image/custom painting centered in a `GridBagLayout` in a scroll pane. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson May 06 '14 at 12:01
  • Yes, that's right. In reality, the problem can be reduced to this: I don't know how I can make the FlowLayout to grow vertically (not horizontally) just enough for showing all the buttons. I'm a .NET developer and this is the first time I have to use layouts in Java. In .NET the problem is solved using a StackPanel vertical specifying only the width. After that, it's only necessary to add the image control, and another StackPanel horizontal containing the buttons, and voilà, the buttons are shown line after line (not all in one single line making wider the whole window). – Alex Text May 06 '14 at 13:46
  • Tip: Add @Arvind (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson May 07 '14 at 06:54
  • Yes, I tried to do it with you, Andrew Thompson, but I couldn't (it didn't show me your user as a link, so I supposed it didn't work). Anyway, thanks for the advice. – Alex Text May 07 '14 at 09:44

1 Answers1

10

don't know how I can make the FlowLayout to grow vertically (not horizontally) just enough for showing all the buttons

FlowLayout does not do this.

You can use the Wrap Layout, which extends FlowLayout and override the preferred size method so that the component can wrap automatically.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I just tried it, but I have the same problem: If I use WrapLayout instead FlowLayout, and I delete `buttonsPanel.setPreferredSize(new Dimension(width, 100))`, I have the second case again. Maybe my problem is that I'm only specifying the size of the drawing panel and the size of the control it contains, but no more, and I'm expecting it would act as the limit of the width of the window. I never specify the width of the window, so, of course, FlowLayout grows horizontally because it doesn't have horizontal limit. But, how can I specify only the width of the window without the height? – Alex Text May 07 '14 at 11:33
  • @AlexText, did you try my suggest about the `setSize(...)` method? – camickr May 07 '14 at 15:41
  • No, I didn't try it because I saw the method signature (specially the `Container` parameter) and I thought it would take the height of that parameter into account anyway. But this second time I checked the implementation of the `layoutSize(...)` and yes, it was what I was looking for. Thanks a lot. – Alex Text May 07 '14 at 16:35