• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTable and tab - last fix before release! Yay!!

 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have searched the internet high and low for a solution to this, but I have received no clear answer. I have a JTable where column 1 is full of editable cells. On pressing the tab key, I want the cell that it is on to stop editing and the next editable cell below it to start editing.

At the moment if I push tab twice, this is accomplished, but I need it to work for one press of tab.

This is the code that is in the keyPressed for the component that the TableCellEditor is extending.



So it will print out
tab
true

But the cell below doesn't start editing unless I hit tab twice.

Any help would be most greatly appreciated as this is the last fix before release! Hooray!!

Many kind regards,
Rachel
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could be a focus issue, the second tab moving focus to the editor component. Can you determine the focused component.
D.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don

In the editable column, I typed the numbers 1 to 9 sequentially in the cells.

In the focus gained I asked it to print out "focus gained"+getText();

Action: I clicked on the cell with 1 in it
Response: Printed "focus gained 1" to screen

Action: Press tab key
Response: Printed "tab" (the key) and "true" (result of editCellAt)
Visual: cell 1 does not look in edited state now

Action: Press tab key (second tab)
Response: Nothing
Visual: No Change

Action: Pressed 'h' key
Response: Printed "focus gained 2h" to screen
Visual: the cell of '2' now has '2h' in it and it looks in it's editable state.

Cheers,
Rachel
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its sounds like some kind of focus issue to me. The use of the TAB key could be confusing the issue as by default, it switches the focus to the next focusable component. I fear you may be switching focus to an editor component or something. Can you find out exactly what does have focus ?
A possible solution is to try theTable.requestFocus()
hth.
D.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Don

You are right - it looks to be with the focus, so I am going to get it to work for another key like f11 or something and then go back and compare that to the tab key.

I'll let you nkow how I get on.

Cheers and thanks again,
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again!

I got the code to work for a key other than tab, in my case the F11 key. I have to take out the stopCellEditing in the focusLost and now it's fine. And the funny thing is that I put the stopCellEditing call there so that I could save the changes to the cell, but now the changes save anyway. The joys of Java!

But now - well the F11 key is all well and good - I need it to work for the tab key. I tried to capture the vk_tab just as I did the vk_f11 but it's not working.

I think that it's still that tab is a special focus key. Is there any way to tell java to pretend that tab is just any other key with no special focus powers other than what I give it?

Can anyone tell me where to go from here?

Many thanks!
Rachel
[ November 25, 2004: Message edited by: Rachel Swailes ]
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could have a look at theTable.getActionMap() and see if the TAB action is contained in there (check the parent map too (map.getParent()) and remove.

An alternative tactic would be to programmtically set the focus with theTable.requestFocus() at an opportune time.

good luck !
D.
 
Pritam Barhate
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try playing with

methods of java.awt.Component class. I have a gut feeling that it may solve it.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

Thanks for all the advice. I haven't had a change to try as the boss has me brainstorming something else. I'll give it a go as soon as I can and I'll let you know what happens.

Thanks again!
Rachel
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

I have played with the focus traversal keys thing, but I can't figure out how this will be able to solve the problem. Could you give me another hint?

Many kind regards,
Rachel
 
Don Kiddick
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance, theTable.setFocusTraversalKeysEnabled(false) should mean tab is handled like a normal key.
 
Rachel Swailes
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did try setting that to false and the tab still behaved as usual. When I press tab once, the editing of that cell stops. When I press it the second time, the editor moves to the next cell but it only looks editable when i start typing.

Cheers,
Rachel
 
Myra Junio
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi rachel,

try placing your code on keyReleased event instead of keyPressed
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rachel: I'm not really understanding your code, it seems very redundant. For example:



Why are you using changeSelection at all? editCellAt(row, col) should be all you need to call to change to the appropriate cell and begin editing.

Furthermore, what functionality should tab have if the user is in one of the uneditable columns? What if they're in an editable column but not currently editing it? You may need to take that into consideration.

Regardless, the most fundamental problem I see with your implementation is that you aren't consuming the event which allows it to propogate up and the default behavior to happen in addition to your behavior. What you need to do is add an e.consume() to the end. Also, I don't remember off the top of my head if the default behavior operates off of keyReleased or keyPressed, but for safety's sake I like to consume the event for the key I'm overriding on both. Thus an implementation would look something like this:



Obviously this provides no alternate functinality if they're in a non-editable column, it just starts editing at the editable column in the next row and starts back at the first row if there isn't another row. You might want to add a row instead of doing that.

Also, I noticed some logic using CompoundKeyPair and TransKeyPair, so that obviously would need to be inserted to determine the next editable row, if any. This is all off the top of my head, so the code may not be perfect, but I just recently wrote a few tables that had to use custom renderers/editors and had to override a lot of the default functionality of things like TAB and ENTER, something akin to above worked fine for me.
 
Charles Woodson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only moving to editable cells has been answereed very nicely here:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=509913
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic