1

Here's what I actually want to put on a panel:

First logical block:

radio button 1       text field     icon button

radio button 2       text field     icon button

check box

Second logical block:

Label       Spinner

        Button

My first decision is to make Vertical Box Layout and put there two Horizontal Box Layouts - for each logical block. But the problem is with these blocks, what layouts to choose to describe this structure? I dislike GridBagLayout - it is very composite and difficult to understand, especially when code isn't yours. For the moment I see that Flow Layout and Grid Layout can be used. But Grid Layout, for example, stretches buttons to the width of a cell and if a button is with icon only it, it looks very strange then.

Hope you can advise me something.

Community
  • 1
  • 1
Dragon
  • 2,431
  • 11
  • 42
  • 68

1 Answers1

6

For the first case you can use a simple GridLayout on the JPanel with 3 Rows each having a separate JPanel with FlowLayout having constraints, FLowLayout.LEFT. Have a look at this code example :

import java.awt.*;
import javax.swing.*;

public class ExampleLayout
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Example Layout");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(0, 1, 5, 5));

        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);
        JTextField tfield1 = new JTextField(10);
        JButton button1 = new JButton("Button 1");

        topPanel.add(rbut1);
        topPanel.add(tfield1);
        topPanel.add(button1);

        JPanel middlePanel = new JPanel();
        middlePanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        JRadioButton rbut2 = new JRadioButton("RadioButton 2", false);
        JTextField tfield2 = new JTextField(10);
        JButton button2 = new JButton("Button 2");

        middlePanel.add(rbut2);
        middlePanel.add(tfield2);
        middlePanel.add(button2);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
        JCheckBox cbox = new JCheckBox("CheckBox 1", false);
        bottomPanel.add(cbox);

        contentPane.add(topPanel);
        contentPane.add(middlePanel);
        contentPane.add(bottomPanel);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ExampleLayout().displayGUI();
            }
        });
    }
}

OUTPUT :

FIRST BLOCK

And for the Second case, simply add first two components to the JPanel having default Layout. And for the third components, simply add components on to a JPanel having GridBagLayout, with no constraints.

EDIT #1 :

Or you can use this approach, for your second block.

import java.awt.*;
import javax.swing.*;

public class ExampleLayout
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("Example Layout");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JPanel basePanel = new JPanel();
        basePanel.setLayout(new GridLayout(0, 1, 5, 5));

        JPanel topPanel = new JPanel();
        //topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

        JLabel label1 = new JLabel("Label 1", JLabel.CENTER);
        JRadioButton rbut1 = new JRadioButton("RadioButton 1", false);

        topPanel.add(label1);
        topPanel.add(rbut1);        

        JPanel middlePanel = new JPanel();
        middlePanel.setLayout(new GridBagLayout());

        JButton button1 = new JButton("Button 1");

        middlePanel.add(button1);

        basePanel.add(topPanel);
        basePanel.add(middlePanel);

        contentPane.add(basePanel, BorderLayout.PAGE_START);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ExampleLayout().displayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • 1
    Wow, it is incredible. Thanks a lot, I think it is what I need. But speaking about GridBagLayout, isn't it better to substitute it with BoxLayout with center alignment? And about buttons for the first case. If I use only icon without text (for example, "open folder" button) won't its size be stretched? I mean something like 40x16 instead of 16x16 (as icon size is). – Dragon Jan 30 '13 at 12:19
  • 1
    @Dragon : Actually, specifying `GridBagLayout` with no constraints, simply puts the component in the center of the container, it might can work for your case I guess, instead of using `BoxLayout`. Never tried `BoxLayout` that much, since `Nested Layout` approach or `GridBagLayout` can do that task for all easily :-) `BoxLayout` stretches the component depending upon the contents of the component. So sometimes it leads to alignment issues, IMHO (that's why I restrain myself from using it, in many situations). – nIcE cOw Jan 30 '13 at 12:49
  • 1
    @GagandeepBali, it seemed to me that BoxLayout agile enough. Hm, maybe I need to rethink it. Thanks a lot, anyway. – Dragon Jan 30 '13 at 12:54
  • 1
    @Dragon : Please have a look at this wonderful [example](http://stackoverflow.com/a/7919280/1057230) by **mKorbel**, for providing properties to the `JButton`, that might will look good. You might be right from your prespective, as I liked `GridBagLayout` and from your perspective it's a bit unfriendly to work with. For the rest, You're MOST WELCOME and KEEP SMILING :-) – nIcE cOw Jan 30 '13 at 12:55