java - Moving a focus to Next JTextfield when it crosses it Jtextfieldlimit -
i'm working design license activation part. had 2 text fields in java swing.
java code:
jtextfield1.setdocument(new jtextfieldlimit(8)); jtextfield2.setdocument(new jtextfieldlimit(8));
when jtextfield1 ended entering 8 alphanumeric characters. had move focus next jtextfield2 automatically.are there default methods available or how can done.
the basic answer question is, keyboardfocusmanager#focusnextcompnent
the longer answer more complicated.
lets start fact java 1.4, no longer required or recommended utilise document
functionality, instead use documentfilter
, see implementing document filter , documentfilter examples more details
i prefer reusable , configurable solutions, end, i'd prefer see solution allowed me change decision making processes involved in deciding when focus should transferred , configure how transfer occurs
now, remember, use custom focustraversalpolicy
change way in focus moves between components, isn't desirable solution.
let's start basics...
public interface autofocustransferdelegate { public boolean shouldtransferfocus(document doc); } public interface autofocustransferobserver { public void focustransfershouldoccur(document doc); } public class autofocustransferhandler implements documentlistener { private autofocustransferlistener listener; private autofocustransferdelegate delegate; public autofocustransferhandler(autofocustransferlistener listener, autofocustransferdelegate delegate) { this.listener = listener; this.delegate = delegate; } @override public void insertupdate(documentevent e) { checkfortransfer(e.getdocument()); } @override public void removeupdate(documentevent e) { checkfortransfer(e.getdocument()); } @override public void changedupdate(documentevent e) { checkfortransfer(e.getdocument()); } public void checkfortransfer(document doc) { if (delegate != null && delegate.shouldtransferfocus(doc)) { if (listener != null) { listener.focustransfershouldoccur(doc); } } } }
here have "delegate", used configure , customise decisions making process on when transfer should take place, "observer" notified when transfer should take place, allows decide "how" transfer should take place , documentlistener
monitor document
used query delegate , notify observer.
sure, wrap in single class, now, have simple , highly configurable solution don't need extend classes in order achieve new result (nit pick on part). it's basic example of composition on inheritance.
and might able use like...
import java.awt.eventqueue; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.keyboardfocusmanager; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.documentevent; import javax.swing.event.documentlistener; import javax.swing.text.abstractdocument; import javax.swing.text.attributeset; import javax.swing.text.badlocationexception; import javax.swing.text.document; import javax.swing.text.documentfilter; public class test { public static void main(string[] args) { new test(); } public test() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { ex.printstacktrace(); } jframe frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(new testpane()); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } public class testpane extends jpanel { public testpane() { setlayout(new gridbaglayout()); gridbagconstraints gbc = new gridbagconstraints(); gbc.gridwidth = gridbagconstraints.remainder; int maxcharacters = 8; autofocustransferlistener listener = new autofocustransferlistener() { @override public void focustransfershouldoccur(document doc) { keyboardfocusmanager.getcurrentkeyboardfocusmanager().focusnextcomponent(); } }; autofocustransferdelegate delegate = new maxlengthautofocustransferdelegate(maxcharacters); documentfilter filter = new sizefilter(maxcharacters); documentlistener doclistener = new autofocustransferhandler(listener, delegate); add(createtextfield(8, filter, doclistener), gbc); add(createtextfield(8, filter, doclistener), gbc); add(new jbutton("ok"), gbc); } protected jtextfield createtextfield(int maxcharacters, documentfilter filter, documentlistener doclistener) { jtextfield field = new jtextfield(maxcharacters); abstractdocument doc = (abstractdocument)field.getdocument(); doc.setdocumentfilter(filter); doc.adddocumentlistener(doclistener); return field; } } public class maxlengthautofocustransferdelegate implements autofocustransferdelegate { private int maxlength; public maxlengthautofocustransferdelegate(int maxlength) { this.maxlength = maxlength; } @override public boolean shouldtransferfocus(document doc) { return doc.getlength() >= maxlength; } } public class sizefilter extends documentfilter { private int maxcharacters; public sizefilter(int maxchars) { maxcharacters = maxchars; } public void insertstring(filterbypass fb, int offs, string str, attributeset a) throws badlocationexception { if ((fb.getdocument().getlength() + str.length()) <= maxcharacters) { super.insertstring(fb, offs, str, a); } } public void replace(filterbypass fb, int offs, int length, string str, attributeset a) throws badlocationexception { if ((fb.getdocument().getlength() + str.length() - length) <= maxcharacters) { super.replace(fb, offs, length, str, a); } } } public class autofocustransferhandler implements documentlistener { private autofocustransferlistener listener; private autofocustransferdelegate delegate; public autofocustransferhandler(autofocustransferlistener listener, autofocustransferdelegate delegate) { this.listener = listener; this.delegate = delegate; } @override public void insertupdate(documentevent e) { checkfortransfer(e.getdocument()); } @override public void removeupdate(documentevent e) { checkfortransfer(e.getdocument()); } @override public void changedupdate(documentevent e) { checkfortransfer(e.getdocument()); } public void checkfortransfer(document doc) { if (delegate != null && delegate.shouldtransferfocus(doc)) { if (listener != null) { listener.focustransfershouldoccur(doc); } } } } public interface autofocustransferdelegate { public boolean shouldtransferfocus(document doc); } public interface autofocustransferlistener { public void focustransfershouldoccur(document doc); } }
Comments
Post a Comment