Problems with JTable when i'm use ListSelectionListener and KeyListener
807607Nov 28 2006 — edited Nov 28 2006Hi!
I need to know the updated row of position when used the button of keyboard (VK_DOWN & VK_UP) in my JTable, the form is the next:
public class SelectionListener implements ListSelectionListener {
JTable table;
// It is necessary to keep the table since it is not possible
// to determine the table from the event's source
SelectionListener(JTable table) {
this.table = table;
}
public void valueChanged(ListSelectionEvent e) {
// If cell selection is enabled, both row and column change events are fired
if (e.getSource() == table.getSelectionModel()
&& table.getRowSelectionAllowed()) {
// Row selection changed
// firstIndex & lastIndex are global variables that i'm used to know
// what the row modify of position
firstIndex = e.getFirstIndex();
lastIndex = e.getLastIndex();
} else if (e.getSource() == table.getColumnModel().getSelectionModel()
&& table.getColumnSelectionAllowed() ){
System.out.println("Estoy moviendome entre columnas!!!");
// Column selection changed
int first = e.getFirstIndex();
int last = e.getLastIndex();
}
if (e.getValueIsAdjusting()) {
// The mouse button has not yet been released
}
}
}
MyKeyListener is implements thus:
public void keyPressed(KeyEvent evt) {
...........
if (evt.getKeyCode() == KeyEvent.VK_ENTER ||
evt.getKeyCode() == KeyEvent.VK_DOWN ||
evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN){
rowToChoose = lastIndex ;
}
else { // VK_PAGE_UP & VK_UP
rowToChoose = firstIndex ;
}
}
}
The problem stays in the event KeyListener is produced before that ListSelectionListener...and the rows returned, they are not been worth.
How could I fix the problem ?
could it handle the first event with listselectionlistener and the next with keylistener ?
Others ?
Thanks