简体   繁体   English

获取JCheckListBox的所有已检查项目索引

[英]get all the checked items indices of the JCheckListBox

Hello am trying to get all the checked items indices of the checkbox list in a array. 您好,我试图获取数组中复选框列表的所有选中项索引。 but apparently the method getCheckBoxListSelectedIndices(); 但是显然方法getCheckBoxListSelectedIndices(); is returning an empty array 返回一个空数组

package cct.karim;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.jidesoft.swing.CheckBoxList;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 *
 * @author beastieux
 */

public class E_JCheckListBox extends JFrame implements ActionListener {
    protected JLabel loglabel;
    private CheckBoxList List; 
    public E_JCheckListBox()
    {
        super("Select RFQs to Export");

        loglabel=new JLabel("Log:                                   .");
        List=new CheckBoxList();
        CheckBoxList List = new CheckBoxList();
        JScrollPane scp=new JScrollPane();

        DefaultListModel lmdlEjemplo=new DefaultListModel();

        lmdlEjemplo.addElement("  Item 0  ");
        lmdlEjemplo.addElement("  Item 1  ");
        lmdlEjemplo.addElement("  Item 2  ");
        lmdlEjemplo.addElement("  Item 3  ");
        lmdlEjemplo.addElement("  Item 4  ");
        lmdlEjemplo.addElement("  Item 5  ");
        lmdlEjemplo.addElement("  Item 6  ");
        lmdlEjemplo.addElement("  Item 7  ");
        lmdlEjemplo.addElement("  Item 8  ");
        lmdlEjemplo.addElement("  Item 9  ");

        List.setModel(lmdlEjemplo);

        scp.add(List);
        this.add(scp);

        scp.getViewport().add(List);

        getContentPane().setLayout(new FlowLayout());
        this.setSize(300, 300);
        scp.setSize(300, 350);

        JPanel p = new JPanel();
        p.setSize(500, 500);
        p.setLayout(new BorderLayout());
        p.add(scp, BorderLayout.CENTER);
        p.add(loglabel, BorderLayout.SOUTH);
        p.setBorder(new TitledBorder(new EtchedBorder(),
        "Please select options:") );

        getContentPane().add(p);
        JButton k= new JButton("Export");
        k.addActionListener(this);

        k.setMaximumSize(new Dimension(1, 1));
        add(k);
    //  pack();
        setVisible(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    }

    public static void main(String args[]) {
        E_JCheckListBox obj = new E_JCheckListBox();
        obj.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        int k[]=List.getCheckBoxListSelectedIndices();
        System.out.println(k[0]);
    }
}

This is the exeption I got: 这是我得到的证明:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 
at cct.karim.E_JCheckListBox.actionPerformed(E_JCheckListBox.java:92)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

am i missing something because am new to GUI programming ? 我是否缺少某些东西,因为它是GUI编程的新手?

The problem appears to be simple. 问题似乎很简单。 You have instance variable and method variable both name same. 您的实例变量和方法变量的名称相同。 List is declared as instance variable and used in ActionListner but same is used to create a local variable so the local got preference in constructor. List声明为实例变量,并在ActionListner中使用,但相同的变量用于创建局部变量,因此局部变量在构造函数中具有首选项。

Erroneous code is corrected below should work. 错误代码已在下面更正,可以正常工作。

private CheckBoxList List; 
        public E_JCheckListBox()
        {
            super("Select RFQs to Export");

            loglabel=new JLabel("Log:                                   .");
            List=new CheckBoxList();
//            CheckBoxList List = new CheckBoxList();
            JScrollPane scp=new JScrollPane();

Also, please add for length checking in array before accessing it's elements. 另外,请在访问数组元素之前添加数组长度检查。 In Java Array indexes are strongly checked. 在Java中,对数组的索引进行严格检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM