62

I am adding checkboxes on JPanel in FlowLayout the checkboxes are being added horizontally.

I want to add checkboxes vertically on the Panel. What is the possible solution?

Matthieu
  • 2,736
  • 4
  • 57
  • 87
adesh
  • 1,157
  • 3
  • 18
  • 28

4 Answers4

58

I hope what you are trying to achieve is like this. For this please use Box layout.

package com.kcing.kailas.sample.client;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;

public class Testing extends JFrame {

    private JPanel jContentPane = null;

    public Testing() {
        super();
        initialize();
    }

    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
        this.setTitle("JFrame");
    }

    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(null);

            JPanel panel = new JPanel();

            panel.setBounds(61, 11, 81, 140);
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            jContentPane.add(panel);

            JCheckBox c1 = new JCheckBox("Check1");
            panel.add(c1);
            c1 = new JCheckBox("Check2");
            panel.add(c1);
            c1 = new JCheckBox("Check3");
            panel.add(c1);
            c1 = new JCheckBox("Check4");
            panel.add(c1);
        }
        return jContentPane;
    }

    public static void main(String[] args) throws Exception {
        Testing frame = new Testing();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
}
Gama11
  • 31,714
  • 9
  • 78
  • 100
Sunil luitel
  • 930
  • 1
  • 8
  • 16
  • 8
    ... and the basic approach (use BoxLayout) differs from the earlier answer by @AbstractChaos in that .. ? Except being worse: null layout in contentpane? manual sizing/positioning of panel? nononono, don't. And what is the updateComponentTreeUI supposed to achieve here? – kleopatra Nov 22 '12 at 11:37
  • 2
    this answe is correct but too complex and outer of focus of problem – Domenico Monaco Jul 18 '14 at 09:45
  • How can I align a single element to the right and have all else centered? – ArchLinuxTux Mar 06 '18 at 10:44
49

I used a BoxLayout and set its second parameter as BoxLayout.Y_AXIS and it worked for me:

panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
Nakul Sudhakar
  • 1,574
  • 1
  • 24
  • 24
15

As I stated in comment i would use a box layout for this.

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout());

JButton button = new JButton("Button1");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);

button = new JButton("Button2");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);

button = new JButton("Button3");
button.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(button);

add(panel);
AbstractChaos
  • 4,211
  • 1
  • 19
  • 28
7
JPanel testPanel = new JPanel();
testPanel.setLayout(new BoxLayout(testPanel, BoxLayout.Y_AXIS));
/*add variables here and add them to testPanel
        e,g`enter code here`
        testPanel.add(nameLabel);
        testPanel.add(textName);
*/
testPanel.setVisible(true);
Opal
  • 81,889
  • 28
  • 189
  • 210
Declan Cahill
  • 71
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! Please consider adding a link to BoxLayout to your answer to improve it, and a short explanation. – juhist Mar 24 '15 at 19:48