java 如何使用 jcheck 框和多选创建 jcombobox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15787379/
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-31 20:51:43  来源:igfitidea点击:

how to create jcombobox with jcheck box and multiple selection

javaswingjcomboboxjlistjcheckbox

提问by adesh singh

I want to create JComboBox with checkboxes and multiple selection . i have created a list with check box after rendering the jlist . I dont know how to render it with jcombobox . Or is it possible to make jlist as drop down list like combo box . for jlist rendering i am using the following code

我想创建带有复选框和多选的 JComboBox。渲染 jlist 后,我​​创建了一个带有复选框的列表。我不知道如何用 jcombobox 渲染它。或者是否可以将 jlist 作为下拉列表,如组合框。对于 jlist 渲染,我使用以下代码

  DefaultListModel listModel = new DefaultListModel();
    ListCheckBox li= new ListCheckBox(listModel);
    JScrollPane jsp= new JScrollPane(li);
    add(jsp);

    listModel.add(0,new JCheckBox("Other Court"));

    listModel.add(0,new JCheckBox("Tribunal Court"));

    listModel.add(0,new JCheckBox("High Court"));
   listModel.add(0,new JCheckBox("Supreme Court"));

ListCheck Box class is as following

ListCheck Box 类如下

 import javax.swing.*;
 import javax.swing.border.*;
 import java.awt.*;
 import java.awt.event.*;

 public class ListCheckBox extends JList
{
protected static Border noFocusBorder =
 new EmptyBorder(1, 1, 1, 1);

public ListCheckBox(DefaultListModel model)


  {

   super(model)  ;
   setCellRenderer(new CellRenderer());

    addMouseListener(new MouseAdapter()
     {
        public void mousePressed(MouseEvent e)
        {
           int index = locationToIndex(e.getPoint());

           if (index != -1) {
              JCheckBox checkbox = (JCheckBox)
                          getModel().getElementAt(index);



              checkbox.setSelected(
                                 !checkbox.isSelected());
              repaint();
            }
          }
        }
      );

     setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   }

   protected class CellRenderer implements ListCellRenderer
   {
   public Component getListCellRendererComponent(
    JList list, Object value, int index,
    boolean isSelected, boolean cellHasFocus)
     {  JCheckBox checkbox = (JCheckBox) value;
     checkbox.setBackground(isSelected ?
      getSelectionBackground() : getBackground());
     checkbox.setForeground(isSelected ?
     getSelectionForeground() : getForeground());
     checkbox.setEnabled(isEnabled());
     checkbox.setFont(getFont());
     checkbox.setFocusPainted(false);
     checkbox.setBorderPainted(true);
     checkbox.setBorder(isSelected ?
      UIManager.getBorder(
       "List.focusCellHighlightBorder") : noFocusBorder);
      return checkbox;
      }
    }
 }

回答by Russell Zahniser

The simplest solution might be to create a popup menu with a JCheckBoxMenuItemfor each option, and then attach that popup menu to a button that displays whatever you would want to show for the "selected item".

最简单的解决方案可能是JCheckBoxMenuItem为每个选项创建一个带有 的弹出菜单,然后将该弹出菜单附加到一个按钮,该按钮显示您想要为“所选项目”显示的任何内容。

final JPopupMenu menu = new JPopupMenu();
menu.add(new JCheckBoxMenuItem("Other Court"));
menu.add(new JCheckBoxMenuItem("Tribunal Court"));
menu.add(new JCheckBoxMenuItem("High Court"));
menu.add(new JCheckBoxMenuItem("Supreme Court"));

final JButton button = new JButton();
button.setAction(new AbstractAction("Court") {
    @Override
    public void actionPerformed(ActionEvent e) {
        menu.show(button, 0, button.getHeight());
    }
});

JFrame frame = new JFrame();
frame.getContentPane().add(button);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);