Solved

Java AWT: how to make cursor automatically show?

Posted on 2014-11-05
8
310 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
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 40424862
Did you set setFocusable(boolean) to true?
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Securing Jmx Console and web console 2 66
Server error in 'XXXXXXXX' application. 12 74
Java Timer (static) 9 36
How to convert String matching to regex in java 4 21
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 …
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now