Java 如何使 JTable 的单元格不可编辑但可选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24726896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make cells of JTable non-editable, but selectable
提问by user3808922
I have overridden the isCellEditable() method of class JTable in my code to make the cells of my JTable non-editable but selectable, but the cells are still editable. How do I solve this problem?
我在代码中重写了 JTable 类的 isCellEditable() 方法,使 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);
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());
}
}
}
回答by GingerHead
You are adding another JTable
in your code, you are confusing between the one extending and the other one added to the JFrame!
您JTable
在代码中添加了另一个,您在扩展的一个和添加到 JFrame 的另一个之间混淆了!
Add these invocations after fixing the above:
修复上述问题后添加这些调用:
table.setFocusable(false);
table.setRowSelectionAllowed(true);
回答by DMunz
I believe you need to override the isCellEditable() method of the TableModel rather than the JTable, like so:
我相信您需要覆盖 TableModel 而不是 JTable 的 isCellEditable() 方法,如下所示:
public class NonEditableModel extends DefaultTableModel {
NonEditableModel(Object[][] data, String[] columnNames) {
super(data, columnNames);
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
It is also possible to simply override the method using an anonymous class.
也可以使用匿名类简单地覆盖该方法。
DefaultTableModel tableModel = new DefaultTableModel(data, col) {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}
This question demonstrates how to perform the override inline, which is handy if you only need to instantiate the TableModel once: How to make a JTable non-editable
这个问题演示了如何执行内联覆盖,如果您只需要实例化 TableModel 一次,这很方便: How to make a JTable non-editable
回答by camickr
Your class extends JTable and you override the isCellEditable(...)
method.
您的类扩展了 JTable 并覆盖了该isCellEditable(...)
方法。
But then you create a new JTable that you add to the frame and you do NOT override the isCellEditable(..)
method of that JTable.
但是随后您创建了一个新的 JTable 并将其添加到框架中,并且您不会覆盖该isCellEditable(..)
JTable的方法。
If you want to extend JTable, then don't create a new JTable inside that class.
如果要扩展 JTable,则不要在该类中创建新的 JTable。