JDK-6607166 : Swing Becomes Very Sluggish When Attempting to displayDialog with setModal(true)
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: solaris_7
  • CPU: sparc
  • Submitted: 2007-09-20
  • Updated: 2010-04-04
  • Resolved: 2007-10-08
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
SunOS cfsc.pbn.bnl.gov 5.7 Generic_106541-46 sun4u sparc SUNW,Ultra-60



A DESCRIPTION OF THE PROBLEM :
When Attempting to display a dialog with setModal(true) the dialog takes a noticeably (unacceptably) longer time to display when more than one JFrame is visible.  This is not noticeable on previous versions of the jdk nor is it noticeable on Linux with jdk1.6.  If the second JFrame is closed the dialog responds quickly again.

I use the dialog window as the example here but I've actually been able to extend this problem to menu selections being slow to respond as well.  The example below only demonstrates the dialog problem.  Because of this I think the problem has more to do with displaying a second jframe rather than bringing up a dialog with setModal set to true.
I had thought I could work around it by using the setModalExclusionType of the JFrame and that kind of worked but I think it only avoids the real problem rather than corrects the problem.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run my program below.
Click "Show Dialog" and notice that the response is relatively quick to display the dialog.  Close the dialog window.
Click "Show Frame".  Then click "Show Dialog" again.
Notice that when the second JFrame is visible the button press is very slow to respond.  If you close the second JFrame and try again the dialog will be responsive again.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I'd like the performance to be independent of whether or not multiple Frames are displayed.
ACTUAL -
The slowness in the swing components.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No Error Messages

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import javax.swing.*;

import java.awt.event.*;
import java.awt.*;

/* Simple program to show how displaying a dialog window when a second jframe
   is displayed is very slow on SUN */
public class Slow extends JFrame {

    public static void main(String args[]) {
        // Slow is the "main" window
        Slow s = new Slow();
        s.setVisible(true);
    }

    public Slow() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        JPanel panel = (JPanel) getContentPane();
        panel.setLayout(new GridLayout());
        panel.add(_button1);
        panel.add(_button2);

        _button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (_frame2 == null)
                        _frame2 = new Frame();
                    _frame2.setVisible(true);
                }
            });

        _button2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (_dialog == null)
                        createDialogWindow();
                    _dialog.setVisible(true);
                }
            });

        pack();
    }

    public void createDialogWindow() {
        _dialog = new JDialog(this);
        Container content = _dialog.getContentPane();
        content.add(new JLabel("Dialog Window"));
        // setModal seems to cause all the problems
        _dialog.setModal(true);
        _dialog.pack();
    }

    JButton _button1 = new JButton("Show Frame");
    JButton _button2 = new JButton("Show Dialog");
    JDialog _dialog = null;
    JFrame  _frame2 = null;

    class Frame extends JFrame {
        public Frame() {
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);

            JPanel panel = (JPanel) getContentPane();
            JLabel l = new JLabel("Click 'Show Dialog' and response to click is slow");
            panel.add(l);

            pack();
        }
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I thought I was able to use JFrame's setModalExclusionType to work around the problem and it does to a point.  But I think this is definitely a workaround and doesn't completely eliminate the problem.

Comments
EVALUATION This problem can be observed both on linux and solaris with all the window managers running, but with different delays. It has been addressed with the fix for 6518077, but the fix is only partial and delays are still noticeable for a large number of visible frames - see 6541375 for details.
08-10-2007