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 Processing a Return or Enter Key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Processing a Return or Enter Key" Watch "Processing a Return or Enter Key" New topic
Author

Processing a Return or Enter Key

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

I need to detect when a carriage return in pressed and take an action. I need to do this both for a JTable and for a JTextComponent.

For the JComponent, it's for a store receiving merchandise with a barcode. The barcode is terminated by a carriage return.
Here is what I have for this:


class ReceivingText extends LengthText implements Keymap {
...
}

LengthText is one of my classes. It's just a JTextComponent that limits how long the text can be.

I'm confused by all of the methods that I have to implement from the Keymap interface.

I also need to process carriage returns in a JTable. Here is what I have so far:

private class TableEditor extends LengthText implements TableCellEditor, Keymap {
..
}

Again, I'm confused by all of the methods of TableCellEditor and Keymap.

Am I on the right track, implementing the Keymap and the TableCellEditor interfaces and if so, how do I proceed?

Thanks for reading my questions.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

OK. I went ahead by myself and I was able to code-up a solution for when the user presses return or enter (or when a barcode is scanned and I get a carriage return character at the end of the input).



This seems to work, but is an awful lot of code. Am I going about this right?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

First of all, you should rarely need to implement Action. Extending AbstractAction is a much more common approach.

For the text component, there are two ways I can think of:
- use a KeyListener or key binding (using InputMap and ActionMap) if the contents are only entered from the user interface.
- use a DocumentListener if the contents can come from code as well.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

OK. Thank you. I got a KeyListener to work for me.

I'm still puzzled by how to install my own editor in a JTable. Ideally, I want to install my own subclass of JTextField that already knows how to look for carriage returns and calls the appropriate method.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

A DocumentListener is the way to go. Even f the contents are only entered from the user interface, you can't rule out Ctrl-V to paste text that includes a newline.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Kaydell Leavitt wrote:I'm still puzzled by how to install my own editor in a JTable. Ideally, I want to install my own subclass of JTextField that already knows how to look for carriage returns and calls the appropriate method.

I think the easiest way is to use a DefaultCellEditor that you construct with a JTextField with the appropriate support added to it. I'm not 100% sure the listeners will work like this, you'll need to check that out.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Thanks again for this other tip about DefaultCellEditor, but I can't get it to work. Here is my code:



The JTable doesn't seem to be using my jTextField. What am I doing wrong?

-- Kaydell
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

I figured out how to get my cell editor to work. For some reason, I have to set it for every column. When I try to set a table cell editor for the whole JTable, it doesn't seem to do anything.


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Or just set it as the default cell editor for Object.class. Unless the table or model returns different classes in getColumnClass this should then be used for all columns.

Also, if you still want to do it your way, you can make all columns share the same cell editor instance. Declare and initialize it outside the loop, then set it inside.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Processing a Return or Enter Key
 
Similar Threads
How to create border or change color in a JTable cell?
jtable
JTable
Wanted: conceptual tutorial on JTable
JTable + TableCellRenderer