获取jtable中的勾选复选框的行值 [英] Get row values of ticked checkbox in jtable

查看:1113
本文介绍了获取jtable中的勾选复选框的行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class TableModel extends AbstractTableModel {

        Object rowData[][] = DataAccess.getSentences();
        String columnNames[] = {"Category", "Sentences", "Boolean"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }

        public Class getColumnClass(int column) {
            return (getValueAt(0, column).getClass());
        }

        public void setValueAt(Object value, int row, int column) {
            rowData[row][column] = value;
        }

        public boolean isCellEditable(int row, int column) {
            return (column >= 2);
        }
    }

范例:

标题:Category,Sentences,Boolean

Headers: Category, Sentences, Boolean

Row1:apple |这是一个苹果|复选框

Row1: apple | this is an apple | checkbox

Row2:cat |这是一只猫|复选框

Row2: cat | this is a cat | checkbox

当我选中第一行时。它会System.out.println()的苹果和这是一个苹果。

When I ticked the first row. It will System.out.println() the "apple" and "this is an apple."

如果你有很多时间,我会欣赏,如果有一个代码片段。谢谢。

If you have much time, i would appreciate if there's a code snippet. Thank you.

推荐答案

CheckABunch 是一个可能让您开始使用的示例:

CheckABunch is an example that may get you started:


  • 如果 getColumnClass()对于列返回 Boolean.class ,则默认渲染器和编辑器将是 JCheckBox

  • If getColumnClass() returns Boolean.class for a column, the default renderer and editor will be a JCheckBox.

对于 AbstractTableModel ,您的实现 setValueAt c $ c>必须触发相应的事件,如此处此处

添加 TableModelListener 您的模型触发的事件,如此处所示。

Add a TableModelListener to receive events fired by your model, as shown here.

这篇关于获取jtable中的勾选复选框的行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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