JTextfield: Changing focus, then doing stuff

So I have 3 JTextFields that are essentially "connected" (I automate tabbing and such back and forth). I have no problem gaining focus, whether it be transferFocusBackward(), or whatever else. My problem is when getting the focus I cannot do anything with it like taking the letter the user typed and insert it into the next field.

My code that I used is in KeyTyped and I do a

JTextField fake = new JTextField();

fake.transferFocus();

JTextField newFocus = ((JTextField)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

newFocus.setText(String.valueOf(event.getKeyChar))

When I check to see who the focus is on before and after I call transferFocus(), it doesn't change JTextFields (I know because I name them differently and ask their names). Does someone know a better way...that actually changes focus, when it is called, or have any workarounds?

[915 byte] By [Flavouskia] at [2007-11-27 11:58:43]
# 1

> My problem is when getting the focus I cannot do anything with it like

> taking the letter the user typed and insert it into the next field.

Well I don't understand your requirement.

Why would you type a character, have focus change to a different text field and then insert that character into the text field. That would be confusing to the user to have the character appear in a different text field than the text field that originally had focus.

camickra at 2007-7-29 19:21:24 > top of Java-index,Desktop,Core GUI APIs...
# 2

Nevermind I figured a workaround for it. I set the setFocusCycleRoot to true in the constructor, and then said

JTextField fake = new JTextField();

JTextField newFocus = ((JTextField)this.getFocusTraversalPolicy().getComponentBefore(this, fake));

//I stuck what the user typed at the beginning of the text field...not really sure which is the better way

newFocus.setText(event.getKeyChar()+newFocus.getText());

fake.transferFocus();

The reason it wasn't working is because swing hadn't actually acknowledged that the focus had changed yet...apparently this happens later.

The reason I had asked was that there was an edge case on the three JTextField, lets say the user types something into the first text field and moves to the next field, then randomly goes back, well, when they press a letter, then they would just jump to the next JTextField, and the letter wouldn't appear...i think that would be more confusing than having the letter appear in the next text field, but meh, we'll see.

Flavouskia at 2007-7-29 19:21:24 > top of Java-index,Desktop,Core GUI APIs...
# 3

Yeah...my explanation...kinda blah after reading it again.

These JTextFields are limited in size, and by what character can be used (its a set of Date fields!). It just feels funny when you type a number in a field and the field is full (because you really didn't want to go back that far) and nothing appears...it doesn't look like the right flow.

But these datefields...looking awesome.

Flavouskia at 2007-7-29 19:21:24 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well you basic code is non standard from what I can tell. Usually when you have related fields with a fixed size you automaticallly tab to the next field when the text field becomes full. This way the caret is always blinking in the text field that is about to accept the character typed.

Of course you can always use a JFormattedTextField which also supports this concept.

camickra at 2007-7-29 19:21:24 > top of Java-index,Desktop,Core GUI APIs...