Solved

Java AWT: how to make cursor automatically show?

Posted on 2014-11-05
8
329 Views
Last Modified: 2014-11-05
Hello experts,

I am working on a user interface with java.awt packages. There are several input fields on different lines. I would like to make the cursor to move automatically to the head position of the next line after I hit "return" on my keyboard on the current line.

How to do this? Any ideas, any website tutorials or a piece of code to share? I searched online but I did not find related information.

Thanks so much.
0
Comment
Question by:Wendyu66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 

Author Comment

by:Wendyu66
ID: 40424443
Just a quick search and I found http://www.experts-exchange.com/Web_Development/Software/Q_24442517.html.

I actually also use requestFocus(), but it did not pop up a cursor. Following is a piece of my code.
	private void addFaxNumberTextBox() {
		faxNumberTextField = new TextField();
		faxNumberTextField.setSize(GUIAttributes.FAX_TEXTFIELD_SIZE);
		faxNumberTextField.setLocation(faxNumberLabel.getX() + faxNumberLabel.getWidth() + GUIAttributes.PADDER, faxNumberLabel.getY());
		setPrevTextFieldPos(faxNumberTextField.getX(), faxNumberTextField.getY());			
		faxNumberTextField.setBackground(GUIAttributes.TEXTFIELD_BACKGROUND);
		
		faxNumberTextField.setVisible(true);
		faxNumberTextField.addFocusListener(new TextBoxListener());
		this.add(faxNumberTextField);	
				
		faxNumberTextField.requestFocus();
}

Open in new window



There are also other methods like addFaxSenderNameTextBox(), addFaxReceiverNameTextBox(), etc. They all have similiar code.  These text fields are all popped up once instead of filling a field then popping up another.

Any ideas? Thanks a lot.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40424486
Yeah iirc you have to use a keylistener to trap the event and find if the user used VK_ENTER, then add in the CRLF tokens.

[edit] no they are for single lines of text.

But it looks anyway like you are talking about moving the tab to another field - is that correct? Can you tell us?
0
 

Author Comment

by:Wendyu66
ID: 40424795
Thanks Karakatoa. I actually have two questions.

1. Why faxNumberTextField.requestFocus() did not bring a cursor to this text field?
2. How to pop up a cursor on the head position of next line (input text field) after I have hit "return" on my keyboard? For example, following are input fields.

Fax Number:
Sender Name:
Sender Email:


After I have inputted a fax number in the input field of Fax Number, right now I have to manually click my mouse to put a cursor at the head position of the input field of Sender Name. Now that I do not want to do this manually, I just want the cursor to show at that position after I hit "return" button on my keyboard after I have finished inputting to the input field of "Fax Number". Is this clear?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 17

Accepted Solution

by:
krakatoa earned 500 total points
ID: 40424862
Did you set setFocusable(boolean) to true?
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40424877
And you might need this :
((JTextField)e.getSource()).transferFocus();

Open in new window

0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40424892
Sorry - in context such as

public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER){((JTextField)e.getSource()).transferFocus();}} 

Open in new window


(keyPressed() being a method in a KeyListener, (which is best obtained via a KeyAdapter), and 'e' being a KeyEvent passed as a parameter to the method from the callback).
0
 

Author Comment

by:Wendyu66
ID: 40425233
I finally figured it out. requestFocus() did the miracle.

Thanks so much for your replies. Appreciated!
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40425247
Right-o.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question