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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:08:20  来源:igfitidea点击:

Java - Check Boxes in a JComboBox

javauser-interface

提问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

That's not what combo boxes are "for". Are you sure you don't want, say, a JMenu with JRadioButtonMenuItems?

这不是组合框的“用途”。你确定你不想要一个带有JRadioButtonMenuItems的 JMenu吗?

If you doreally want to proceed, then you'd use a custom renderer, as you suggested.

如果你真的想继续,那么你会使用一个自定义渲染,因为你建议

回答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鼠标点击时显示的文件中。

回答by taynguyen

You can take a look at japura. It's a open source custom component based on swing.

你可以看看japura。它是一个基于swing的开源定制组件。