我可以将JRadioButton添加到JTable中吗? [英] Can I add JRadioButton into JTable

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

问题描述

我尝试使用 CellEditor JRadioButton 添加到 JTable c>和 CellRenderer ,但我无法将 JRadioButton 添加到 JTable 。我正在使用NetBeans和后端MySQL。请帮帮我。



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



编辑:如果我尝试使用此方法在jTable的自定义代码中添加radiobutton,



新的JRadioButton(a),然后它来了, javax.swing.JRadioButton [,0,0,0x0,无效,alignmentX = 0.0,..列中的... text = a],而不是按钮

解决方案

目前尚不清楚你想在 JTable 中使用 JRadioButton ;考虑以下替代方案:




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

      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 


  • 使用类型的列Boolean.class ,将使用 JCheckBox 进行渲染。此示例将选择限制为单行。


  • 使用 JComboBox 作为编辑器,用于

  • 附录: JRadioButton 是必需的,它们可以添加到 JPanel ,如下所示示例由于@mKorbel。


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


  • 附录:此由于@Guillaume Polet,示例说明了每行管理一个单选按钮的方法。



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.

Edit: 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

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

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

解决方案

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

  • Use SINGLE_SELECTION mode to select individual rows.

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    

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

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

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

  • 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.

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

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

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