简体   繁体   English

双击编辑Jtable

[英]Edit Jtable on double click

I need to edit table when user double clicks on row, with new values. 当用户双击具有新值的行时,我需要编辑表。 I tried with setValuesAt method but it didn't do anything. 我尝试使用setValuesAt方法,但没有执行任何操作。

This is what I got so far but I don't know how to set new values to Jtable . 到目前为止,这是我得到的,但是我不知道如何为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();
            }
        });

}

For starters, you should make your model editable, so change this line: 对于初学者,您应该使模型可编辑,因此更改此行:

model = new DefaultTableModel() {

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

};

To: 至:

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. 
    }

};

This will invoke the JTable's DefaultCellEditor, which will then call on the model's setValueAt method, when the user has made the change on the edit. 这将调用JTable的DefaultCellEditor,当用户在编辑中进行了更改时,该DefaultCellEditor将调用模型的setValueAt方法。

All of these components can be replaced with custom components to perform different actions. 所有这些组件都可以替换为自定义组件以执行不同的操作。

Here's the official Oracle documentation on JTables: 这是有关JTables的Oracle官方文档:

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

If you're trying to get your dialog to work, the problem is in these line: 如果您要使对话框正常工作,则问题在于以下几行:

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

Basically the setValueAt only works on a cell by cell basis. 基本上,setValueAt仅在逐个单元的基础上工作。 You can't update a whole row like this. 您不能像这样更新整行。 Instead try: 而是尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM