Strahinja Ivkovic
Strahinja Ivkovic

Reputation: 15

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.

This is what I got so far but I don't know how to set new values to 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();
            }
        });

}

Upvotes: 0

Views: 3111

Answers (1)

lordoku
lordoku

Reputation: 1112

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.

All of these components can be replaced with custom components to perform different actions.

Here's the official Oracle documentation on JTables:

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

Upvotes: 3

Related Questions