Java - JComboBox 中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1573159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Check Boxes in a JComboBox
提问by javacavaj
I would like to make a JComboBox that has check boxes for items instead of text. In addition, it should be possible to check multiple items and retrieve the selected items from the component. Should I be make a custom ComboBoxUI, ComboBoxEditor, ListCellRenderer, ComboPopUp, or something different entirely? Is there an existing Java control that does this?
我想制作一个 JComboBox,它具有项目的复选框而不是文本。此外,应该可以检查多个项目并从组件中检索所选项目。我应该制作自定义 ComboBoxUI、ComboBoxEditor、ListCellRenderer、ComboPopUp 还是完全不同的东西?是否有现有的 Java 控件可以执行此操作?
回答by javacavaj
This was fairly easy to implement. Found it here link text
这很容易实现。在这里找到链接文本
/* * The following code is adapted from Java Forums - JCheckBox in JComboBox URL: http://forum.java.sun.com/thread.jspa?forumID=257&threadID=364705 Date of Access: July 28 2005 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.*;
public class JComboCheckBox extends JComboBox {
public JComboCheckBox() { addStuff(); }
public JComboCheckBox(JCheckBox[] items) { super(items); addStuff(); }
public JComboCheckBox(Vector items) { super(items); addStuff(); }
public JComboCheckBox(ComboBoxModel aModel) { super(aModel); addStuff(); }
private void addStuff() {
setRenderer(new ComboBoxRenderer());
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) { itemSelected(); }
});
}
private void itemSelected() {
if (getSelectedItem() instanceof JCheckBox) {
JCheckBox jcb = (JCheckBox)getSelectedItem();
jcb.setSelected(!jcb.isSelected());
}
}
class ComboBoxRenderer implements ListCellRenderer {
private JLabel defaultLabel;
public ComboBoxRenderer() { setOpaque(true); }
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if (value instanceof Component) {
Component c = (Component)value;
if (isSelected) {
c.setBackground(list.getSelectionBackground());
c.setForeground(list.getSelectionForeground());
} else {
c.setBackground(list.getBackground());
c.setForeground(list.getForeground());
}
return c;
} else {
if (defaultLabel==null) defaultLabel = new JLabel(value.toString());
else defaultLabel.setText(value.toString());
return defaultLabel;
}
}
}
}
回答by Jonathan Feinberg
回答by akf
We were once given this same inane requirement as well. We complied with a brand new component.
It was essentially a JPanelwhich had a text field and a down arrow button. It contained a JListwhich used a JCheckbox-derrived ListCellRenderer. The JListwas packaged in a JPopupMenuwhich was displayed on mouse clicks.
我们曾经也被赋予了同样的愚蠢要求。我们遵守了一个全新的组件。它本质上是一个JPanel有一个文本字段和一个向下箭头按钮的。它包含一个JList使用了JCheckbox-derrived 的ListCellRenderer。该文件JList被打包在一个JPopupMenu鼠标点击时显示的文件中。

