Solved

Java AWT: how to make cursor automatically show?

Posted on 2014-11-05
8
262 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

760 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

17 Experts available now in Live!

Get 1:1 Help Now