| > ..I would like to move the cursor from one
| > JTextField to another one when I press <CR>.
|
| Try adding an ActionListener to the JTextField.
Huh! It had never occured to me that
would work!*
| It's always best to minimize hooking keystrokes directly.
Why?
* well, for the sake of the OP..
(and because I had invested an entire
2 minutes of my life checking what
Jon said)
__________________________
import java.awt.*;
import java.awt.event.*;
public class KeyboardText extends Frame implements ActionListener
{
TextField tf1 = new TextField(),
tf2 = new TextField();
KeyboardText()
{
super("KeyBoard Text");
setLayout(new GridLayout(0,1));
tf1.addActionListener(this);
add( tf1 );
tf2.addActionListener(this);
add( tf2 );
pack();
setVisible(true);
}
public void actionPerformed( ActionEvent ae )
{
System.out.println(ae);
if (ae.getSource()==tf1) tf2.requestFocus();
else tf1.requestFocus();
}
public static void main(String args[])
{
new KeyboardText();
}
}
__________________________
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Jon A. Cruz - 20 Jan 2004 02:27 GMT
> | It's always best to minimize hooking keystrokes directly.
>
> Why?
To keep things higher-level, encapsulated and robust.
If you go mucking about with keys directly you might:
* mess up keys that are used by the windowing system
* use keys that are not accessible or easy on keyboards in other countries
* use keys that don't match the defaults for all other applications on
the target platform (Ctrl-C versus Command-C on the Mac, for example)
* interfere with accesibility software/systems
* make your program hard to internationalize
* interfere with other widget/frame combinations
* confuse users
Those are just what I could list in about 30 seconds. I'm sure there are
a lot more.
It's similar to avoiding hooking mouse events directly. Easy to forget
to call MouseEvent.isPopupTrigger() when needed, etc.
Andrew Thompson - 20 Jan 2004 02:59 GMT
"Jon A. Cruz" wrote in message
| > | It's always best to minimize hooking keystrokes directly.
....
| To keep things higher-level, encapsulated and robust.
<snip list>
..hmmm. Makes me wonder just how
workable my Calculator is on other systems.
http://www.physci.org/launcher.jsp#Calculator
I'll have to check it out further..
--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Jim Sculley - 21 Jan 2004 02:17 GMT
> "Jon A. Cruz" wrote in message
> | > | It's always best to minimize hooking keystrokes directly.
[quoted text clipped - 7 lines]
>
> I'll have to check it out further..
Be sure to explore InputMap and ActionMap. These are the preferred
methods for binding keystrokes to actions.
Jim S.
Andrew Thompson - 21 Jan 2004 02:30 GMT
"Jim Sculley" writ..
| > "Jon A. Cruz" wrote in message
| > | > | It's always best to minimize hooking keystrokes directly.
....
| Be sure to explore InputMap and ActionMap. These are the preferred
| methods for binding keystrokes to actions.
Thanks Jim, I'll look into it.