How to implement checkbox list java

后端 未结 2 1250
情深已故
情深已故 2020-12-12 07:20

Probably a noob question, but im new to java. I have a need for a checkbox list which I found is not supported in swing, but I found this custom control here

http://

2条回答
  •  伪装坚强ぢ
    2020-12-12 07:38

    The code is expecting a list of JCheckBox objects - so this works

    CheckBoxList cbList = new CheckBoxList();  // the class you have
    JCheckBox check1 = new JCheckBox("One");
    JCheckBox check2 = new JCheckBox("two");
    JCheckBox[] myList = { check1, check2};    list of checkbox object
    cbList.setListData(myList);   // set the list data for the object
    

    Small Swing program using your class below

     util;
    
    import javax.swing.*;
    
    public class HelloWorldSwing {
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        CheckBoxList cbList = new CheckBoxList();
        JCheckBox check1 = new JCheckBox("One");
        JCheckBox check2 = new JCheckBox("two");
        JCheckBox[] myList = { check1, check2};
        cbList.setListData(myList);
        frame.getContentPane().add(cbList);
    
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    

    }

提交回复
热议问题