0

Hey everyone I am trying to add checkboxes into JList but for some reasons it's giving me IllegalArgumentException. If anyone know how to add checkbox in JList please tell me. Thank you in advance

    JCheckBox []data={
    new JCheckBox("C"),
    new JCheckBox("C++"),
    new JCheckBox("Java"),
    new JCheckBox("C sharp")};
    JList l=new JList(data);
    JScrollPane sp=new  JScrollPane(l,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
Vighanesh Gursale
  • 921
  • 5
  • 15
  • 31

3 Answers3

3

This is the full exception message:

IllegalArgumentException: invalid verticalScrollBarPolicy

caused when JScrollPane is instantiated. The JScrollPane policies are the wrong way around:

JScrollPane sp = new JScrollPane(l,
        ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

The problem is in the constructor for JScrollPane. The second parameter should be the Veritical scrollbar policy, not the Horizontal:

public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
sikander
  • 2,286
  • 16
  • 23
0

The illegalArgumentExceptions ocurs because you switch the horizontal scrollbar policy with the vertical scrollbar policy.

But with this code you will not get a list of checkbox, only the text representation of the objets.

Look at this question

Community
  • 1
  • 1
Joan
  • 427
  • 3
  • 6