如何在jtable中增加一行单选

linpyi 2008-04-22 09:26:01
各位大虾,小弟对swing不熟,请教个问题



写了一个jtable,

MyTableModel tableModel = new MyTableModel();
JTable table = new JTable(tableModel);


class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "First Name", "Last Name", "Sport",
"# of Years", "Vegetarian" };

private Object[][] data = {
{ "Mary", "Campione", "Snowboarding", new Integer(5),
new Boolean(false) },
{ "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
{ "Kathy", "Walrath", "Knitting", new Integer(2),
new Boolean(false) },
{ "Sharon", "Zakhour", "Speed reading", new Integer(20),
new Boolean(true) },
{ "Philip", "Milne", "Pool", new Integer(10),
new Boolean(false) } };

public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}

public int getRowCount() {
// TODO Auto-generated method stub
return data.length;
}

public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return data[rowIndex][columnIndex];
}


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

}



public void setValueAt(Object value, int row, int col) {

data[row][col] = value;
fireTableCellUpdated(row, col);
}

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

public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
System.out.println(col);
System.out.println(row);
if (col < 2) {
return false;
} else {
return true;
}
}


}


在每一行的末尾都会有一个checkbox,复选框

我想再第一行,为每列加一个复选框来确定这列是否被选中,怎么增加
...全文
247 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bradwoo8621 2008-04-29
  • 打赏
  • 举报
回复
Data数组第一列是boolean值就行了吧, 和你最后一列一样.
linpyi 2008-04-23
  • 打赏
  • 举报
回复
up
linpyi 2008-04-23
  • 打赏
  • 举报
回复
up

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧