双击编辑Jtable

时间:2015-05-18 21:43:34

标签: java swing jtable defaulttablemodel

我需要在用户双击行时使用新值编辑表格。我尝试使用setValuesAt方法,但它没有做任何事情。

这是我到目前为止所得到的,但我不知道如何将新值设置为Jtable

 public class Table extends JFrame {

        private JTable table;
        private JScrollPane jsPane;
        private DefaultTableModel model;
        private JPanel dialogPanel;
        private JTextField tf[];
        private JLabel lbl[];
        private JPanel panel;

        Object[] columns = new Object[]{"Name", "Last Name", "ID", "Email"};
        Object[][] inData ;

        public void prepareAndShowGUI() {

            setTitle("Overview");

            model = new DefaultTableModel() {

                @Override
                public boolean isCellEditable(int row, int column) {
                    return false;
                }

            };

            model.setColumnIdentifiers(columns);

            table = new JTable(model);

            for (int i = 1; i <= 10; i++) {
                model.addRow(new Object[]{"a", "s", "w", "e"});
            }

            jsPane = new JScrollPane(table);

            table.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {

                        int x = e.getX();
                        int y = e.getY();
                        int row = table.rowAtPoint(new Point(x, y));
                        int col = table.columnAtPoint(new Point(x, y));

                        String array[] = new String[table.getColumnCount()];
                        for (int i = 0; i < array.length; i++) {
                            array[i] = (String) table.getValueAt(row, i);
                        }
                        populateTextField(array);
                        JOptionPane.showMessageDialog(null, dialogPanel, "Information", JOptionPane.INFORMATION_MESSAGE);

                        String[] values = new String[tf.length];
                        for (int i = 0; i < tf.length; i++) {
                            values[i] = tf[i].getText();

                        }
                        model.setValueAt(values, row, row);

                    }

                }
            });

            panel = new JPanel(new BorderLayout());

            JPanel panel1 = new JPanel();
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));

            panel.add(panel1, BorderLayout.NORTH);
            panel.add(jsPane, BorderLayout.CENTER);
            getContentPane().add(panel);

            pack();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            prepareDialogPanel();

            setVisible(true);
        }

        private void prepareDialogPanel() {

            dialogPanel = new JPanel();
            int col = table.getColumnCount();
            dialogPanel.setLayout(new GridLayout(col, 1));
            tf = new JTextField[col];
            lbl = new JLabel[col];

            for (int i = 0; i < col; i++) {
                lbl[i] = new JLabel(table.getColumnName(i));
                tf[i] = new JTextField(10);

                dialogPanel.add(lbl[i]);
                dialogPanel.add(tf[i]);

            }
        }

        private void populateTextField(String[] s) {
            for (int i = 0; i < s.length; i++) {
                tf[i].setText(s[i]);

            }
        }

    }
 public static void main(String st[])
    {
        SwingUtilities.invokeLater( new Runnable()
        {
            @Override
            public void run()
            {
                Table td = new Table();
                td.prepareAndShowGUI();
            }
        });

}

1 个答案:

答案 0 :(得分:3)

对于初学者,您应该使您的模型可编辑,因此请更改此行:

model = new DefaultTableModel() {

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

};

要:

model = new DefaultTableModel() {

    @Override
    public boolean isCellEditable(int row, int column) {
        return true; //use the row and col to determine which 
                    //cells are editable. If you want all, have this return true. 
    }

};

这将调用JTable的DefaultCellEditor,当用户对编辑进行更改时,它将调用模型的setValueAt方法。

可以使用自定义组件替换所有这些组件以执行不同的操作。

以下是有关JTables的官方Oracle文档:

https://docs.oracle.com/javase/tutorial/uiswing/components/table.html

如果您尝试让对话框正常工作,问题出在以下几行:

String[] values = new String[tf.length];
for (int i = 0; i < tf.length; i++) {
    values[i] = tf[i].getText();
}
model.setValueAt(values, row, row);

基本上,setValueAt仅适用于逐个单元格。你不能像这样更新整行。而是尝试:

for(int i=0;i<tf.length;i++)
{
   model.setValueAt(tf[i].getText(), row, i); 
}