JList 包含 JCheckBox 和一个字符串,带有非 ctrl 单击选择?

发布于 2024-08-12 18:45:15 字数 1058 浏览 11 评论 0 原文

自从我在这里发帖以来已经有一段时间了,现在我发现自己再次陷入困境。

对于我在大学的作业,我们被要求创建一个地址簿应用程序。进展顺利,即将完成,但是该项目的一部分(纯粹是我的偏好)却让我无法理解。我并不是在作业的任何部分寻求帮助,这会给我带来分数,只是澄清一下。如果有人想要证据,我可以上传规范。 (/disclosure)

该应用程序的一部分是从自定义文件类型和 vcard 格式导入和导出。这很好用。自定义文件类型包含多个联系人,每个联系人仅包含 4 行。由于它包含多个联系人,因此会打开一个新窗口,要求用户选择他们希望导出到文件中的联系人。其中包含一个 JList,用户可以在其中选择联系人并导出文件,没有问题。

我一直在尝试做的是创建一个自定义单元格渲染器,它将允许在 JList 中使用复选框。我几乎通过查看这里(http:// /java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer)和此处(http://www.jroller.com/santhosh/entry/jlist_with_checkboxes),但是没有运气。第一个链接解释了如何创建自定义单元格渲染器,但不解释复选框,第二个链接解释了如何创建复选框。我可以显示复选框,但是这就是显示的全部内容。

在后一个示例中,必须单击该框本身才能选择列表中的成员。我希望能够做的是允许用户单击列表项或复选框来选择该项目。

在我使用的仅复选框的版本中,选择有效,但是仍然必须按住 ctrl 才能选择多个项目。有什么方法可以让多项选择时不需要 ctrl 吗?我知道您可以在单击时读取修饰符,但不知道是否可以将它们应用于指定对象的单击。

非常欢迎您的想法、评论、建议、链接和准确答案! :)

Been a while since I posted on here, and now I am at a point where I find myself stuck yet again.

For my assignment at uni, we have been asked to create an address book application. It is going well, and it's nearly completed, however one part of the project, which is purely my preference, eludes me. I'm not asking for help on any part of the assignment which will gain me marks, just to be clear. I can upload the spec if anyone would like evidence of that. (/disclosure)

Part of the application is importing and exporting from a custom file type and the vcard format. This works fine. The custom file type holds multiple contacts, with only 4 lines per contact. Because it holds multiple contacts, a new window opens which asks the user to select the contacts they wish to be exported to the file. This contains a JList, where the user can select the contacts and export the file, no problem.

What I have been trying to do, is create a custom cell renderer which will allow the use of a check box in the JList. I nearly got this working by looking here (http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html#renderer) and here (http://www.jroller.com/santhosh/entry/jlist_with_checkboxes), however no luck. The first link explains how to create the custom cell renderer, but not check boxs, and the second explains how to do check boxs. I can get check boxs to display, however then that is ALL that displays.

In the latter example, the box's themselves must be clicked in order for the members of the list to be selected. What I would like to be able to do, is to allow the user to click on either the list items or the check box's to select the item.

In the version of just check boxs that I got working, the selection worked, however ctrl still had to be held down for multiple items to be selected. Is there any way to make it so ctrl is not needed for multiple selection of items? I know you can read modifiers on a click, but no idea if you can apply them to a click of a specified object.

Your thoughts, comments, suggestions, links and exact answers are all very welcome! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雾里花 2024-08-19 18:45:15

有点这样;尽管它本质上是伪造的,因为复选框实际上并没有做任何事情,但检查它们确实会选择列表项。

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public class Relequestual extends JFrame {

   private final FunkyCellRendererComponent funkyCRC = new FunkyCellRendererComponent();

   public Relequestual() {
      getContentPane().setLayout(new FlowLayout());

      String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
      JList theList = new JList(items);
      theList.setCellRenderer(
      new ListCellRenderer() {
         @Override
         public Component getListCellRendererComponent(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
            funkyCRC.setup(list, value, index, isSelected, cellHasFocus);
            return funkyCRC;
         }
      });
      getContentPane().add(theList);

   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      Relequestual gui = new Relequestual();
      gui.pack();
      gui.setVisible(true);
   }

   class FunkyCellRendererComponent extends JPanel {

      private JCheckBox checkBox = new JCheckBox();
      private JLabel label = new JLabel();
      private JList list;
      private int index;

      public FunkyCellRendererComponent() {
         super(null);
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
         add(this.checkBox);
         add(this.label);
      }

      public void setup(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
         this.list = list;
         this.index = index;
         this.label.setText((String) list.getModel().getElementAt(index)); 
         if (isSelected) {
            setForeground(list.getSelectionForeground());
            setBackground(list.getSelectionBackground());
         } else {
            setForeground(list.getForeground());
            setBackground(list.getBackground());
         }
         this.checkBox.setSelected(isSelected);
      }

   }

}

This does it, sort of; though it's essentially faked because the checkboxes don't really do anything, yet checking them does select the list item.

import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;


public class Relequestual extends JFrame {

   private final FunkyCellRendererComponent funkyCRC = new FunkyCellRendererComponent();

   public Relequestual() {
      getContentPane().setLayout(new FlowLayout());

      String[] items = { "Item 1", "Item 2", "Item 3", "Item 4" };
      JList theList = new JList(items);
      theList.setCellRenderer(
      new ListCellRenderer() {
         @Override
         public Component getListCellRendererComponent(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
            funkyCRC.setup(list, value, index, isSelected, cellHasFocus);
            return funkyCRC;
         }
      });
      getContentPane().add(theList);

   }

   /**
    * @param args
    */
   public static void main(String[] args) {
      Relequestual gui = new Relequestual();
      gui.pack();
      gui.setVisible(true);
   }

   class FunkyCellRendererComponent extends JPanel {

      private JCheckBox checkBox = new JCheckBox();
      private JLabel label = new JLabel();
      private JList list;
      private int index;

      public FunkyCellRendererComponent() {
         super(null);
         setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
         add(this.checkBox);
         add(this.label);
      }

      public void setup(JList list, Object value, int index,
               boolean isSelected, boolean cellHasFocus) {
         this.list = list;
         this.index = index;
         this.label.setText((String) list.getModel().getElementAt(index)); 
         if (isSelected) {
            setForeground(list.getSelectionForeground());
            setBackground(list.getSelectionBackground());
         } else {
            setForeground(list.getForeground());
            setBackground(list.getBackground());
         }
         this.checkBox.setSelected(isSelected);
      }

   }

}
酒浓于脸红 2024-08-19 18:45:15

我将使用 JTable 并将复选框添加为单独的列。

然后,您可以将 MouseListener 添加到表中,每当选中另一列时,您就可以切换复选框的值。

I would use a JTable and add the checkbox as a separate column.

You could then add a MouseListener to the table and whenever the other column is checked you toggle the value of the checkbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文