javaswingjpanelflowlayoutboxlayout

Java Swing BoxLayout: Adjusting space between panels in a multi-panel setup?


I'm writing this swing application and I'm using multiple panels in a BoxLayout format, but it seems to be that the empty space between the panels is being divided up between them and it looks really ugly. How do you adjust and customize how much space is put between panels? Sorry if this is a repost; I was unable to find an older post.

    import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

class gui extends JFrame implements ActionListener{
    JFrame frame = new JFrame("Ark Admin Spawn Commands");
    JPanel panel = new JPanel();
    JComboBox dinos;
    JComboBox corruptedDinos;
    public JSlider dinoLevel;
    JLabel input;
    JLabel dino;
    JLabel title;
    
    
    
    
    
    gui(){
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          JPanel mainPanel = new JPanel();
          mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));

          JPanel panelOne = new JPanel();
          JPanel panelTwo = new JPanel();
          JPanel panelThree = new JPanel();

          mainPanel.add(panelOne);
          mainPanel.add(panelTwo);
          mainPanel.add(panelThree);
         
        
        
        
        title = new JLabel("Spawning Dinos");
        
        String[] dinoNames= {"The Island:"};
        String[] corruptedDinoNames = {"Extinction:"};
        dinos = new JComboBox(dinoNames);
        corruptedDinos = new JComboBox(corruptedDinoNames);
        dinoLevel = new JSlider(JSlider.HORIZONTAL, 1, 600, 400);
        dinoLevel.setMajorTickSpacing(20);
        dinoLevel.setPaintTicks(true);
        input = new JLabel("Select Level: ");
        event e = new event();
        dinoLevel.addChangeListener(e);
        
        
        dinos.addActionListener(this);
        corruptedDinos.addActionListener(this);
        panelOne.add(title);
        
        panelTwo.add(input);
        panelTwo.add(dinoLevel);
        
        panelThree.add(dinos);
        panelThree.add(corruptedDinos);
        this.getContentPane().add(mainPanel);
        this.setTitle("Ark Admin Spawn Commands");
        this.setSize(600, 600);
        this.setVisible(true);
        
    }
    public static void main (String args[]) {

        new gui();
        
        
    
    
    }
    
    public class event implements ChangeListener{
        
        @Override
        public void stateChanged(ChangeEvent e) {
            int value = dinoLevel.getValue();
            input.setText("Level: " + value);
            
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent arg0) {
        
        
    }
}

Solution

  • but it seems to be that the empty space between the panels is being divided up between them

    Correct. A BoxLayout will attempt to allocate extra space to all components.

    However, it will respect the maximum size of each panel.

    So to prevent the panels height from growing you can use code like:

    JPanel panelOne = new JPanel()
    {
        @Override
        public dimension getMaximumSize()
        {
            Dimension d = getPreferredSize()
            d.width = Integer.MAX_VALUE;
    
            return d;
        }
    };
    

    Or because the default layout manager of the frame is a BorderLayout, you can just use:

    //this.getContentPane().add(mainPanel);
    add(mainPanel, BorderLayout.PAGE_START); // getContentPane() is not needed
    

    The PAGE_START of the BorderLayout respects the preferred height of the component.