Auto-shift of focus between JTextFields

Greetings.

I have an array of 50 JTextFields which I restricted to a four-character entry limit. The limit works just fine, but I would like the focus to shift to the next JTextField when the limit is reached. Currently, I'm watching for the limit via an extended PlainDocument called CharLimitDocument. (*code included below) I know that Java offers the function FocusManager.focusNextComponent(Component aComp), but I'm not sure how to retrieve the current component. Can anyone help?

Thanks in advance,

Liz

*CharLimitDocument Code

import javax.swing.text.*;

import java.awt.*;

//This class overloads the Document so that a JTextField can be designed to accept only a specific number of characters.

class CharLimitDocument extends PlainDocument

{

private int limit;

public CharLimitDocument(int limit)

{ this.limit = limit; }

public void insertString(int offs, String str, AttributeSet a) throws BadLocationException

{

if((str.length() + getLength()) <= limit)

{super.insertString(offs, str, a);}

else

{Toolkit.getDefaultToolkit().beep();}

}

}//end class CharLimitDocument

[1239 byte] By [chaosmaster] at [2007-9-26 1:32:45]
# 1

Maybe you were talking about this function in javax.swing.JComponent?

hasFocus() (returns boolean)

You could test each component to see if it hasFocus() and then you'd be able to retrieve the 'current' component (pseudo code below). Even though you have fifty fields, this still woudn't be memory/processor intensive.

Hope that helps,

H

int compindex=0;

while((compindex)<=(getComponentCount()))

{

if(getComponent(compindex).hasFocus()==true)

{this is the current component... do whatever;}

else

{compindex++;}

}

HFactor at 2007-6-29 1:34:14 > top of Java-index,Archived Forums,Swing...
# 2
I'm beginning to think this is what I'm going to have to do. My only problem now is how to receive the information that the input is at maximum. This suggests, at least to me, use of an event.Thanks for your suggestion.Liz
chaosmaster at 2007-6-29 1:34:14 > top of Java-index,Archived Forums,Swing...
# 3
Yeah, probably.You'll want to check http://java.sun.com/j2se/1.3/docs/api/javax/swing/JTextField.html for more information on the text fields. I'd probably be using things like *hint* paramString() and createActionPropertyChangeListener(Action a)...Good luck
HFactor at 2007-6-29 1:34:14 > top of Java-index,Archived Forums,Swing...
# 4

Yep, I did something similar to this, and created a custom event type and listener (MaximumLengthEvent & MaximumEventListener). When the limit is reached (the point in your code where you call Toolkit.beep()), just fire the event.

Your registered listener will now be able to shift the focus when it receives the event.

growler2 at 2007-6-29 1:34:14 > top of Java-index,Archived Forums,Swing...
# 5
How about using javax.swing.SwingUtilities.findFocusOwner(Component c) function? It returns the child component which has focus, if any.
asulkina at 2007-6-29 1:34:14 > top of Java-index,Archived Forums,Swing...