Vighanesh Gursale
Vighanesh Gursale

Reputation: 921

How to add checkboxes in list in java

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);

Upvotes: 0

Views: 2818

Answers (3)

Reimeus
Reimeus

Reputation: 159754

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);

Upvotes: 3

Joan
Joan

Reputation: 426

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

Upvotes: 0

sikander
sikander

Reputation: 2286

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)

Upvotes: 1

Related Questions