Search code examples
javaswingborder-layout

Use BorderLayout to create two even columns


I'm trying to create a program using BorderLayout() that I want to look like this (but with even left right height and such)

enter image description here

although I am having trouble resizing the two JPanels (two boxes within the large box). At the moment my GUI looks like this,

enter image description here

I believe it is due to the CENTER still being there, I looked up on how to remove it but could not get it to work,

Question

Can can I edit this so that it will look like the top image.

package fuelstation;
import java.awt.*;
import java.util.*;
import javax.swing.*;


public class Fuelstation extends JFrame {


    JButton btn1 = new JButton("Random Button");


    public Fuelstation() {
        JFrame frame = new JFrame("Fuel Station");
        frame.setLayout(new BorderLayout());
        frame.setResizable(false);
        frame.setPreferredSize(new Dimension(500,350));
        frame.setMaximumSize(new Dimension(500,350));

//        Left Hand Side
        JPanel lhs = new JPanel();
        JTextArea tf_list = new JTextArea();
        tf_list.setEditable(false);
        tf_list.setWrapStyleWord(true);
        tf_list.setText("This is a list of items");
        lhs.add(tf_list);
        tf_list.setSize(245, 325);
        lhs.setBorder(BorderFactory.createTitledBorder("Left"));
//        Left Hand Side End


//        Right Hand Side
        JPanel rhs = new JPanel();
        rhs.setAlignmentX(Component.CENTER_ALIGNMENT);
        rhs.setBorder(BorderFactory.createTitledBorder("Right"));
        rhs.add(btn1);
        tf_list.setSize(245, 325);

        JPanel center = new JPanel();
        center.setSize(0, 0);
//        Right Hand Side End

        frame.add(lhs, BorderLayout.WEST);
        frame.add(center, BorderLayout.CENTER);
        frame.add(rhs, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) {
        Fuelstation gui = new Fuelstation();
    }

}

Solution

  • You need to drop the requirement to use a BorderLayout. The resizing policy for components when using a BorderLayout is stated in the class javadoc

    The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.

    By forcing your JFrame to a certain size

    frame.setResizable(false);
    frame.setPreferredSize(new Dimension(500,350));
    frame.setMaximumSize(new Dimension(500,350));
    

    your center component will take the extra width, as the EAST and WEST component will only stretch vertically.

    So you need to use another LayoutManager. You can use the Visual guide to layout managers to get a grasp of the available LayoutManagers and their capabilities. That document states that a GridLayout would be a good candidate:

    GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns