1

I have a BorderLayout and I wish to include a BoxLayout in it such that I have three vertical buttons it . The design is like this: I want one horizontal borderlayout,below which I have two borderlayouts. In the left borderlayout I want boxlayout with three vertical buttons.

Here is the code I have tried:

    JLabel label2 = new JLabel("LOGO");
    pane.add(label2, BorderLayout.PAGE_START);

    button = new JButton("Button 2 (CENTER)");
    button.setPreferredSize(new Dimension(200, 100));
    pane.add(button, BorderLayout.CENTER);

    button = new JButton("Button 3 (LINE_START)");
    pane.add(button, BorderLayout.LINE_START);

    button = new JButton("Long-Named Button 4 (PAGE_END)");
    pane.add(button, BorderLayout.PAGE_END);

How should I go about such that I get the design mentioned. Right now I am not getting it and I do not know how to add boxlayout inside borderlayout.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
user2822187
  • 307
  • 2
  • 11
  • 26
  • 1
    *"Right now I am not getting it"* Can you provide an image of what you *are* getting? Other tips: 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) See [How do I create screenshots?](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) (for tips on making *great* screenshots). – Andrew Thompson Feb 25 '14 at 10:08
  • BTW - If the problem is the components are being stretched, a common fix is to wrap the `BoxLayout` into a `FlowLayout` befored adding that to the `BorderLayout`. The panel *using* the `FlowLayout` will be stretched, but the *content* will remain at the preferred size. – Andrew Thompson Feb 25 '14 at 10:11
  • 1
    `button.setPreferredSize(new Dimension(200, 100));` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). A good way to increase the size of buttons is to call `setMargin` or increase the `Font` size. Both best done in the PLAF. – Andrew Thompson Feb 25 '14 at 10:15

1 Answers1

0

You need to create a new JPanel with desired layout (BoxLayout) and add those two buttons to it and then add this newly created panel to your existing pane.

  JPanel pane1 = new JPanel(<Pass BoxLayout here>);
  pane1.add(<add button 1 here>);
  pane1.add(<add button 2 here>);

  pane.add(pane1, <Position>);

Hope this helps.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33