1

I have a JTable in my program and I want to allow the user to update the table on click of a button.

public void popUpWindow(JTable t)
 {
            JFrame frame=new JFrame();

            DefaultTableModel dtm=new DefaultTableModel(data,columnNames);
            t.setModel(dtm);

            JButton btnEdit=new JButton("Edit");
            JButton btnUpdate=new JButton("Update");
            btnUpdate.setEnabled(false);
            JButton btnDelete=new JButton("Delete");

            btnEdit.setBounds(150, 220, 100, 25);
            btnUpdate.setBounds(150, 260, 100, 25);
            btnDelete.setBounds(150, 300, 100, 25);
            JScrollPane jsp=new JScrollPane(t);
            jsp.setBounds(0, 0, 880, 200);
            frame.setLayout(null);

            frame.add(jsp);

                    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
            jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

            frame.add(btnEdit);
            frame.add(btnUpdate);
            frame.add(btnDelete);
            frame.setSize(900,400);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            int row,column;
            btnEdit.addActionListener(new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {
                      t.setEnabled(true);
                      btnUpdate.setEnabled(true);
                    }
                }); 

            btnUpdate.addActionListener(new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {                                           
                        if(t.isEditing())
                        {
                            row=t.getEditingRow();
                            column=t.getEditingColumn();
                            // System.out.println("row : "+row+" column : "+column);
                            Object s=dtm.getValueAt(row, column);
                            dtm.setValueAt(s, row, column);
                            dtm.fireTableCellUpdated(row,column);
                            // System.out.println(s);
                      }

                    }
                }); 
 }

Whenever , I click on the "Update" button, the content of the cell is not updated. What could be the reason behind this?

Functioning of "update" button : Whenever a user clicks on it, jtable enters into an editing mode and the user can edit the already populated content of jtable. I want to store the edited cell value in a particular variable and I have chosen variable s for that purpose, i.e., "s" will store the edited content of the cell.

ninja coder
  • 27
  • 1
  • 8
  • Why are you using `t.isEditing()`? won't that be false if you leave the cell to press a JButton? That just looks sketchy to me, that and that you're changing the data in the re-extracting the table model, and then firing the notification method on the dtm variable. They look to be one and the same, but still, it looks funny. As an aside, that top level window should be a JDialog **not** a JTable. – Hovercraft Full Of Eels Mar 23 '16 at 21:04
  • I am pretty sure `isEditing()` is when you are currently editing a cell in the table which you can't do when the focus is pressing a button. You might have more luck with getting the selected cell `table.getSelectedRow()` and `table.getSelectedColumn()`. Also it seems that you are just setting the same object to the same cell without changing it. – Matthew Wright Mar 23 '16 at 21:05
  • @Matthew I tried with table.getSelectedRow() and table.getSelectedColumn(). Also, I have removed the isEditing() condition, but it still doesn't work. – ninja coder Mar 23 '16 at 21:10
  • Add a `System.out.println("Update at ["+row+", "+column+"]");` in there just to be sure it is actually running. But from what I said before, it might not look like anything is happening because you don't do anything with `Object s`. You are getting the value and setting it right back. – Matthew Wright Mar 23 '16 at 21:12
  • @Matthew I did add System.out.println("row : "+row+" column : "+column); to see if it's actually working. And, yeah the output window shows that it is working. How can I actually store the edited content in Object s? – ninja coder Mar 23 '16 at 21:17
  • I'm not sure I understand your goal. What do you want to do when you select a cell and press update? You aren't using `Object s` in a meaningful way. – Matthew Wright Mar 23 '16 at 21:21
  • Sorry for the misunderstanding. This is how the "update" button functions: Whenever a user clicks on it, jtable enters into an editing mode and the user can edit the already populated content of jtable. I want to store the edited cell value in a particular variable and I have chosen variable s for that purpose, i.e., "s" will store the edited content of the cell. – ninja coder Mar 23 '16 at 21:25
  • I'm not sure on the point. Aren't the cells of the JTable already editable? If you want to do something when a cell is changed by the user then this [thread might point you in the right direction](http://stackoverflow.com/questions/6889694/jtable-detect-cell-data-change) – Matthew Wright Mar 23 '16 at 21:39

0 Answers0