This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes JTable cell is selected but i want the entire row to be selected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTable cell is selected but i want the entire row to be selected" Watch "JTable cell is selected but i want the entire row to be selected" New topic
Author

JTable cell is selected but i want the entire row to be selected

Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
Hello all,
i have a few fields which receive focus when i press Tab, beneath these fields i have a JTable. The first cell in the JTable receives focus when i press Tab and looses focus when i press Tab again. Instead of this single cell to be selected i want the entire row to be selected instead.
How can this be accomplished?
Thanks a lot!!
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
Try this:
table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION)
Is that what you want? It might help to describe WHY you want the sort of behavior you're looking for.
Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
Well i have set the mode to single selection. What i want is when i tab through the different fields that once i have focus on the JTable it selects the first row. I have accomplished that it sets focus to the JTable only once (the first row) and then sets focus to one of the other fields, but it now only has focus on the first cell in the first row and not the entire row.

I want to have focus on the entire row when pressing tab to set focus on the JTable. I want this because then when i tab through my application and have focus on my JTable i can proceed by pressing the arrow down.
Hope this clears things??
Thanks for the help!!
Dhanush Dharsh
Ranch Hand

Joined: Oct 30, 2000
Posts: 33
Hi Ling,
I think it would be a better approach to have your own implementation of ListSelectionModel and ListSelectionListener used in JTable. This would give you the flexibility to have your own way to implementing selecting stuff in the table.
-Dhanush
Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
Hi,
i have my own listselectionmodel, but how can i make it select an entire row if a single cell is selected(this is only the case when i get focus on the table by pressing Tab, othwerwise(mouseclick) is selects the entire row), or how can i make sure that when Tab is pressed an entire row is selected instead of a single cell.
Hope this makes my question more clear...
Please help me out on this one...

Thanks!!
[ April 20, 2004: Message edited by: Ling Chung ]
Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
Guys please help me out on this one...

Thanks!!
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
[the requirements seem so arbitrary, it reminds me of times when users tell the programmers how to program]
if you want to never allow a cell to be selected or have focus, it sounds like your table is not editable. let's go on that assumption.
however, you need the rows to be selectable in order to fire some event to do something related to the data in the row being selected.
but, you are snagged because when you hit tab to enter the table, instead of highlighting the whole row, it highlights the row and puts the focus on the first cell. and when you arrow down, the next row highlights, but the focus is still on the first cell.
but (let's fantasize for a moment) the row is correctly being "selected" and "highlighted" but the fact that one of the cells "has focus" is causing that cell to "seem selected", and that what you really want to solve is how to make it seem to the user that the row is the only thing selected, and not the cell.
how close is that to your problem? i'm just restating it because i can't understand your problem as you stated it--no point going down blind alleyways.
Ling Chung
Ranch Hand

Joined: Mar 29, 2004
Posts: 39
Originally posted by maggie woo:
however, you need the rows to be selectable in order to fire some event to do something related to the data in the row being selected.

That's correct

but, you are snagged because when you hit tab to enter the table, instead of highlighting the whole row, it highlights the row and puts the focus on the first cell.

No it doesn't highlight the row only the first cell gets highlighted.

and when you arrow down, the next row highlights, but the focus is still on the first cell.

Yes when i arrow down the next row highlights, the first row didn't highlight. I want the entire first row to be selected(highlighted) when i enter the table by pressing TAB.

but (let's fantasize for a moment) the row is correctly being "selected" and "highlighted" but the fact that one of the cells "has focus" is causing that cell to "seem selected", and that what you really want to solve is how to make it seem to the user that the row is the only thing selected, and not the cell.

No that's not it i don't mind the cell seeming selected, all i want is when the user Tabs through the GUI, and when the Table gets focus for the first time that not just the first cell is selected, but the entire first row...
Sorry if it seems so unclear....
Thanks!!
[ April 22, 2004: Message edited by: Ling Chung ]
maggie woo
Ranch Hand

Joined: Dec 18, 2003
Posts: 35
Hmm.. Ok, I'm probably out of my league here.
Just to backtrack a bit. If you just have a JTable with the DefaultListSelectionModel, and the mode set to SINGLE_SELECTION, when you tab in the table, it moves cell-to-cell, but when you arrow down or up, entire rows are selected. This is true--yes?
So, when you modified whatever you did to change the tab behavior, what did you change--maybe what you changed affected this?? And it's a matter of tracking that down...
 
GeeCON Prague 2014
 
subject: JTable cell is selected but i want the entire row to be selected