Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Processing a Return or Enter Key

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic