Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

How can you move the cursor authomatically among different GUI components in a JFrame

Thread view: 
Fereshteh Shojaei - 19 Jan 2004 19:12 GMT
Hi,

I wonder if somebody can help me. I would like to move the cursor from one
JTextField to another one when I press <CR>.
Regards,
fereshteh
Andrew Thompson - 19 Jan 2004 21:39 GMT
....
| I wonder if somebody can help me. I would like to move the cursor from one
| JTextField to another one when I press <CR>.

A FocusTraversalPolicy should be able to
do that for you..

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Andrew Thompson - 19 Jan 2004 21:42 GMT
..
| ....I would like to move the cursor from one
| JTextField to another one when I press <CR>.

Oops!  I was thinking of going around the
components in a 'non-standard' way, you
need to implement a KeyListener and add it
to each JTextField, then on KeyEvent, check
if it is the <cr> key..

--
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 01:08 GMT
> Hi,
>
> I wonder if somebody can help me. I would like to move the cursor from one
> JTextField to another one when I press <CR>.

Try adding an ActionListener to the JTextField.

It's always best to minimize hooking keystrokes directly.
Andrew Thompson - 20 Jan 2004 01:30 GMT
| > ..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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.