在运行时禁用JTable单元格上的编辑。 [英] Disable Editing on JTable cell in runtime.

查看:88
本文介绍了在运行时禁用JTable单元格上的编辑。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编辑的表格,当我放置值时,在表格的单元格中,当单元格编辑仍然打开时,它不会通过方法getValueAt()得到值....

它只获取值通过getValueAt()禁用单元格编辑时..



为此我想自动禁用JTable单元格的编辑...



谢谢你们..

Vishal!

I have a editable table, In cell of table when i put value then it not get value through the method getValueAt() when the cell editing still on....
it only get value through getValueAt() when the cell editing is disable..

For that i want automatically disable editing of the JTable cell...

Thanks regards..
Vishal!

推荐答案

你可以使用TableModel。



喜欢这个

You can use a TableModel.

Like this
public class MyModel extends AbstractTableModel{

}



然后将模型设置为JTable。


Then set the model into your JTable.

JTable myTable = new JTable();
myTable.setModel(new MyModel());


这是另一种方式..

Here is another way..
JTable table = new JTable(){  
       public boolean isCellEditable(int row,int column){  
         Object o = getValueAt(row,column);  
         if() return false;  
         return true;  
       }  
     }; 


它的工作方式如你所愿。单击禁用按钮后,单元格编辑将停止。

Its work as you want. After click the disable button cell editing will stop.
public class TestTable {

    boolean flag = false;

    public void create() {
        JTable table = new JTable(2, 2) {

            public boolean isCellEditable(int row, int column) {
                if (flag) {
                    return false;
                }
                return true;
            }
        };
        JFrame f = new JFrame();
        JButton button = new JButton("Disable");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                flag = true;
            }
        });
        f.setLayout(new FlowLayout());
        f.add(new JScrollPane(table));
        f.add(button);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new TestTable().create();
            }
        });
    }


这篇关于在运行时禁用JTable单元格上的编辑。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆