How to set preferred size for BoxLayout Manager in Java?



In this article, we will learn to set the preferred size for a panel within a JFrame using the BoxLayout manager in Java Swing. By setting the preferred and maximum size for the panel, we can control the layout's appearance, ensuring that the panel maintains a consistent size as other components are added. This approach is useful when arranging elements vertically or horizontally in a structured format while having specific size requirements.

Steps to set preferred size for BoxLayout Manager

The following are the steps for setting the preferred size for BoxLayout Manager in Java ?

  • First, we will import the necessary classes from java.awt and java.swing package.
  • Initialize the frame and Layout by creating a JFrame and setting its layout to BoxLayout using a vertical arrangement (BoxLayout.Y_AXIS).
  • Create panels and buttons by initializing a JPanel and several JButton components. Add the buttons to the panel.
  • Set Panel Size and Alignment for that use setPreferredSize() and setMaximumSize() to set the panel's preferred and maximum sizes. For consistent positioning, set the alignment to LEFT_ALIGNMENT.
  • Add Panel to Frame by adding the panel to the frame's content pane and setting the frame size.
  • At the end, we will set the frame visibility to true to display the layout.

Java program to set preferred size for BoxLayout Manager

The following is an example of setting the preferred size for BoxLayout Manager in Java ?

package my;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
      JPanel panel = new JPanel();
      JButton btn1 = new JButton("One");
      JButton btn2 = new JButton("Two");
      JButton btn3 = new JButton("Three");
      JButton btn4 = new JButton("Four");
      JButton btn5 = new JButton("Five");
      panel.add(btn1);
      panel.add(btn2);
      panel.add(btn3);
      panel.add(btn4);
      panel.add(btn5);
      panel.setAlignmentX(Component.LEFT_ALIGNMENT);
      panel.setPreferredSize(new Dimension(100, 500));
      panel.setMaximumSize(new Dimension(100, 500));
      panel.setBorder(BorderFactory.createTitledBorder("demo"));
      frame.getContentPane().add(panel);
      frame.setSize(550, 300);
      frame.setVisible(true);
   }
}

Output

Code explanation

In the above code, we create a JFrame with a vertical BoxLayout (arranging components vertically). We then add a JPanel containing five JButton components (btn1 to btn5), aligned to the left. The setPreferredSize() and setMaximumSize() methods fix the panel's size, keeping it stable. A titled border is added for organization, and the frame is set to be visible with the specified dimensions, displaying the panel with the buttons arranged neatly.

Updated on: 2024-10-29T00:37:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started