java - Traverse JDateChooser with Enter Key -
java - Traverse JDateChooser with Enter Key -
i input validation when come in key pressed in jdatechooser
. know in jtextfield
elements possible add together actionlistener
whereby come in key fires action. however, when add together actionlistener
, press come in in date chooser, action not received.
in below example, pressing come in key when programme first starts fires action in jdatechooser
, focus traverses next component expected. however, in subsequent traversals, have come in character before action fired. action fired in 2 jtextfield
elements expected.
can explain why come in key not behave same when adding actionlistener
editor of jdatechooser
?
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.emptyborder; import com.toedter.calendar.jdatechooser; public class dateexample extends jframe implements actionlistener { private static final long serialversionuid = 1l; private jpanel contentpane; private jtextfield textfield; private jtextfield textfield_1; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { seek { dateexample frame = new dateexample(); frame.setvisible(true); } grab (exception e) { e.printstacktrace(); } } }); } public dateexample() { setdefaultcloseoperation(jframe.exit_on_close); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new gridlayout(0, 1, 0, 0)); jdatechooser datechooser = new jdatechooser("yyyy/mm/dd", "####/##/##", '_'); ((jtextfield) datechooser.getdateeditor().getuicomponent()).addactionlistener(this); contentpane.add(datechooser); textfield = new jtextfield(); textfield.setcolumns(10); textfield.addactionlistener(this); contentpane.add(textfield); textfield_1 = new jtextfield(); textfield_1.setcolumns(10); textfield_1.addactionlistener(this); contentpane.add(textfield_1); pack(); } @override public void actionperformed(actionevent e) { system.out.println("action received."); keyboardfocusmanager.getcurrentkeyboardfocusmanager().focusnextcomponent(); } }
for validating can add together caretlistener editor or can extend jdatechooser , overwrite propertychange method.
on personal experience, ended creating own textfielddateeditor
public class mydateeditor implements com.toedter.calendar.idateeditor
and called
public jdatechooser(date date, string dateformatstring, idateeditor dateeditor)
that way can interact textfield like.
java swing focus jcalendar
Comments
Post a Comment