Reputation:
nice day for all; i have table model extends AbstractTableModel add objects to jtable and its fine and work good and have this code to get objects from table
ListSelectionModel rowSM1 = carTable.getSelectionModel();
rowSM1.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
if (carTable.getSelectedRow() < 0) {
try {
throw new Exception();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
editMI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print(new CarTableModel(carsList).getCar(carTable.getSelectedRow())+"\n");
}
});
}
}
});
and its work and give me object of selected row but when select other row give me him object and repeat it according selection if second select repeat twice if third select repeat 3 time like next :
first select
Car{id=1, carLicense=CarLicense{id=1, human=Human{id=2,
second select
Car{id=3, carLicense=CarLicense{id=3, human=Human{id=2,
Car{id=3, carLicense=CarLicense{id=3, human=Human{id=2,
third select
Car{id=2, carLicense=CarLicense{id=2, human=Human{id=1,
Car{id=2, carLicense=CarLicense{id=2, human=Human{id=1,
Car{id=2, carLicense=CarLicense{id=2, human=Human{id=1,
fourth select
Car{id=1, carLicense=CarLicense{id=1, human=Human{id=2,
Car{id=1, carLicense=CarLicense{id=1, human=Human{id=2,
Car{id=1, carLicense=CarLicense{id=1, human=Human{id=2,
Car{id=1, carLicense=CarLicense{id=1, human=Human{id=2,
and so on please any solve
Upvotes: 0
Views: 578
Reputation: 319
You're adding a new ActionListener every time you select a row from the table. That's why you get that repeating output.
I can't see a lot of your code but I think the whole stuff controlling the selection of the list is superfluous. The action event is triggered within the "editMI" component itself.
So it is enough to have:
editMI.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print(new CarTableModel(carsList).getCar(carTable.getSelectedRow())+"\n");
}
});
Independently from the selection event of the table.
Upvotes: 1