java - Prevent breaking of JTextField / JDialog with repeated transferFocus() -


i have strange scenario unfortunately cannot prevent occurring in swing application. when occurs however, has major consequences me. perhaps help!

the basic setup follows:

  • linux environment.
  • multiple jtextfields in jframe.
  • jtextfields push through transferfocus() when enter key pressed.
  • a jdialog pops on leaving 1 of fields requires enter key pressed remove it.

the situation causes issue follows:

  • the enter key held down few seconds.

when enter key held down, focus flies through different text fields. when dialog box shown, enter key closes causing focus continue fly through text fields. eventually, within couple of seconds, java breaks. textboxes stop responding key strokes - cannot type in them @ all. other that, seems normal - can click around , focus on different textboxes, close application etc.

i have created simple test case can use recreate situation.

the jframe:

public class testswing extends jframe {      jtextfield jtftext1, jtftext2, jtftext3;     texthandler handler = null;      public testswing() {         super("textfield test demo");         container container = getcontentpane();         container.setlayout(new flowlayout());         jtftext1 = new myjtextfield(10);         jtftext2 = new myjtextfield(10);         jtftext3 = new myjtextfield(10);         container.add(jtftext1);         container.add(jtftext2);         container.add(jtftext3);         handler = new texthandler();         jtftext3.addactionlistener(handler);         setsize(325, 100);         setvisible(true);     }     private class texthandler implements actionlistener {         public void actionperformed(actionevent e) {             joptionpane.showconfirmdialog(null, "wait!");         }     }     public static void main(string args[]) {         testswing test = new testswing();         test.setdefaultcloseoperation(jframe.exit_on_close);     } } 

the custom jtextfield:

public class myjtextfield extends jtextfield {         public myjtextfield(int len) {         super(len);         addkeylistener(new keyadapter() {               public void keypressed(keyevent evt) {                 int key = evt.getkeycode();                 if (key == keyevent.vk_enter)                   transferfocus();               }         });     } } 

to answer potential questions front:

  • the enter key must used transfer focus.
  • the spamming of enter key comes user leaving on keyboard (this in retail environment happens often).
  • simply closing , restarting application not option there no mouse plugged computer. application booted automatically on start-up making scenario devastating way fix problem restart machine.
  • the machines aren't powerful (processing & memory) somehow causes issue happen lot quicker when it's recreated on development machine.

is bug in java? can think of way prevent happening?

the closest can preventing happening put sleep(500) call in jdialog (mine extended) before closes that's not great fix...

i have tested in jdk 1.6, 1.7 , 1.8. while takes bit longer in later versions textboxes become unresponsive, still happens eventually.

thanks in advance!

xandel

don't use keyevents. keyevents used in awt. swing has newer , better api's use (in cases). in case jtextfield designed respond actionevent when enter key pressed.

you try keep track of last time enter pressed , ignore events seem invoked within repeat rate of os. repeat rate appears around 35ms:

import java.awt.event.*; import javax.swing.*;  public class myjtextfield extends jtextfield {     private static long lasttime = system.currenttimemillis();      public myjtextfield(int len)     {         super(len);         addactionlistener(new actionlistener()         {             public void actionperformed(actionevent evt)             {                  long diff = evt.getwhen() - lasttime;                 system.out.println(diff);                  if (diff > 50)                 {                     transferfocus();                 }                  lasttime = evt.getwhen();             }         });     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -