Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Recognizing enter on text entry  (Read 1276 times)
0 Members and 1 Guest are viewing this topic.
Offline Gannin

Senior Newbie





« Posted 2006-10-29 10:08:24 »

I'm trying to create a simple text adventure and some modules to go along with it.  The main window of the engine has a JTextArea for displaying text, a JTextField for entering text, and a JButton to act as a send button.

Right now, the only way I know of to see if enter is pressed while the user is typing in text is to scan each key as its typed to see if it's enter or not.  Is there a better way to recognize when the user hits enter when typing in a JTextField?
Offline Kova

Senior Member





« Reply #1 - Posted 2006-10-29 10:27:51 »

sure... you can install KeyListener on that component (JTextField). Basicly you can install KeyListener on every component. See addKeyListener(). When you install it you test key code for VK_ENTER.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #2 - Posted 2006-10-29 10:44:54 »

ActionListener. An ActionEvent is triggered if the user presses return inside some JTextField.

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2006-10-29 14:41:05 »

ok thread hyjack I knew some off the unwritten standards of feel of components was inplemented such as this one, has someone seen the property select-all-on-focus or must I fill in an rfc?

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline ryanm

Senior Member


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #4 - Posted 2006-10-29 15:42:18 »

FocusListener?
Offline Kova

Senior Member





« Reply #5 - Posted 2006-10-29 16:43:47 »

ActionListener. An ActionEvent is triggered if the user presses return inside some JTextField.

? ... maybe action on parent container?

but I use KeyListener:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
        chat_tf.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                int keyCode = evt.getKeyCode();
                if (keyCode == KeyEvent.VK_ENTER) {
                    JTextField src = (JTextField)evt.getSource();
                    HostedGameData.getMyPlayer().say(src.getText().trim(), 5000);
                    src.setText("");
                    viktorije.requestFocus();
                }
            }
        });
Offline Mr_Light

Senior Member




shiny.


« Reply #6 - Posted 2006-10-29 17:00:46 »

would be nice if it worked. however if you take a JFormattedTextfield the focusevent is triggered before the check on the format which causes the text to become unselected. I have to dig up some code.

there was a bug report about this where the proposed workaround was to use invokelater useing some ugly que-ed thing. now hacking around a normal JFormattedTextfield wasn't the problem however since JFormattedTextfield is used in quite some compound-components things get really messy.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #7 - Posted 2006-10-29 22:00:34 »

>but I use KeyListener

Sure works. Like you can write x=x+4-3 instead of ++x.

弾幕 ☆ @mahonnaiseblog
Offline Kova

Senior Member





« Reply #8 - Posted 2006-10-29 22:50:44 »

>but I use KeyListener

Sure works. Like you can write x=x+4-3 instead of ++x.

so actionListener is standard, tnx, I'll change to that
Offline Gannin

Senior Newbie





« Reply #9 - Posted 2006-10-30 10:13:43 »

Thanks for all of the replies, and especially thanks to oNyx and Kova for their suggestions.  I've gone with using ActionListener, and it works great.  Thanks again Smiley.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!