自从我在这里发帖以来已经有一段时间了,现在我发现自己再次陷入困境。
对于我在大学的作业,我们被要求创建一个地址簿应用程序。进展顺利,即将完成,但是该项目的一部分(纯粹是我的偏好)却让我无法理解。我并不是在作业的任何部分寻求帮助,这会给我带来分数,只是澄清一下。如果有人想要证据,我可以上传规范。 (/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! :)
发布评论
评论(2)
有点这样;尽管它本质上是伪造的,因为复选框实际上并没有做任何事情,但检查它们确实会选择列表项。
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.
我将使用 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.