Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Using KeyListener with JTable

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having problems getting the same value for JTable.getSelectedRow() when using KeyEvents and MouseEvents. If I click on the very first table row, the value 0 is reported. However, if I then use the down key to get to row 1 (the second row in reality) getSelectedRow() reports 0 again (if I use the mouse to get to this row getSelectedRow reports 1 - which is correct). It gets even stranger when I then use the up key to get back to the first row in the table, getSelectedRow reports 1!! I'm unsure what is going on here - has anyone experienced this before? The MouseEvent reports back correctly, bu the KeyEvent isn't.
 
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this is a feature - the row being returned is the row that was selected *before* the KeyEvent was processed. So you press down, row 0 was previously selected. When you press up again, row was 1 selected. I don't know why this would happen though.

Do repeated calls to getSelectedRow() (without further keyevents) return the same value? Perhaps there is a delay in the event processing.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u should use mouseListener like this

table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
int X = e.getX();
int Y = e.getY();

int currentCol = getCurrentColumn(X,Y);
int currentRow = getCurrentRow(X,Y);
String str = getTableValueAt(cRow,cCol);
}
}
don't use getselectedrow, first dwtwct column and row then fetch a value when user click any row, i hope now ur problem get sorted off.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But that won't help with the issue of keypresses, just mouse clicks.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use keyReleased() instead of keyPressed()
 
Ste Graham
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thanks for the help!! D'oh! I was catching the selected row (Jtable.getSelectedRow()) within the keyPressed() method. Of course when you press the key you start at the row you at, even though if you are pressing down you end up somehwere else. I did indeed use the keyReleased() method and this returned the value I was looking for. Once again thank you!
reply
    Bookmark Topic Watch Topic
  • New Topic