我已经编写了这段代码来获取用户双击的行的索引,问题是我想让单元格不可编辑,因为我已经扩展了JTable并重写了它的isCellEditableMethod(int ,int);在我也尝试了所有已经存在的答案,但我没有得到我的结果。请帮助我只想让JTable的单元格不可编辑但可选择。
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class A extends JTable {
JFrame frame = new JFrame();
Object data[][] = {{"1","Jahanzeb"},{"2","Ahmed"},{"3","Shaikh"}};
String col[] = {"#","Names"};
DefaultTableModel tableModel = new DefaultTableModel(data, col);
JTable table = new JTable(tableModel);
JScrollPane scroll = new JScrollPane(table);
public static void main(String arg[]) {
new A();
}
public A() {
table.addMouseListener(new Click());
table.setModel(tableModel);
table.setFocusable(false);
table.setRowSelectionAllowed(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scroll);
frame.add(table);
frame.setVisible(true);
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
class Click extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2)
System.out.println(table.getSelectedRow());
}
}
}