0

Is there a way to add jcheckboxes into a jscrollpane?

I have an array of checkboxes and I want to add them into a Jscrollpane, which i will then add into a JOptionPane. So far I have this:

Object[] books = new Object[10000];
    books[0] = "Choose books to purchase: ";
    for(int l = 1;l<checkboxes.length;l++)
    {
        books[l] = checkboxes[l];
    }

    JList list = new JList(checkboxes);
    //JTextPane test = new JTextPane();
    //test.add(checkboxes[0]);

    JScrollPane myScrollPane = new JScrollPane(list);
    myScrollPane.setPreferredSize(new Dimension(250,250));




    JOptionPane.showMessageDialog(null, "Choose the books you want to purchase below.\nPlease note that each book has a $5 shipping charge.\nThe books are formatted as Title - Author - Price", "Reminder",JOptionPane.PLAIN_MESSAGE);
    JOptionPane.showMessageDialog(null,myScrollPane,"Book List",JOptionPane.OK_CANCEL_OPTION);

The output does not give me the checkboxes, just the pointer of the checkboxes i think. As you can see, I've tried putting the checkboxes into a JList but failed.

Thanks a lot guys!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Paolo Papa
  • 37
  • 4
  • 1
    Why not just add them to a JPanel and that to the scroll pane – MadProgrammer Apr 07 '13 at 00:30
  • 3
    You're trying to do a list of Checkboxes aside a list of book titles, so the user can 'check' the ones he wants? First, I'm not sure if you want to put them in a JOptionPane, if you need it to be an external window separate from your main one, a JDialog could be a better option. Second, is normal that when adding the checkboxes to the `JList` you only see the instance information (jcheckbox@codenumber) since the `JList` processes the `toString()` method of the added objects. – Noe Apr 07 '13 at 00:31

1 Answers1

1

You can add the checkboxes to JPanel and that Jpanel can be shown in JDialog instead of JOptionPane. And to add the Checkboxes to JPanel u can check this link How to use JCheckbox array in JPanel array

Community
  • 1
  • 1
Azuu
  • 836
  • 2
  • 16
  • 32