简体   繁体   中英

how to add a checkbox into JList (java netbeans)?

I have added a checkbox into JList its working successfully but problem is it not select multiple checkbox in same time and the multi-selection code for checkbox not working and I also add the image.

在此处输入图片说明

 lstsubsub.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

public class CheckboxListCellRenderer extends JCheckBox implements 
       ListCellRenderer {

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, 
            boolean isSelected, boolean cellHasFocus) {


        setComponentOrientation(list.getComponentOrientation());
        setFont(list.getFont());
        setBackground(list.getBackground());
        setForeground(list.getForeground());
        setSelected(isSelected);
        setEnabled(list.isEnabled());


        setText(value == null ? "" : value.toString());  

        return this;
    }
}

Don't use a JList for the JCheckBoxe s... Use a JPanel with GridLayout like so:

import java.awt.GridLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
    private final JCheckBox checkReg, checkPerm, checkAcc;

    public Main() {
        super(new GridLayout(0, 1)); //1 column, any number of rows...
        super.add(checkReg = new JCheckBox("User Registration"));
        super.add(checkPerm = new JCheckBox("User Permission"));
        super.add(checkAcc = new JCheckBox("User Accounts"));
    }

    public static void main(final String[] args) {
        final JFrame frame = new JFrame("List of checkboxes");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Main());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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