B
barichards21
I'm pretty new programming with swing and I'm running into a problem
setting up a table. I'm using the DefaultTableModel instead of
setting up my own because I don't need to do anything too complex with
it. I'm having a problem though when I try to remove rows. For
example if I remove the first row, the information shows up correctly
on my table and shifts everything up. But if I then try to reference
the information in the first row it gives me the information of the
row I just deleted. I'm sure it's something stupid but I can't seem
to figure it out. Here's the code I'm using at the moment:
instantiating the table:
tblPhoneNumbers = new JTable(new DefaultTableModel(
new Object[][]{{"",""}},
new Object[]{"Number", "Type"})
);
action to remove the selected row:
protected void btnRemovePhoneActionPerformed(ActionEvent evt) {
DefaultTableModel phoneDm = (DefaultTableModel)
tblPhoneNumbers.getModel();
int selectedRow = tblPhoneNumbers.getSelectedRow();
int numRows = tblPhoneNumbers.getRowCount();
if(selectedRow >= 0 && selectedRow < numRows-1)
{
phoneDm.removeRow(selectedRow);
}
}
setting up a table. I'm using the DefaultTableModel instead of
setting up my own because I don't need to do anything too complex with
it. I'm having a problem though when I try to remove rows. For
example if I remove the first row, the information shows up correctly
on my table and shifts everything up. But if I then try to reference
the information in the first row it gives me the information of the
row I just deleted. I'm sure it's something stupid but I can't seem
to figure it out. Here's the code I'm using at the moment:
instantiating the table:
tblPhoneNumbers = new JTable(new DefaultTableModel(
new Object[][]{{"",""}},
new Object[]{"Number", "Type"})
);
action to remove the selected row:
protected void btnRemovePhoneActionPerformed(ActionEvent evt) {
DefaultTableModel phoneDm = (DefaultTableModel)
tblPhoneNumbers.getModel();
int selectedRow = tblPhoneNumbers.getSelectedRow();
int numRows = tblPhoneNumbers.getRowCount();
if(selectedRow >= 0 && selectedRow < numRows-1)
{
phoneDm.removeRow(selectedRow);
}
}