I am trying to add JButton
components to a JPanel
that I added to a JScrollPane
. I want that they are vertically aligned. 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