473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Hi,

I wonder if somebody could help me. I would like to move the cursor from one
JTextField to another one when I press <CR>.
Regards,
Fereshteh

Jul 17 '05 #1
4 6917
"Fereshteh Shojaei" <fe****************@comhem.se> wrote in message news:<SA********************@newsc.telia.net>...
Hi,

I wonder if somebody could help me. I would like to move the cursor from one
JTextField to another one when I press <CR>.
Regards,
Fereshteh


Hello Fereshteh,

One solution to your problem involves calling a component's
requestFocus() method from within another component's action listener.
To demonstrate, I've created a MoveCursor.java program. The source
code appears below:

// MoveCursor.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MoveCursor
{
static JTextField t1, t2;

public static void main (String [] args)
{
JFrame f = new JFrame ("Move Cursor");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel (new FlowLayout (FlowLayout.LEFT));

JLabel l = new JLabel ("Name:");
p.add (l);

t1 = new JTextField (20);
t1.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t2.requestFocus ();
}
});
p.add (t1);

f.getContentPane ().add (p, BorderLayout.SOUTH);

p = new JPanel (new FlowLayout (FlowLayout.LEFT));
l = new JLabel ("Address:");
p.add (l);

t2 = new JTextField (20);
t2.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t1.requestFocus ();
}
});
p.add (t2);

f.getContentPane ().add (p, BorderLayout.NORTH);

f.setSize (350, 100);
f.setResizable (false);
f.setVisible (true);
}
}

Although I haven't commented the code, note that the t1-referenced
JTextField invokes t2.requestFocus (); from within its action
listener. This causes focus to shift to the t2-referenced JTextField
when the <CR> key is pressed. The same idea holds with the other
JTextField.

I hope this answers your question. For more help with Java, please
visit my Web site at http://www.javajeff.net

Have a good day.

Jeff
Jul 17 '05 #2
"Fereshteh Shojaei" <fe****************@comhem.se> wrote in message news:<SA********************@newsc.telia.net>...
Hi,

I wonder if somebody could help me. I would like to move the cursor from one
JTextField to another one when I press <CR>.
Regards,
Fereshteh


Hello Fereshteh,

One solution to your problem involves calling a component's
requestFocus() method from within another component's action listener.
To demonstrate, I've created a MoveCursor.java program. The source
code appears below:

// MoveCursor.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MoveCursor
{
static JTextField t1, t2;

public static void main (String [] args)
{
JFrame f = new JFrame ("Move Cursor");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel (new FlowLayout (FlowLayout.LEFT));

JLabel l = new JLabel ("Name:");
p.add (l);

t1 = new JTextField (20);
t1.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t2.requestFocus ();
}
});
p.add (t1);

f.getContentPane ().add (p, BorderLayout.SOUTH);

p = new JPanel (new FlowLayout (FlowLayout.LEFT));
l = new JLabel ("Address:");
p.add (l);

t2 = new JTextField (20);
t2.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t1.requestFocus ();
}
});
p.add (t2);

f.getContentPane ().add (p, BorderLayout.NORTH);

f.setSize (350, 100);
f.setResizable (false);
f.setVisible (true);
}
}

Although I haven't commented the code, note that the t1-referenced
JTextField invokes t2.requestFocus (); from within its action
listener. This causes focus to shift to the t2-referenced JTextField
when the <CR> key is pressed. The same idea holds with the other
JTextField.

I hope this answers your question. For more help with Java, please
visit my Web site at http://www.javajeff.net

Have a good day.

Jeff
Jul 17 '05 #3
Thank you Jeff, It works perfectly.
regards, Fereshteh
"Jeff" <je**@gatewest.net> skrev i meddelandet
news:78**************************@posting.google.c om...
"Fereshteh Shojaei" <fe****************@comhem.se> wrote in message

news:<SA********************@newsc.telia.net>...
Hi,

I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>.
Regards,
Fereshteh


Hello Fereshteh,

One solution to your problem involves calling a component's
requestFocus() method from within another component's action listener.
To demonstrate, I've created a MoveCursor.java program. The source
code appears below:

// MoveCursor.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MoveCursor
{
static JTextField t1, t2;

public static void main (String [] args)
{
JFrame f = new JFrame ("Move Cursor");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel (new FlowLayout (FlowLayout.LEFT));

JLabel l = new JLabel ("Name:");
p.add (l);

t1 = new JTextField (20);
t1.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t2.requestFocus ();
}
});
p.add (t1);

f.getContentPane ().add (p, BorderLayout.SOUTH);

p = new JPanel (new FlowLayout (FlowLayout.LEFT));
l = new JLabel ("Address:");
p.add (l);

t2 = new JTextField (20);
t2.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t1.requestFocus ();
}
});
p.add (t2);

f.getContentPane ().add (p, BorderLayout.NORTH);

f.setSize (350, 100);
f.setResizable (false);
f.setVisible (true);
}
}

Although I haven't commented the code, note that the t1-referenced
JTextField invokes t2.requestFocus (); from within its action
listener. This causes focus to shift to the t2-referenced JTextField
when the <CR> key is pressed. The same idea holds with the other
JTextField.

I hope this answers your question. For more help with Java, please
visit my Web site at http://www.javajeff.net

Have a good day.

Jeff

Jul 17 '05 #4
Thank you Jeff, It works perfectly.
regards, Fereshteh
"Jeff" <je**@gatewest.net> skrev i meddelandet
news:78**************************@posting.google.c om...
"Fereshteh Shojaei" <fe****************@comhem.se> wrote in message

news:<SA********************@newsc.telia.net>...
Hi,

I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>.
Regards,
Fereshteh


Hello Fereshteh,

One solution to your problem involves calling a component's
requestFocus() method from within another component's action listener.
To demonstrate, I've created a MoveCursor.java program. The source
code appears below:

// MoveCursor.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

class MoveCursor
{
static JTextField t1, t2;

public static void main (String [] args)
{
JFrame f = new JFrame ("Move Cursor");
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

JPanel p = new JPanel (new FlowLayout (FlowLayout.LEFT));

JLabel l = new JLabel ("Name:");
p.add (l);

t1 = new JTextField (20);
t1.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t2.requestFocus ();
}
});
p.add (t1);

f.getContentPane ().add (p, BorderLayout.SOUTH);

p = new JPanel (new FlowLayout (FlowLayout.LEFT));
l = new JLabel ("Address:");
p.add (l);

t2 = new JTextField (20);
t2.addActionListener (new ActionListener ()
{
public void actionPerformed
(ActionEvent e)
{
t1.requestFocus ();
}
});
p.add (t2);

f.getContentPane ().add (p, BorderLayout.NORTH);

f.setSize (350, 100);
f.setResizable (false);
f.setVisible (true);
}
}

Although I haven't commented the code, note that the t1-referenced
JTextField invokes t2.requestFocus (); from within its action
listener. This causes focus to shift to the t2-referenced JTextField
when the <CR> key is pressed. The same idea holds with the other
JTextField.

I hope this answers your question. For more help with Java, please
visit my Web site at http://www.javajeff.net

Have a good day.

Jeff

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hung Huynh | last post by:
Hi, I'm trying to use either one of these methods to position the cursor in a specific position inside a recordset, but neither one seems to work. For example, I have 10 records in my rsData...
1
by: gmtongar | last post by:
Hi, I've made a custom scrollbar which consists of three buttons and a panel. My problem is: How do I move it like a scrollbar slider? I've tried DoDragDrop, but it does not appear to be the same,...
1
by: Phil Endecott | last post by:
Dear Postgresql experts, According to the documentation for MOVE, it returns the number of rows that it has moved over. It seems to me that this is true for MOVE FORWARD n, but not for MOVE...
7
by: sreenulanka | last post by:
please help me in my forum i have labels and textareas .iwant to add textarea components dyanamically when i am clicking the button.please help me import java.awt.*; import javax.swing.*;...
4
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
Am I not supposed to be able to set the current cursor by assigning Cursor.Current? this.Cursor in the main dialog works, but not Cursor.Current other components. I am using a third party tool...
5
by: heat84 | last post by:
Hie , I am adding three buttons to my frame but when I am running the code , the buttons do not have uniform width. Can someone help me out.- I need ideas on how to make btnA,btnB and btnC have the...
1
by: bbtunde87 | last post by:
Hello guzs. I' m Tunde from Lokoja, Nigeria. Pls has anyone got an idea of how I can authomatically start my applications.
1
by: pankajs | last post by:
Hello ! Freinds I m a new member & I have to prepare a project on a game in java I want to know that how to remove my JFrame Components.???????
5
by: michels287 | last post by:
Right now I have a touchscreen with a virual keyboard. I would like to create arrow keys. If the user messed up the inputted text, he can move the blinking cursor left one space at a time between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.