所以我试图实现一个动态可编辑的列表,当我点击最后一行或编辑最后一行时,我想动态添加一行。尽管我想知道如何实现 actionlistener,但我知道如何添加行。帮助将不胜感激。
import java.awt.BorderLayout;
import java.util.Date;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class EditListAction {
public static void main(String args[]) {
TableModel model = new AbstractTableModel() {
Object rowData[] = {"English","hindi","Spanish","Russian" };
String columnName = new String("Languages");
public int getColumnCount() {
return 1;
}
public String getColumnName() {
return columnName;
}
public int getRowCount() {
return rowData.length;
}
public Object getValueAt(int row,int col) {
return rowData[row];
}
public Class getColumnClass(int column) {
return (getValueAt(0, 1).getClass());
}
public void setValueAt(Object value, int row,int column) {
if(column==0){
rowData[row] = value;
}
}
public boolean isCellEditable(int row, int column) {
return (column == 0);
}
};
JFrame frame = new JFrame("Column Renderer Table");
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}