各位大虾,小弟对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,复选框我想再第一行,为每列加一个复选框来确定这列是否被选中,怎么增加
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,复选框我想再第一行,为每列加一个复选框来确定这列是否被选中,怎么增加
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货