我想知道是否有办法,如何在Jtable中禁用双击可编辑单元格,但可以选择单个行。
因为我要创建一个按钮来打开另一个表单来编辑表中的所选行。
谢谢。
这是我的代码
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MainWindow extends JFrame{
private JFrame frame;
private JTable table;
private JButton btnEdit;
public MainWindow() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 780, 615);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Main Window");
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 23, 744, 476);
frame.getContentPane().add(scrollPane);
///Sample DATA
Object[][] Data = {
{"John", "18", "Male"},
{"Mull", "19", "Male"},
{"Shaine", "20", "Female"},
{"Baine", "30", "Male"},
{"Sussie", "40", "Female"},
};
Object[] Columns = {"Name", "Age","Gender" };
//
DefaultTableModel model = new DefaultTableModel(Data,Columns);
table = new JTable(model);
table.setEnabled(false);
scrollPane.setViewportView(table);
btnEdit = new JButton("Edit");
btnEdit.setBounds(215, 527, 89, 23);
frame.getContentPane().add(btnEdit);
}
}