-1

i am trying to make some list of JCheckBoxes and put it inside of a scrollpane.

I tried to make array of checkboxes and put it in JList and then in JScrollPane but it only prints some info about checkboxes from methods.

I want to achieve something like this:

enter image description here

This is my code so far:

public class MainFrame extends JFrame
{
private JPanel panel = new JPanel();
private JScrollPane scroll;

public MainFrame()
{
    add();
    setTitle("Dropable checkbox");
    setSize(500, 500);

    add(panel);

    setVisible(true);
}

private void add()
{
    String categories[] = { "Household", "Office", "Extended Family",
            "Company (US)", "Company (World)", "Team", "Will",
            "Birthday Card List", "High School", "Country", "Continent",
            "Planet","KITA" };

    JPanel p = new JPanel();
    BoxLayout layout = new BoxLayout(p, BoxLayout.Y_AXIS);
    p.setLayout(layout);

    for (String string : categories) {
        p.add(new JCheckBox(string));
    }

    JScrollPane scroll = new JScrollPane(p);
    panel.add(scroll);
}

}

This is what my screen looks like now

enter image description here

Miljan Rakita
  • 1,433
  • 4
  • 23
  • 44
  • one could create a Y_LAYOUT box and add the checkboxes to that. see https://docs.oracle.com/javase/8/docs/api/javax/swing/Box.html and http://docs.oracle.com/javase/8/docs/api/javax/swing/BoxLayout.html – Chains Nov 02 '16 at 22:28
  • Put the check boxes in a panel. Put the panel in a scroll pane. – Andrew Thompson Nov 02 '16 at 22:28
  • I made one panel with Y_layout, put all the checkboxes in it and then that panel put in ScrollPane. When i put that scrollpane into panel, and then panel into frame it doesnt show the scrollbar. But when i put scrollpane directly to the frame then it show scroll bar. How to get scroll bar in panel ? – Miljan Rakita Nov 02 '16 at 22:38
  • scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); might help with getting the scrollbar to show – Chains Nov 02 '16 at 22:53
  • 2
    Why not use a single column `JTable` with a suitable renderer & editor? – trashgod Nov 02 '16 at 23:32
  • @Chains I see some changes but scroll bar doesnt show up... – Miljan Rakita Nov 03 '16 at 08:05

1 Answers1

0

You should rename your variables and methods to the Java standard, because you seem to have confused yourself.

For example you are creating the scroll panel 2 times (once at the top and once at the bottom). Also the panel p is not needed. Also you dont create the scroll pane the way it should be created.

I've created a working program, but I use the MigLayout since I dont know the BoxLayout that much, but you should see the point and understand the important part of how to create the scroll pane.

public class MainFrame extends JFrame {
    private JPanel panel = new JPanel();
    JScrollPane scroll = new JScrollPane();

    public MainFrame() {
        createUI();
        setTitle("Dropable checkbox");
        setSize(300, 400);
        setVisible(true);
    }

    private void createUI() {
        String categories[] = { "Household", "Office", "Extended Family", "Company (US)", "Company (World)", "Team",
                "Will", "Birthday Card List", "High School", "Country", "Continent", "Planet", "KITA" };

        setLayout(new MigLayout());
        panel.setLayout(new MigLayout("wrap 1"));

        for (String string : categories) {
            panel.add(new JCheckBox(string));
        }
        scroll.setViewportView(panel);
        add(scroll);
    }

    public static void main(String[] args) {
        MainFrame mf = new MainFrame();
        mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mf.setLocationRelativeTo(null);
    }
}
hamena314
  • 2,969
  • 5
  • 30
  • 57
  • 1
    JTable with only one column, without JTableHeader :-) – mKorbel Nov 03 '16 at 12:54
  • @mKorbel: Yeah [trashgod](http://stackoverflow.com/questions/40390744/how-to-make-list-of-jcheckboxes-and-put-it-into-jscrollpane/40397037?noredirect=1#comment68034699_40390744) proposed this idea which I find better as well. But since OP did not include the purpose of their component after trashgod pointed this out, I went ahead and showed solution with a scrollpane and checkboxes. – hamena314 Nov 03 '16 at 13:05