我正在研究一个 JTable,它有一列包含List<List<String>>
. 此列应向用户显示一个按钮。当他单击按钮时,将对单元格的数据进行一些处理。
使用以下代码,所有这些都可以正常工作:
JFrame testFrame = new JFrame("TEST");
testFrame.setLayout(new BorderLayout() );
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
List<List<String>> list = new ArrayList<List<String>>();
list.add(new ArrayList<String>());
list.get(0).add("Hello World");
OptConstraintTableModel dm = new OptConstraintTableModel(
new String[] {"Data with a button"}, new Class<?>[] {List.class}, new Object[][] { {list} }
);
JTable table = new JTable(dm);
Action action_show = new AbstractAction()
{
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int modelRow = Integer.valueOf( e.getActionCommand() );
System.out.println(((List<List<String>>) table.getValueAt(modelRow,0)).get(0).get(0));
}
};
ButtonColumn button_show = new ButtonColumn(table, action_show, 0);
testFrame.add(table);
testFrame.pack();
testFrame.setVisible(true);
ButtonColumn
取自这里的哪里以及 TableModel 在哪里实现如下:
public class OptConstraintTableModel extends AbstractTableModel {
/**
* Default Serial Version UID
*/
private static final long serialVersionUID = 1L;
private String[] columnNames;
private List<Object[]> data;
private Class<?>[] values;
/**
* @param objects2
* @param objects
*
*/
public OptConstraintTableModel(String[] columnNames, Class<?>[] values, Object[][] data) {
this.columnNames = columnNames;
this.values = values;
this.data = new ArrayList<Object[]>();
for(Object[] row : data) {
this.data.add(row);
}
}
/**
* Adds the given Object array as last row into the TableModel
* @param objects the row to be added
*/
public void addRow(Object[] objects) {
this.data.add(objects);
this.fireTableRowsInserted(data.size()-1, data.size()-1);
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getColumnCount()
*/
@Override
public int getColumnCount() {
return columnNames.length;
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getRowCount()
*/
@Override
public int getRowCount() {
return data.size();
}
/* (non-Javadoc)
* @see javax.swing.table.TableModel#getValueAt(int, int)
*/
@Override
public Object getValueAt(int row, int col) {
return data.get(row)[col];
}
/*
* (non-Javadoc)
* @see javax.swing.table.AbstractTableModel#setValueAt(java.lang.Object, int, int)
*/
public void setValueAt(Object aValue, int row, int column) {
data.get(row)[column] = aValue;
}
/*
* (non-Javadoc)
* @see javax.swing.table.AbstractTableModel#getColumnClass(int)
*/
public Class<? extends Object> getColumnClass(int c) {
return values[c];
}
/*
* (non-Javadoc)
* @see javax.swing.table.AbstractTableModel#getColumnName(int)
*/
public String getColumnName(int col) {
return columnNames[col];
}
/*
* (non-Javadoc)
* @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
*/
public boolean isCellEditable(int row, int col) {
if (col == 1) return (Boolean) this.getValueAt(row, 0);
if (col == 3) return (Boolean) this.getValueAt(row, 2);
return true;
}
/**
* Removes the indicated row from the table model
* @param i the index of the row to be delete
*/
public void removeRow(int i) {
this.data.remove(i);
this.fireTableRowsDeleted(i, i);
}
}
问题是:如何更改按钮的标题?现在,单元格的当前数据也显示为按钮的标题,这不是很人性化。我希望所有按钮都带有“编辑”标题,我该如何实现?