This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Processing a Return or Enter Key

 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5126
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic