Results 1 to 3 of 3
- 12-13-2010, 04:55 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
Checkbox items in combox rendering
Hi All,
I am trying to show checkboxes in combobox i.e. each element in the combobox will be a checkbox so that user can select multiple options in the combobox. This will be same as "AutoFilter Option in MS Excel 2007". After much googling around, I have the following solution with only one issue pending:
When you run the above code, you will see a combo box with three elements, "Item-A", "Item-B" and "Item-C". All the three elements are checkboxes so that user can select multiple options. However, if you will select the option which is already selected in the combobox, its checkbox will not be rendered correctly, even though the state of the checkbox has changed correctly.Java Code:import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.BorderLayout; import javax.swing.JComboBox; import java.awt.event.ActionEvent; import javax.swing.JList; import javax.swing.ListCellRenderer; import javax.swing.JCheckBox; import java.awt.Component; import java.awt.Color; public class CheckCombo1 extends JFrame { JPanel buttonPanel; CheckCombo checkCombo; public CheckCombo1() { buttonPanel = new JPanel(); checkCombo = new CheckCombo(); checkCombo.addItem(new CheckComboStore("Item-A", true)); checkCombo.addItem(new CheckComboStore("Item-B", true)); checkCombo.addItem(new CheckComboStore("Item-C", true)); buttonPanel.add(checkCombo); getContentPane().add(buttonPanel, BorderLayout.NORTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(650, 250); setVisible(true); } public static void main(String args[]) { CheckCombo1 fe = new CheckCombo1(); } class CheckCombo extends JComboBox { CheckComboRenderer renderer; public CheckCombo() { renderer = new CheckComboRenderer(); setRenderer(renderer); addActionListener(this); } public void actionPerformed(ActionEvent e) { System.out.println("action"); CheckComboStore store = (CheckComboStore) getSelectedItem(); // CheckComboRenderer ccr = (CheckComboRenderer) getRenderer(); store.state = !store.state; // ccr.checkBox.setSelected(store.state); repaint(); } public void setPopupVisible(boolean flag) { } } class CheckComboRenderer implements ListCellRenderer { JCheckBox checkBox; public CheckComboRenderer() { checkBox = new JCheckBox(); // checkBox.addActionListener(new java.awt.event.ActionListener() { // public void actionPerformed(ActionEvent e) { // System.out.println("here"); // } // } // ); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { CheckComboStore store = (CheckComboStore) value; System.out.println("text=" + store.id + " state=" + store.state.booleanValue()); checkBox.setText(store.id); checkBox.setSelected( ( (Boolean) store.state).booleanValue()); checkBox.setBackground(isSelected ? Color.red : Color.white); checkBox.setForeground(isSelected ? Color.white : Color.black); return checkBox; } } class CheckComboStore { String id; Boolean state; public CheckComboStore(String id, Boolean state) { this.id = id; this.state = state; } } }
Ex: On running the sample application, if you open the combobox and select the "Item-A", its checkbox will not be rendered correctly. If you move the mouse over other item (with pop up already opened), you will see the state of the "Item-A" checkbox has indeed changed but it is not showing correctly. The same thing happens whenever you select a already selected item in the combobox. It seems there is some rendering issue.
Please help.
Thanks,
Charanjeet
- 12-13-2010, 07:21 AM #2
Last edited by DarrylBurke; 12-13-2010 at 07:24 AM.
- 12-13-2010, 08:10 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 63
- Rep Power
- 0
The combobox was designed to show one selected item. I would think the preferred solution for multiple selections would be a JList in multi-selection mode or a group of check boxes in a JPanel. Either can have a scroll bar and/or be placed in a popup dialog if realestate is a problem.I am trying to show checkboxes in combobox i.e. each element in the combobox will be a checkbox so that user can select multiple options in the combobox.
Similar Threads
-
selecting checkbox of parent node not resulting into selction of checkbox of all desc
By aparnakumari in forum AWT / SwingReplies: 3Last Post: 07-27-2010, 09:52 AM -
How to get the selected item from combox
By man4ish in forum AWT / SwingReplies: 5Last Post: 12-30-2009, 10:29 AM -
CheckBox Jlist items
By dropez in forum AWT / SwingReplies: 1Last Post: 03-26-2009, 07:39 PM -
Help with JTable rendering
By daniel2008 in forum AWT / SwingReplies: 8Last Post: 01-18-2009, 03:51 AM -
JLabel Rendering
By random4534 in forum New To JavaReplies: 3Last Post: 12-16-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks