java - HOWTO Select JSpinner date field when JSpinner gets focus -


i trying make jspinner (containing minute/second timer) work bunch of set-in-their-ways folks don't care beans mouse want every keystroke used to. works i'd last mile hardest.

what means in practice spinner should , behave when 1 of text fields gains focus looks , behaves after user has pressed arrow key 1 of fields.

spinner created follows:

this.countdown = new jspinner(this.model); this.editor = new jspinner.dateeditor(countdown, "m:ss"); jformattedtextfield textfield = editor.gettextfield(); 

step 1. when spinner comes looks this: ('|' indicates caret, bold indicates selction)

|1:00
(nothing selected)

step 2. if arrow pressed here looks this:

2|:00
(2 in minutes field selected)

step 3. if right arrow pressed here get:

2:|00
(nothing selected)

step 4. if arrow pressed here get

2:01|
(01 in seconds field selected)

i'd work in these cases in steps 2 , 4. when 1 of subfields gains focus, should selected.

is there way make happen?

maybe can use focuslistener.

import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*;  public class spinnerfocustest {   private static final string pattern = "m:ss";   public jcomponent makeui() {     calendar c = calendar.getinstance();     c.settimeinmillis(60*1000);     date value = c.gettime();      spinnerdatemodel m = new spinnerdatemodel(         value, null, null, calendar.minute);      jspinner sp1 = new jspinner(m);     sp1.seteditor(new jspinner.dateeditor(sp1, pattern));      jspinner sp2 = new jspinner(m);     final jspinner.dateeditor editor =       new jspinner.dateeditor(sp2, pattern);     sp2.seteditor(editor);     editor.gettextfield().addfocuslistener(new focusadapter() {       @override public void focusgained(focusevent e) {         eventqueue.invokelater(new runnable() {           @override public void run() {             jtextfield f = editor.gettextfield();             int = f.gettext().lastindexof(":");             f.select(i+1, i+3);           }         });       }     });      jpanel p = new jpanel(new gridlayout(2,1,5,5));     p.add(sp1);     p.add(sp2);     jpanel panel = new jpanel(new borderlayout());     panel.add(p, borderlayout.north);     panel.setborder(borderfactory.createemptyborder(8,8,8,8));     return panel;   }   public static void main(string[] args) {     eventqueue.invokelater(new runnable() {       @override public void run() {         createandshowgui();       }     });   }   public static void createandshowgui() {     jframe f = new jframe("");     f.setdefaultcloseoperation(windowconstants.exit_on_close);     f.getcontentpane().add(new spinnerfocustest().makeui());     f.setsize(320, 240);     f.setlocationrelativeto(null);     f.setvisible(true);   } } 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -