Different layout managers handle the preferred size differently. Also, setting the size with setSize() not a good idea. Let the layout manager do the layout for you. See the Visual Guide for Layout Managers for more information.
For example, you can create a separate panel containing buttons. Set its location to GridLayout . In this layout, the components occupy all the free space inside their cell, and each cell has exactly the same size. Add this panel to the container. See How to use GridLayout for an example.
Here is a simple demo of GridLayout and GridBagLayout :

import java.awt.*; import javax.swing.*; public class DemoButtons { public DemoButtons() { final JFrame frame = new JFrame("Demo buttons"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(new GridLayout(3, 1)); buttonPanel.add(new JButton("Export do SVG")); buttonPanel.add(new JButton("Export do PNG")); buttonPanel.add(new JButton("Tisk...")); JPanel east = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.anchor = GridBagConstraints.NORTH; gbc.weighty = 1; east.add(buttonPanel, gbc); JPanel center = new JPanel(){ @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } }; center.setBorder(BorderFactory.createLineBorder(Color.BLACK)); frame.add(east, BorderLayout.EAST); frame.add(center); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { e.printStackTrace(); } SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new DemoButtons(); } }); } }
source share