*/
public static JPanel createTableModelModifier(final DefaultTableModel tableModel) {
JPanel tableModelPanel = new JPanel(new BorderLayout(6, 6));
final JTable table = new JTable(tableModel);
tableModelPanel.add(new JScrollPane(table));
ButtonPanel buttonPanel = new ButtonPanel();
JButton insert = new JButton("Insert");
insert.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
Vector rowData = tableModel.getDataVector();
int index = table.getSelectedRow();
if (index != -1) {
Vector v = (Vector) rowData.get(index);
Vector clone = new Vector();
for (int i = 0; i < v.size(); i++) {
if (i == 0) {
clone.add((int) (Math.random() * 10));
}
else {
clone.add("" + v.get(i));
}
}
tableModel.insertRow(index, clone);
}
}
});
JButton delete = new JButton("Delete");
delete.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
int[] rows = table.getSelectedRows();
for (int i = rows.length - 1; i >= 0; i--) {
int row = rows[i];
tableModel.removeRow(row);
}
}
});
JButton clear = new JButton("Clear");
clear.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < tableModel.getRowCount(); i++) {
tableModel.removeRow(0);
}
}
});
buttonPanel.add(insert);
buttonPanel.add(delete);
buttonPanel.add(clear);
tableModelPanel.add(buttonPanel, BorderLayout.AFTER_LAST_LINE);
return tableModelPanel;
}