I am trying to add JButtons to my JPanel that i added to a JScrollPane. And I want that they are in a vertical align. I already found the solution with the BoxLayout(panel, BoxLayout.Y_AXIS) in the Panel, but after I did that the Buttons had different widths.
I also would like to add some space between the Buttons.
CodePudding user response:
You need to use a different layout. Grid or GridBag is what you're looking for, although GridBag is overkill for something this simple:
panel.setLayout(new GridLayout(3, 1));
panel.add(new JButton("Button 1"));
panel.add(new JButton("Button 2"));
panel.add(new JButton("Button 3"));
CodePudding user response:
Use a gridbaglayout , and apply vertical spacers at the button of each button. Also the text in button is not equal to the second and third button dats why it has a different size. So you can adjust it using the preferred size tool