本文介绍了我可以将 JRadioButton 添加到 JTable 中吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 CellEditorCellRendererJRadioButton 添加到 JTable 中,但我无法添加JRadioButtonJTable.我正在使用 NetBeans 和后端 MySQL.请帮帮我.

I tried to add JRadioButton into JTable by using CellEditor and CellRenderer, but I can't add JRadioButton into JTable. I am using NetBeans and backend MySQL. Please help me.

谢谢,但我不知道如何对 JRadioButton 进行分组.你能帮助我吗?我有 4 列.第一列单元格包含项目名称,第二列单元格包含数量,第 3 和第 4 列单元格包含 JRadio 按钮.然后我想将包含 JRadio 按钮的第 3 和第 4 列单元格分组在每行

Thank you, but I have no idea about how to group JRadioButton. Can you help me?I have 4 columns .First column cell containing item name , second column cell containing quantity,3rd and 4th column cells contiaining JRadio Buttons.Then I want to grouping 3rd and 4th column cells containing JRadio Buttons in each row

如果我尝试使用这个在 jTable 的自定义代码中添加单选按钮,

If I try to add radiobutton in the customize code of jTable by using this,

new JRadioButton("a") ,那么它会变成,javax.swing.JRadioButton[,0,0,0x0,invalid,alignmentX=0.0,.....text=a], 在列而不是按钮

new JRadioButton("a") , then it'l come as, javax.swing.JRadioButton[,0,0,0x0,invalid,alignmentX=0.0,.....text=a], in the column instead of button

推荐答案

不清楚你想如何在 JTable 中使用 JRadioButton;考虑这些替代方案:

It's not clear how you want to use JRadioButton in a JTable; consider these alternatives:

  • 使用 SINGLE_SELECTION 模式选择单个行.

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

  • 使用 Boolean.class 类型的列,它将使用 JCheckBox 呈现.此示例将选择限制为单行.

  • Use a column of type Boolean.class, which will be rendered using a JCheckBox. This example limits selections to a single row.

    使用 JComboBox 作为 编辑器 用于一行内的互斥选择.

    Use a JComboBox as an editor for mutually exclusive choices within a row.

    附录:如果需要 JRadioButton,可以将它们添加到 JPanel,如下面的 示例由于@mKorbel.

    Addendum: If JRadioButton is required, they can be added to a JPanel, as shown in this example due to @mKorbel.

    附录:如果每个 JRadioButton 都有自己的列,则不能使用 ButtonGroup 因为单个按钮用于所有具有相同 渲染器.您可以从您的TableModel更新同一行中的其他按钮,这应该覆盖setValueAt()以强制执行单选规则通常由 ButtonGroup 管理.有一个例子这里.

    Addendum: If each JRadioButton has its own column, you can't use a ButtonGroup because a single button is used for all cells having the same renderer. You can update other button(s) in the same row from your TableModel, which should override setValueAt() to enforce the single-selection rule that is usually managed by the ButtonGroup. There's an example here.

    附录:这个由@Guillaume Polet 提供的示例说明了一种管理每行一个单选按钮的方法.

    Addendum: This example due to @Guillaume Polet illustrates a way to manage one radio button per row.

    这篇关于我可以将 JRadioButton 添加到 JTable 中吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-12 04:19