JTable help

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

SadRed

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);
}
}
if I then try to reference
the information in the first row
I suspect the code is wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top