[英]remove a selected row from jtable on button click
我想从java中的表中删除Selected行。 应该在按钮点击时执行该事件。 如果有人帮忙,我会感激不尽的...
例如,有一个名为sub_table的表,其中包含3列,即sub_id,sub_name,class。 当我从该表中选择其中一行并单击删除按钮时,应删除该特定行。
这很简单。
ActionListener
。 示例代码:(有2列的表)
Object[][] data = { { "1", "Book1" }, { "2", "Book2" }, { "3", "Book3" },
{ "4", "Book4" } };
String[] columnNames = { "ID", "Name" };
final DefaultTableModel model = new DefaultTableModel(data, columnNames);
final JTable table = new JTable(model);
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
JButton button = new JButton("delete");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// check for selected row first
if (table.getSelectedRow() != -1) {
// remove selected row from the model
model.removeRow(table.getSelectedRow());
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.