如何使用jcheckbox和多重选择创建jcombobox [英] how to create jcombobox with jcheck box and multiple selection

查看:90
本文介绍了如何使用jcheckbox和多重选择创建jcombobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建带有复选框和多项选择的JComboBox.我在渲染jlist之后创建了一个带有复选框的列表.我不知道如何用jcombobox渲染它.或者有可能使jlist作为下拉列表,如combo box. 对于jlist渲染,我正在使用以下代码

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

  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类如下

ListCheck Box class is as following

 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;
      }
    }
 }

推荐答案

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

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

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);

这篇关于如何使用jcheckbox和多重选择创建jcombobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆