Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable column non-focusable?  RSS feed

 
Michael Labuschagne
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you set a column in a JTable to make it unable to gain focus i.e. select the next column on the same row or ignore the command completely when selecting a column that should not be able to be gained focus on?

Thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a little advanced for the beginners forum. I'm moving this to the Swing forum for expert attention.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this might start you off (you can still d/click to edit)

 
Michael Labuschagne
Ranch Hand
Posts: 56
Java Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael that was a lot of help...

Just one more thing how would you get it to be robust enough so that if you were on the right hand side of the cell it would skip over left and vice-versa?

Thanks...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're talking shift-tab?

if so, add this class field
boolean isShiftDown = false;

add this to the constructor

change the renderer
if(hasFocus) table.changeSelection(row,column+(isShiftDown? -1:1),false,false);

there's probably a simpler way, but it works.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is old but this was a GIGANTIC help. I'm having trouble getting the reverse to work, but that's just a casting issue I think and will look at it later. I've searched and searched for this fix, and THIS was IT. Thank you
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, this shouldn't be regarded as a solution but as a workaround for a non-standard requirement. I say that because the function of a renderer is to render; renderer code, like painting code, shouldn't change the state of the component being rendered.

As a user, I would be confused if tabbing skipped over a column. So I'm curious: what led to this requirement in the first place?
 
Rob Camick
Ranch Hand
Posts: 2752
11
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes this is old and I would not recommend this approach.

A renderer is used to render the data in the table NOT to control focus. I see Darryl made the same observation while I was thinking about the answer

For an alternative approach you could customize the default tabbing Action for the table:


You will need the class from Table Tabbing.

Another option would probably be to override the changeSelection(...) method of the JTable. Of course the logic gets more complicated because you can't just assume focus goes to the next column, like the renderer solution does above.
 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Technically, this shouldn't be regarded as a solution but as a workaround for a non-standard requirement. I say that because the function of a renderer is to render; renderer code, like painting code, shouldn't change the state of the component being rendered.

As a user, I would be confused if tabbing skipped over a column. So I'm curious: what led to this requirement in the first place?


So in this case, and I'm sure I'm doing something sideways (which often tends to be the case with this Swing stuff), in certain tables, I'm using row 1 as a Psudo Header column, so column two is the only column accepting input.

So table with two columns might look like:

FIELD VALUE
----------------
NAME Bob
COLOR blue
COMMENTS blah blah


so when they tab, we don't want to go into the "header" row, but rather only back and forth in Column 1



 
Jason Richard
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:Yes this is old and I would not recommend this approach.

A renderer is used to render the data in the table NOT to control focus. I see Darryl made the same observation while I was thinking about the answer

For an alternative approach you could customize the default tabbing Action for the table:

You will need the class from Table Tabbing.

Another option would probably be to override the changeSelection(...) method of the JTable. Of course the logic gets more complicated because you can't just assume focus goes to the next column, like the renderer solution does above.


This is fantastic. Works great, and pulls the work out of the Render. TY!
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Richard wrote:I'm using row 1 as a Psudo Header column

Why not make it a real header column? You can find how to do that in another of Rob's classes: Row Number Table.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!