0

I have made an SSCCE. Please note that it must be Windows Look&Feel.

import java.awt.*;
import javax.swing.*;

public class DefaultButtonBug {
private static final String LAF_WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

public static void main(String[] args) {

    try {
        UIManager.setLookAndFeel(LAF_WINDOWS);
    } catch (Exception ex) {
        System.out.println("Setting the L&F failed so I cannot reproduce the bug.");
        System.exit(1);
    }

    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            JPanel content = new JPanel();
            JButton defaultButton = new JButton("Default");

            content.add(defaultButton);

            JFrame frame = new JFrame();

            frame.getRootPane().setDefaultButton(defaultButton);
            frame.setContentPane(content);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);                
        }
    });
}
}
  • Launch this
  • The button should be focused. If not, click it.
  • Click on any other window, to make sure this current window loses the focus
  • The button keeps animating in blue tints, even when this window has no focus anymore!

The button 'pulsing' animation is something not present in the standard Java L&F.

Remark that when this button is no longer the default button (remove the appropriate line in the code), the button will be gray after the window loses focus and there is no animation whatsoever.

My question to you is: is this considered a bug? Because this makes the EDT keep doing stuff instead of being idle when the window is hidden behind another window too (I did some profiling). Indeed, that's the stuff that bothers me the most of all: hiding the window does not make the EDT go idle.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Timmos
  • 3,215
  • 2
  • 32
  • 40
  • no issue, tested on WinXp, 7, 8 compiled in Java 6 / 7 – mKorbel Mar 21 '13 at 13:20
  • @mKorbel Does your Look&Feel under WinXP has that glowing/pulsing animation on focused buttons? – Timmos Mar 21 '13 at 13:21
  • and JButton is accesible for mousehover over events in Win8 / Java 7 on focusLost (another window has focus in OS) – mKorbel Mar 21 '13 at 13:22
  • 1
    What is the exact bug? The fact that the button "pulsing" animation keeps on playing although the Window lost focus? The fact that the Window does not have the focus does not mean that the button loose its own. Moreover, the animation is performed using a Swing Timer (see `com.sun.java.swing.plaf.windows.AnimationController`) which is triggered every 1000/30 ms. So most of the time the EDT is idling and the cost of that animation is pretty much nothing. – Guillaume Polet Mar 21 '13 at 13:23
  • are we talking only about WinXP and Java6 ??? (I have only stable version ends with 022 and 031) – mKorbel Mar 21 '13 at 13:24
  • No, Windows 7 and Java 7. – Timmos Mar 21 '13 at 13:58
  • you are right, my bad I didn't compiled that in JDK7, only in JDK6 then there all focused JButton JComponents flashing with blue Color, ignored used Colors from theme in Win7 but I didn't saw that on WinXP and Win8 – mKorbel Mar 21 '13 at 14:40
  • `Does your Look&Feel under WinXP has that glowing/pulsing animation on focused buttons`. This happens whether the button is the default button or not. Add a second button to your little demo and see the same behaviour for that button. The button that has focus is the one that animates the color even if the frame doesn't have focus. – camickr Mar 21 '13 at 15:59

1 Answers1

2

getRootPane() default button - Is this a bug?

  • not as described in a comment by @Guillaume Polet

  • but I'd be inclined to use KeyBindings, because any JComponents with FocusInWindow and added ActionListener can consume() ENTER key pressed, for all JButtons JComponents

  • focus is managable by getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT/*.WHEN_FOCUSED*/)

  • notice (Win OS) JButton has implemented TAB as an accelerator in KeyBindings, too.

from code

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class DefaultButtonBug {

    private static final String LAF_WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(LAF_WINDOWS);
        } catch (Exception ex) {
            System.out.println("Setting the L&F failed so I cannot reproduce the bug.");
            System.exit(1);
        }
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JPanel content = new JPanel();
                JButton focusedButton1 = new JButton("Focused");
                focusedButton1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Focused pressed");
                    }
                });
                content.add(focusedButton1);
                final JButton defaultButton2 = new JButton("Default");
                defaultButton2.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
                defaultButton2.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("Default pressed");
                    }
                });
                defaultButton2.getModel().addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        ButtonModel model = (ButtonModel) e.getSource();
                        if (model.isRollover()) {
                            defaultButton2.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
                        } else {
                            defaultButton2.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
                        }
                    }
                });
                content.add(defaultButton2);
                JFrame frame = new JFrame();
                frame.getRootPane().setDefaultButton(defaultButton2);
                frame.getRootPane().getInputMap(
                        JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT/*.WHEN_FOCUSED*/)
                        .put(KeyStroke.getKeyStroke("ENTER"), "clickButton");
                frame.getRootPane().getActionMap().put("clickButton", new AbstractAction() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        defaultButton2.doClick();
                    }
                });
                frame.getRootPane().setDefaultButton(defaultButton2);
                frame.setContentPane(content);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

learning item of day

  • on Win7/8 (Java6/7) are allowed mouse event on un_focused Java Window (for all standard L&F), can be listener from ChangeListener added to ButtonModel

  • doesn't work on WinXP

focused

enter image description here

enter image description here

un_fosused firing the same events a

enter image description here

EDIT

in Win7 compiled in JDK7_011 flashing JButtons (focused in Java window) with blue Color

  • flashing with blue color on second period

enter image description here

and

enter image description here

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
mKorbel
  • 109,525
  • 20
  • 134
  • 319