Link to home
Start Free TrialLog in
Avatar of Wendyu66
Wendyu66

asked on

Java AWT: how to make cursor automatically show?

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.
Avatar of Wendyu66
Wendyu66

ASKER

Just a quick search and I found https://www.experts-exchange.com/questions/24442517/How-do-you-set-the-cusor-caret-to-a-particluar-TextField.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.
Avatar of krakatoa
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?
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?
ASKER CERTIFIED SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And you might need this :
((JTextField)e.getSource()).transferFocus();

Open in new window

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).
I finally figured it out. requestFocus() did the miracle.

Thanks so much for your replies. Appreciated!
Right-o.