Solved

Java AWT: how to make cursor automatically show?

Posted on 2014-11-05
8
327 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
  • 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 16

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 16

Accepted Solution

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

Expert Comment

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

Open in new window

0
 
LVL 16

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 16

Expert Comment

by:krakatoa
ID: 40425247
Right-o.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
Opening PDF on button click and fill new document 2 47
jsp error 6 26
Java class and jar 3 24
Objective of This Article In 1990’s, when I was a budding software professional, I had a lot of confusion about which stream or technology, I had to choose to build my career. In those days, I had lot of confusion like whether to choose System so…
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

860 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