Search code examples
javaswinguser-interfacelayoutlayout-manager

how to make custom nested layout in java


enter image description here

im trying to do this form in java and im getting this results enter image description here

how do i place b6 on the top center and b4 on the bottom center and how can i change split panel location


Solution

  • Start by taking a look at Laying Out Components Within a Container

    You need to break down your needs and focus on which layouts are going to give you what you want.

    I'd start with a BorderLayout as it gives you the ability to have two components at the top and bottom, which honour the preferred sizes of those components and the centre component can be allowed to resize based on the available space of the window.

    The title is simple, as it's just a JLabel. For the buttons, I've used a JPanel using a GridBagLayout, as it will centre the components by default, BUT it won't fill them components evenly, so beware of that.

    Both the leading and trailing components of the JSplitPane I used a GridBagLayout as it allows the child components to be laid out in the centre of the container and allows a large amount of flexibility in terms of how the components are managed.

    I've used a EmptyBorder to add some padding around some parts of the components. There are several ways to achieve this, but this is just one way.

    Beware, GridBagLayout is very flexible, but it's a complex layout manager.

    Simple Example

    import java.awt.BorderLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new BorderLayout());
    
                JLabel label = new JLabel("Traducteur");
                label.setHorizontalAlignment(JLabel.CENTER);
                label.setBorder(new EmptyBorder(10, 10, 10, 10));
    
                add(label, BorderLayout.NORTH);
    
                JPanel actions = new JPanel(new GridBagLayout());
                actions.add(new JButton("Help"));
                actions.add(new JButton("Exit"));
                actions.setBorder(new EmptyBorder(10, 10, 10, 10));
    
                add(actions, BorderLayout.SOUTH);
    
                JSplitPane splitPane = new JSplitPane();
    
                JPanel leadingPane = new JPanel(new GridBagLayout());
                leadingPane.setBorder(new EmptyBorder(40, 20, 40, 20));
                JComboBox<String> comboBox = new JComboBox<>();
                DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>();
                model.addElement("In English");
                comboBox.setModel(model);
                leadingPane.add(comboBox);
    
                splitPane.setLeftComponent(leadingPane);
    
                JPanel trailingPane = new JPanel(new GridBagLayout());
                trailingPane.setBorder(new EmptyBorder(40, 20, 40, 20));
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.fill = GridBagConstraints.HORIZONTAL;
                gbc.weightx = 1;
    
                trailingPane.add(new JTextField("Some text"), gbc);
                trailingPane.add(new JTextField("Some more text"), gbc);
    
                splitPane.setRightComponent(trailingPane);
    
                add(splitPane);
            }
    
        }
    }