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.