Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

valueChange in a ListSelection when i

 
ben chato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi !
I've got a problem with my JTable.
Normally, when we click on a row, its background becomes blue (it is now selected !)
But, when the cellEditor is a JComboBox, even if i change the value in the 3rd row, table.getSelectedRow() still returns 0. (and the first row is still blue).
How can i get the row index of the JComboBox i clicked and force the selectedRow ?
thanks !
Ben
------------------
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a listener to the JComboBox. When its value changes, update your table to select the row in which the JComboBox resides. The problem is trying to determine the row number that the JComboBox is in, because that's the row that you have to select after the JComboBox's value changes.
SAF
 
ben chato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes... that's the problem ! :-)
i'm still searching...
Ben
 
SAFROLE YUTANI
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're adding the JComboBox's dynamically, meaning that your application will add a variable amount of JComboBox's to your JTable, then just subclass JComboBox and add a new attribute that stores the row number in which the combox box is located in your JTable. Add a method to your new class that will allow the JTable to get the row number from the combo box, then you will know the row to select.
SAF
 
ben chato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, it could be a good idea but i think it won't work.
My JComboBox are not placed dynamically : i use them as a DefaultCellEditor for my Column.
Ben
 
ben chato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally found the answer :
1- I placed a mouseListener on the JTable
2- When i click on a cell, the mousePressed tell me the point.
3- I get the rowAtPoint()
4- I send the focus on the row with :
try {
table.editCellAt(line,0);
table.getEditorComponent().requestFocus();
lsm = table.getSelectionModel();
lsm.setSelectionInterval(line,line);
} catch (Exception ex) {
...
}
that's all !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic