-
December 13th, 2010, 12:24 AM
#1
Checkbox items in combox
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:
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;
}
}
}
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.
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
-
December 13th, 2010, 11:52 AM
#2
Re: Checkbox items in combox
Please use code tags when posting code.
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.
There isn't a rendering issue as such. It's just that the renderer does not redraw the item clicked on as the combobox is designed to hide the drop down list when an item is selected. You have overridden this hide the popup behaviour by providing a null implementation of the setPopupVisible method.
Unfortunately there isn't an easy way that I know of to force the popup to stay visible and force the item in the drop down list to redraw itself.
BTW this is a poor implementation of checkboxes in a drop down list because using the arrow up and down keys causes the selection to change and the newly selected item to change state.
-
May 11th, 2011, 06:31 AM
#3
Re: Checkbox items in combox
-
September 12th, 2014, 03:11 AM
#4
Re: Checkbox items in combox
Originally Posted by charanjeet_sm
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:
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;
}
}
}
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.
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
Hi Charanjeet,
I have the same problem and I have one solution. That is change the code as:
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());
repaint();
checkBox.setBackground(isSelected ? Color.red : Color.white);
checkBox.setForeground(isSelected ? Color.white : Color.black);
return checkBox;
}
Hope this help you!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|