I am working on a JTable that has one column that contains a List<List<String>>. This column should show a button to the user. When he clicks on the Button, something is to be done with the data of the cell.
All that works really fine with the following code:
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);
Where ButtonColumn is taken from here and where the TableModel is implemented as follows:
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);
}
}
Question is: How to I change the Caption of the Button? Right now, the current data of the cell is also displayed as caption of the Button, which is not very user-friendly. I would like all buttons to bear the caption “Edit”, how can I realize this?
That is done in the
ButtonColumnclassIf you just set “Edit” as text instead of
value.toString()all buttons will show ‘Edit’This has to be done in the method
getTableCellRendererComponent. The same has to be done for theeditButtonin the methodgetTableCellEditorComponent, otherwise the value is still shown on the button, while the user performs a click on it.