0

I experienced a weird issue in my real time stock prices GUI java application. The problem is this code:

InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
im.put(KeyStroke.getKeyStroke("released SPACE"), "none");

If you call it from main thread the whole GUI application becomes very slow, lagging when scrolling a table up/down. The fix seems to be to call the code from Swing thread.

What is going on when the wrong thread calls it?

vmuser
  • 51
  • 1

2 Answers2

1

The fix seems to be to call the code from Swing thread.

Actually the fix is to call the processing logic from a separate Thread, NOT the Event Dispatch Thread (EDT). The EDT is the Swing Thread responsible for responding to events and painting the GUI, so it should not execute long running code or the GUI becomes unresponsive.

Read the section from the Swing tutorial on Concurrency for more information. You can use a SwingWorker to execute long running code.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

The application is very fast, used for trading of many futures and options markets every day. We discovered that when specific buttons were on focus a space bar would cause an unintended click. Therefore we came with the focus input map fix. However calling this "fix" from non EDT thread would cause the whole application to function in "limp" mode, which is crazy.

This has nothing to do with any processing or races. Here is more context:

//SLOW:
public static void main(String[] args)
{
    InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
    im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
    im.put(KeyStroke.getKeyStroke("released SPACE"), "none");

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
           //just GUI no processing, no changes
            startAndRunGUI();
        }
    }
}
//FAST:
public static void main(String[] args)
{
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {

            InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
            im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
            im.put(KeyStroke.getKeyStroke("released SPACE"), "none");


            //just GUI no processing, no changes
            startAndRunGUI();
        }
    }
}
vmuser
  • 51
  • 1