File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Issues with JTextArea as cellEditor of JTable Column. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Issues with JTextArea as cellEditor of JTable Column." Watch "Issues with JTextArea as cellEditor of JTable Column." New topic
Author

Issues with JTextArea as cellEditor of JTable Column.

nirvan bud
Greenhorn

Joined: Dec 27, 2008
Posts: 5
I am trying to use JTextArea as a cellEditor for a JTextArea column. I created a custom CellEditor for the purpose. But when I use the JTextArea component I get a few issues.

1. The JTextArea default "Tab" key operation does not take the cursor out of the textarea. To avoid this I used
textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,new Tab Key). This solved my problem.

2. To come out of the textArea column into the next column, I have to press the "Tab" key twice. I have not been able to solve this problem.

3. Since I am using JTextArea, I want to use JScrollPane with JTextArea in it instead. But when I use JScrollPane with JTextArea in it pressing the "Tab" key takes focus out of the JTable, instead of traversing to the next column. I haven't been able to solve this problem too.

Here is the custom CellEditor code.




regards,
nirvan
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
BeForthrightWhenCrossPostingToOtherSites

OP has been asked to provide a SSCCE in the other forum but has yet to do so.
nirvan bud
Greenhorn

Joined: Dec 27, 2008
Posts: 5
Rob,
Sorry about cross-posting. Here is the SSCCE that shows the problem. Please note following.

1. In the SSCCE, I have used JTextArea inside the JTable column without JScrollPane holding it. This will show that you will have to press "Tab" key twice in order to get out of JTable column.

2. If you change the SSCCE to use JScrollPane as container for JTextArea, you will see that pressing the "Tab" key takes the cursor out of the JTable instead of to the next column.


The main JFrame that contains JTable




The Data Oboject that ArrayList needs to back the JTable




The WrappedAction class for setting new action to TAB key (Written by you).




regards,
nirvan
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
That is NOT a SSCCE.

Your question is about a custom cell editor. Your code includes a custom TableModel and custom data class. Those are irrelevant for testing a cell editor. Maybe the model or data class is causing the problem so get rid of them. It takes one line of code to create a JTable:

JTable table = new JTable(10, 5);

Then you add you custom editor to it and start testing. We don't want (or have time) to look at code that is not related to the problem.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
And can I remind you of what the other Rob told you. Private message to follow in case you can't remember.
nirvan bud
Greenhorn

Joined: Dec 27, 2008
Posts: 5
Sorry once again Rob. This time I am sure it is an SSCCE.

Issue Once again:

1. If the column editor is JTextArea, then after editing the column, it requires two "Tab" key presses to come out of it.

2. If the column editor is JScrollPane with JTextArea inside, then it is not possible to start it editing using the keyboard. It requires use of mouse to start editing.

Here is the code.



regards,
nirvan.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Please read your private messages. This is not optional.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2216
    
    7
Well, there are actually more than 2 InputMaps for the text area so you need to loop through all of them to make sure the binding is removed.

nirvan bud
Greenhorn

Joined: Dec 27, 2008
Posts: 5
I have resolved the problem no 1 presented in the last SSCCE post. Now the only problem is (problem no 2) that when I add scrollPane (with JTextArea in it), instead of directly adding JTextArea to the JTable, the column cannot be edited by keyboard i.e. it requires a mouse click.

regards,
nirvan.
nirvan bud
Greenhorn

Joined: Dec 27, 2008
Posts: 5
I implemented focus listener on the Custom cell editor class to find where exactly the focus is going when using keyboard. When scrollPane is used instead of directly adding JTextarea, the focus goes to scrollPane and not the JTextArea and hence I am not able to edit the cell. Surprisingly, when mouse is directly used over the cell to gain focus, the Jtextarea gets the focus !.

I tried to use table.setSurrendersFocusOnKeystroke(true) but that doesn't solve the problem.

If anyone knows how to set focus to the child component of jscrollpane preferrably without using Focus Traversal Policy, please let me know.


regards,
nirvan.
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
this thread is not that very old so i will reply. in case you are interested nirvan bud, i created a TextAreaCellEditor class that traps the TAB key event and transfers focus to the next cell of the JTable.

in this case, the TAB key will loop through all cells of the JTable endlessly, meaning, if it is at the last column and last row of the JTable, the TAB key will then focus the 1st row and 1st column.

http://tech.chitgoks.com/2010/01/28/jtextarea-cell-editor-tab-key-traversal-to-next-cell-in-jtable/


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issues with JTextArea as cellEditor of JTable Column.