javaswingborder-layout

Java BorderLayout: How to set Preferred size of components


I Have a Frame with two panels, one named ScreenPanel and one named MenuPanel, Both have Border Layout Manager. ScreenPanel is the Content Panel for the JFrame. Menu Panel is on the WEST of the ScreenPanel. However, when I run, the width of the MenuPanel is too small, I tried adding setSize(); to MainPanel and all of its components, but that didn't work, is there a way I can set the size of the MenuPanel in BorderLayout?

MenuPanel:

DefaultListModel<String> MenuModel = new DefaultListModel<>();
JList<String> MenuList = new JList<>(MenuModel);
JLabel MenuLabel = new JLabel();
MenuPanel(){
    setBackground(new Color(0,168,243));
    setLayout(new BorderLayout());

    MenuList.setBackground(new Color(0,200,250));
    MenuList.setForeground(new Color(80,80,80));
    MenuList.setFont(new Font("Arial",Font.BOLD,20));

    MenuModel.addElement("Page 1");
    MenuModel.addElement("Page 2");
    MenuModel.addElement("Page 3");
    MenuModel.addElement("Page 4");
    MenuModel.addElement("Page 5");

    MenuLabel.setText("Menu");
    MenuLabel.setFont(new Font("Arial",Font.BOLD,50));
    MenuLabel.setForeground(new Color(50,50,50));

    add(MenuList,BorderLayout.CENTER);
    add(MenuLabel,BorderLayout.NORTH);

ScreenPanel:

public class ScreenPanel extends JPanel{
     ScreenPanel(){
         setLayout(new BorderLayout());
         setBackground(new Color(230,230,230));
         add(new MenuPanel(),BorderLayout.WEST);
     }
 }

Frame:

 public class ScreenFrame extends JFrame{
     ScreenFrame(){
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setExtendedState(Frame.MAXIMIZED_BOTH);
         setContentPane(new ScreenPanel());
        setTitle("Random Project");
     }
 }

Main Method:

 public class Main {
     public static void main(String[] args) {
         ScreenFrame screen = new ScreenFrame();
         screen.pack();
         screen.setVisible(true);
     }
 }

Expectation: enter image description here

Reality: enter image description here


Solution

  • First of all, variable names should NOT start with an upper case character. Learn and follow Java conventions.

    I tried adding setSize()

    Don't attempt to play with the size. It is the responsibility of the layout manager to determine the size and location of each component.

    You should also not attempt to "set" the preferred size of a component. All Swing components determine their own preferred size based on the properties of the component.

    You can affect the preferred size calculation of components by changing the properties of the component.

    For all components you could use:

    1. setFont(..) - to make the text larger
    2. setBorder( new EmptyBorder(...) ) - to give extra space around the 4 borders. The border can be set for individual components or on the JPanel for all components added to the panel.

    In addition, for the JList you can use:

    1. setPrototypeCellValue( "Page 0000" ) - to calculate a larger preferred width based on the text you specify

    Read the API you may find other properties you can change.