如何在 Java Swing 中制作带有复选框的列表?
在 Java Swing 中拥有每个项目都带有复选框的列表的最佳方式是什么?
即一个 JList,其中每个项目都有一些文本和一个复选框?
What would be the best way to have a list of items with a checkbox each in Java Swing?
I.e. a JList with items that have some text and a checkbox each?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
只需实现
ListCellRenderer
并设置渲染器,
这将导致
详细信息位于 自定义 swing 组件渲染器。
PS:如果您想要单选元素,只需将
extends JCheckbox
替换为extends JRadioButton
即可。Just implement a
ListCellRenderer
and set the renderer
this will result in
Details at Custom swing component renderers.
PS: If you want radio elements just replace
extends JCheckbox
withextends JRadioButton
.创建自定义
ListCellRenderer
并将其分配给JList
。此自定义
ListCellRenderer
必须在getListCellRendererComponent(...)
方法的实现中返回一个JCheckbox
。但是这个
JCheckbox
将不可编辑,只是在屏幕上进行简单的绘制,由您选择何时必须“勾选”或不“勾选”这个JCheckbox
,例如,显示当选择该行时(参数
isSelected
),它会打勾,但是这样,如果选择发生变化,检查状态将不会保持。 最好显示它检查了ListModel
下面的数据,但接下来由您来实现更改数据检查状态的方法,并将更改通知JList< /code> 重新绘制。
如果您需要的话,我稍后会发布示例代码
ListCellRenderer
Create a custom
ListCellRenderer
and asign it to theJList
.This custom
ListCellRenderer
must return aJCheckbox
in the implementantion ofgetListCellRendererComponent(...)
method.But this
JCheckbox
will not be editable, is a simple paint in the screen is up to you to choose when thisJCheckbox
must be 'ticked' or not,For example, show it ticked when the row is selected (parameter
isSelected
), but this way the check status will no be mantained if the selection changes. Its better to show it checked consulting the data below theListModel
, but then is up to you to implement the method who changes the check status of the data, and notify the change to theJList
to be repainted.I Will post sample code later if you need it
ListCellRenderer
一个很好的答案是这个
CheckBoxList
。 它实现了 Telcontar 的答案(尽管是 3 年前:)...我在 Java 1.6 中使用它没有任何问题。 我还添加了一个像这样的addCheckbox
方法(当然可以更短,有一段时间没有使用 Java):我尝试了 Jidesoft 的演示,使用
CheckBoxList
我遇到了一些问题(不起作用的行为)。 如果我发现链接到的CheckBoxList
存在问题,我将修改此答案。A wonderful answer is this
CheckBoxList
. It implements Telcontar's answer (though 3 years before :)... I'm using it in Java 1.6 with no problems. I've also added anaddCheckbox
method like this (surely could be shorter, haven't used Java in a while):I tried out the demo for the Jidesoft stuff, playing with the
CheckBoxList
I encountered some problems (behaviors that didn't work). I'll modify this answer if I find problems with theCheckBoxList
I linked to.使用 Java,很有可能有人已经实现了您需要的小部件或实用程序。 大型 OSS 社区的部分好处。 除非您真的想自己动手,否则无需重新发明轮子。 在这种情况下,这将是一次很好的 CellRenderers 和 Editor 学习练习。
我的项目在JIDE的帮助下取得了巨大的成功。 您想要的组件(复选框列表)位于 JIDE 公共层(它是 OSS,托管在 java.net 上)。 商业的东西也很好,但你不需要它。
http://www.jidesoft.com/products/oss.htm
https://jide-oss.dev.java.net/
Odds are good w/ Java that someone has already implemented the widget or utility you need. Part of the benefits of a large OSS community. No need to reinvent the wheel unless you really want to do it yourself. In this case it would be a good learning exercise in CellRenderers and Editors.
My project has had great success with JIDE. The component you want, a Check Box List, is in the JIDE Common Layer (which is OSS and hosted on java.net). The commercial stuff is good too, but you don't need it.
http://www.jidesoft.com/products/oss.htm
https://jide-oss.dev.java.net/
Swing 中的所有聚合组件(即由其他组件组成的组件,例如 JTable、JTree 或 JComboBox)都可以高度自定义。 例如,JTable 组件通常显示 JLabel 组件的网格,但它也可以显示 JButton、JTextField 甚至其他 JTable。 然而,让这些聚合组件显示非默认对象是比较容易的部分。 由于 Swing 将组件分为“渲染器”和“编辑器”,因此使它们正确响应键盘和鼠标事件是一项艰巨的任务。 这种分离(在我看来)是一个糟糕的设计选择,并且只会在尝试扩展 Swing 组件时使问题变得复杂。
要明白我的意思,请尝试增强 Swing 的 JList 组件,以便它显示复选框而不是标签。 根据 Swing 理念,此任务需要实现两个接口:ListCellRenderer(用于绘制复选框)和 CellEditor(用于处理复选框上的键盘和鼠标事件)。 实现 ListCellRenderer 接口很容易,但 CellEditor 接口可能相当笨拙且难以理解。 在这种特殊情况下,我建议完全忘记 CellEditor 并直接处理输入事件,如以下代码所示。
在这里,我拦截列表框中的鼠标单击并模拟相应复选框的单击。 结果是一个“CheckBoxList”组件,它比使用 CellEditor 界面的等效组件更简单、更小。 要使用该类,只需实例化它,然后通过调用 setListData 向其传递 JCheckBox 对象(或 JCheckBox 对象的子类)的数组。 请注意,此组件中的复选框不会响应按键(即空格键),但如果需要,您可以随时添加自己的按键侦听器。
来源:DevX.com
All of the aggregate components in Swing--that is, components made up other components, such as JTable, JTree, or JComboBox--can be highly customized. For example, a JTable component normally displays a grid of JLabel components, but it can also display JButtons, JTextFields, or even other JTables. Getting these aggregate components to display non-default objects is the easy part, however. Making them respond properly to keyboard and mouse events is a much harder task, due to Swing's separation of components into "renderers" and "editors." This separation was (in my opinion) a poor design choice and only serves to complicate matters when trying to extend Swing components.
To see what I mean, try enhancing Swing's JList component so that it displays checkboxes instead of labels. According to Swing philosophy, this task requires implementing two interfaces: ListCellRenderer (for drawing the checkboxes) and CellEditor (for handling keyboard and mouse events on the checkboxes). Implementing the ListCellRenderer interface is easy enough, but the CellEditor interface can be rather clumsy and hard to understand. In this particular case, I would suggest forgetting CellEditor entirely and to handle input events directly, as shown in the following code.
Here, I intercept mouse clicks from the listbox and simulate a click on the appropriate checkbox. The result is a "CheckBoxList" component that is both simpler and smaller than an equivalent component using the CellEditor interface. To use the class, simply instantiate it, then pass it an array of JCheckBox objects (or subclasses of JCheckBox objects) by calling setListData. Note that the checkboxes in this component will not respond to keypresses (i.e. the spacebar), but you could always add your own key listener if needed.
Source: DevX.com
我建议您使用 GridLayout 为 1 列的 JPanel。 将复选框添加到 JPanel 中,并将 JPanel 设置为 JScrollPane 的数据源。 要获取选中的复选框,只需调用 JPanel 的 getComponents() 即可获取复选框。
I recommend you use a JPanel with a GridLayout of 1 column. Add the checkBoxes to the JPanel, and set the JPanel as the data source of a JScrollPane. And to get the selected CheckBoxes, just call the getComponents() of the JPanel to get the CheckBoxes.
我不喜欢将复选框放入模型中的解决方案。 模型应该只包含数据而不是显示元素。
我发现这个 http://www.java2s.com/Tutorials/Java /Swing_How_to/JList/Create_JList_of_CheckBox.htm
我对其进行了一些优化。 ACTIVE 标志代表复选框,SELECTED 标志显示光标所在的条目。
我的版本需要一个渲染器
和一个获得活动字段的实体:
现在您只需将其添加到您的 JList 中:
I don't like the solutions that put a Checkbox into the model. The model should only contain data not display elements.
I found this http://www.java2s.com/Tutorials/Java/Swing_How_to/JList/Create_JList_of_CheckBox.htm
which I optimized a bit. The ACTIVE flag represents the Checkbox, the SELECTED flag shows what entry the cursor sits on.
my version requires a renderer
and an entity that got the active field:
Now you only have to add this to your JList:
这是使用复选框制作列表的另一个示例
this is yet another example of making list with checkboxes
我可能希望使用 JTable 而不是 JList 并且由于复选框的默认渲染相当难看,我可能希望添加一个自定义 TableModel、CellRenderer 和 CellEditor 来表示布尔值。 当然,我想这已经被做过无数次了。 Sun 有很好的例子。
I'd probably be looking to use a JTable rather than a JList and since the default rendering of a checkbox is rather ugly, I'd probably be looking to drop in a custom TableModel, CellRenderer and CellEditor to represent a boolean value. Of course, I would imagine this has been done a bajillion times already. Sun has good examples.
Java 7 和更新版本的更好解决方案
我偶然发现了这个问题,并意识到一些答案非常旧且过时。 如今,JList 是通用的,因此有更好的解决方案。
我的通用 JCheckBoxList 解决方案:
要动态添加 JCheckBox 列表,您需要创建自己的 ListModel 或添加 DefaultListModel。
DefaultListModel 是通用的,因此您可以使用 JAVA 7 API 此处指定的方法像这样:
Better solution for Java 7 and newer
I stumbled upon this question and realized that some of the answers are pretty old and outdated. Nowadays, JList is generic and thus there are better solutions.
My solution of the generic JCheckBoxList:
For dynamically adding JCheckBox lists you need to create your own ListModel or add the DefaultListModel.
The DefaultListModel are generic and thus you can use methods specified by JAVA 7 API here like this:
这里只是 Rawa 对 JCheckBoxList 的一点补充。 这将添加使用空格键进行选择的功能。 如果选择了多个项目,则所有项目都将设置为第一个项目的反转值。
Here is just a little addition to the JCheckBoxList by Rawa. This will add the ability to select using space bar. If multiple items are selected, all will be set to inverted value of the first item.