无法从JTable更新单元格

时间:2014-11-21 01:04:07

标签: java jtable

我一直试图找到一种通过直接输入更新我的细胞的方法。从我的回顾中,每个人似乎都说你必须使用

myJTable.fireTableCellUpdated()

尽管如此,它一直要求我施放myJTable - >它不能被转换为AbstractTableModel,根据我的理解,这是必需的。

这是我目前的表格代码,如果这可以有所帮助:

    myJTable= new JTable();

    myJTable.setModel(new DefaultTableModel(
        new Object[][] {
            {null, null, null},
        },
        new String[] {
            "Thing#1", "Thing#2", "Thing#3"
        }
    ));

    myJTable.getModel().addTableModelListener(new TableModelListener() { 
         public void tableChanged(TableModelEvent e) { 
             myJTable.fireTableCellUpdated(e.getFirstRow(), e.getColumn());
             myJTable.repaint(tblCG.getCellRect(e.getFirstRow(), e.getColumn(), false));
             myJTable.putClientProperty("terminatedEditOnFocusLost", true);
         } 
         });

如何从通过用户直接输入更新的单元格中保存数据? 谢谢!

2 个答案:

答案 0 :(得分:1)

根据您的目的,您可以使用...

例如......

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    private JTable myJTable;

    private String values[] = {"Orange", "Green", "Blue", "Pink", "Yellow"};
    private int count;

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                myJTable = new JTable();

                myJTable.setModel(new DefaultTableModel(
                                new Object[][]{{"Banana", "Yellow"}},
                                new Object[]{"Fruit", "Color"}
                ));

                JButton change = new JButton("Change");
                change.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        count++;
                        String value = values[Math.abs(count % values.length)];
                        myJTable.setValueAt(value, 0, 1);
                    }

                });
                JButton add = new JButton("add");
                add.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        DefaultTableModel model = (DefaultTableModel) myJTable.getModel();
                        model.addRow(new Object[]{"Apple " + model.getRowCount(), "Green"});
                    }

                });
                JButton delete = new JButton("delete");
                delete.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int rowIndex = myJTable.getSelectedRow();
                        rowIndex = myJTable.convertRowIndexToModel(rowIndex);
                        if (rowIndex > 0) {
                            DefaultTableModel model = (DefaultTableModel) myJTable.getModel();
                            model.removeRow(rowIndex);
                        }
                    }

                });

                JPanel actions = new JPanel();
                actions.add(change);
                actions.add(add);
                actions.add(delete);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(myJTable));
                frame.add(actions, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

如果您已经以某种方式自己定制了类,并且只能从类上下文中调用它们,那么您应该只需要调用一个帮助fireXxx事件方法,它们不应该在外部触发,它&# 39;实施班负责这样做

所以,没有额外的代码......

Editing

默认内置单元格编辑...

请参阅...

了解更多详情...

答案 1 :(得分:0)

这是我的建议(希望我理解正确的问题):

  1. 我会声明作为类属性传递给DefaultListModel的数据字段。然后,您可以通过这些属性访问表格中的对象。

  2. 我会覆盖隐式构造函数中的DefaultTableModel#setValueAt(Object, int, int)DefaultTableModel#getValueAt(int, int)

  3. 请尝试以下解决方案:

    //declared as class attributes
    Object[][] data = {{null, null, null},};
    String[] columns = {"Thing#1", "Thing#2", "Thing#3"};
    
    /*...*/
    
    table.setModel(new DefaultTableModel(data, columns) {
        @Override
        public Object getValueAt(int row, int col) { return data[row][col]; }
        @Override
        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
    }); 
    
    希望这可以帮助......