Forums Register Login

how to make Jlsit as Checkbox using model in Java Swing

+Pie Number of slices to send: Send
i have create the new JList as below:-

JList AttachFileList = new JList(model);
AttachFileList.setCellRenderer(new CheckBoxListCellRenderer());
AttachFileList.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
AttachFileList.setModel(model);



class CheckBoxListCellRenderer extends JComponent implements ListCellRenderer {
//private static final long serialVersionUID = 1L;
DefaultListCellRenderer defaultComp;
JCheckBox checkbox;

public CheckBoxListCellRenderer() {
setLayout(new BorderLayout());
defaultComp = new DefaultListCellRenderer();
/* defaultComp.setSelectionMode(DefaultListSelectionModel.MULTIPLE_INTERVAL_SELECTION);*/
checkbox = new JCheckBox();
checkbox.setBackground(getBackground());
checkbox.setSelected(false);
// checkbox.setSelected(true);

add(checkbox, BorderLayout.WEST);
add(defaultComp, BorderLayout.CENTER);
}

public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
defaultComp.getListCellRendererComponent(list, value, index,isSelected, cellHasFocus);

checkbox.setSelected(isSelected);

return this;
}
}

problem is that when i clicked one of the item in the JList i can't deselect it my clicking it once again
please help me. . . i'll will help full to my project. . . . thanks in advance
+Pie Number of slices to send: Send
A renderer isn't an editor, and JList doesn't support editing. If that doesn't make much sense to you, read the API for JTable and follow the link to the tutorial on How to Use Tables, where you will find a section titled Renderers and Editors: concepts.
+Pie Number of slices to send: Send
Thank you it help full to my project
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1946 times.
Similar Threads
actionevent in jcombobox of jtable
doubt in JTable
JRadioButtonList error
is it possible to let jComboBox display the object's string field in th DefaultComboBoxModel Vector
Problem with JComboBox background colour when using customer renderer
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:35:34.