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.
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
un_fosused firing the same events a
EDIT
in Win7 compiled in JDK7_011 flashing JButtons (focused in Java window) with blue Color
flashing with blue color on second period
and
Related
I want JOptionPane to appear above modeless dialogs. For example, in the following app please press the JDialog button to show a modeless dialog, and then press the JOptionPane button to show a JOptionPane confirmation dialog. Unfortunately, the JOptionPane appears under the modeless dialog.
In my real app, I have several modeless JDialogs, and I use JOptionPane from several different places.
How can I easily make the JOptionPane appear above all the modeless JDialog instances?
By "easily" I mean by adding 1 or 2 lines to each modeless JDialog construction or to each JOptionPane invocation.
One way I tried was to make a new temporary unowned JFrame with always-on-top option as the owner of the JOptionPane. This makes the JOptionPane on top, but the JOptionPane is in center of the screen instead of the center of original JFrame, and I worry that the user may not notice it.
Another way I tried was to make all modeless dialogs invisible before showing the JOptionPane and then making them visible again afterward. But this way is not easy to put round all calls to JOptionPane, because (I believe) it requires a try-finally block to do reliably.
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
public class App {
public static void main(String[] args) {
JFrame f = new JFrame("App Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnDialog = new JButton("JDialog");
JButton btnOptionPane = new JButton("JOptionPane");
btnDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dlg = new JDialog(f, "Modeless Dialog", false);
dlg.setSize(256, 256);
dlg.setLocationRelativeTo(f);
dlg.setVisible(true);
}
});
btnOptionPane.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(f, "Confirm JOptionPane");
}
});
f.add(btnDialog, BorderLayout.WEST);
f.add(btnOptionPane, BorderLayout.EAST);
f.setSize(512, 512);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
You need to set the correct parent for your option pane. To determine it you can use the list of all opened windows. In my example I use the last opened window.
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class App {
public static void main(String[] args) {
JFrame f = new JFrame("App Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnDialog = new JButton("JDialog");
JButton btnOptionPane = new JButton("JOptionPane");
btnDialog.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JDialog dlg = new JDialog(f, "Modeless Dialog", false);
dlg.setSize(256, 256);
dlg.setLocationRelativeTo(f);
dlg.setVisible(true);
}
});
btnOptionPane.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(findLatestWindow(), "Confirm JOptionPane");
}
});
f.add(btnDialog, BorderLayout.WEST);
f.add(btnOptionPane, BorderLayout.EAST);
f.setSize(512, 512);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static Window findLatestWindow() {
Window result = null;
for (Window w : Window.getWindows()) {
if (w.isVisible()) {
result = w;
}
}
return result;
}
}
If you have more than one dialog opened at the same time, and user can switch between these dialogs, so you need some more lines of code. Because in your case after button click the frame is always the focus owner.
After trying and experimenting with #Sergiy's idea of using static Window methods I came up with this:
import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class App {
static JFrame hideOwnedWindows(JFrame f) {
ArrayList<Window> arHidden = new ArrayList();
WindowAdapter wa = new WindowAdapter() {
#Override
public void windowActivated(WindowEvent e) {
for (Window w : arHidden)
w.setVisible(true);
f.removeWindowListener(this);
}
};
for (Window w : f.getOwnedWindows()) {
if (w.isVisible()) {
w.setVisible(false);
arHidden.add(w);
}
}
f.addWindowListener(wa);
return f;
}
public static void main(String[] args) {
JFrame f = new JFrame("App Frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btnDialog = new JButton("JDialog");
JButton btnOptionPane = new JButton("JOptionPane");
btnDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dlg = new JDialog(f, "Modeless Dialog", false);
dlg.setSize(256, 256);
dlg.setLocationRelativeTo(f);
dlg.setVisible(true);
}
});
btnOptionPane.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showConfirmDialog(hideOwnedWindows(f), "Confirm JOptionPane");
}
});
f.add(btnDialog, BorderLayout.WEST);
f.add(btnOptionPane, BorderLayout.EAST);
f.setSize(512, 512);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
The "hideOwnedWindows" method hides all owned windows including the dialogs, and then restores them next time the main JFrame is activated. As all owned Windows are invisible during the JOptionPane, I think (hope) the main JFrame is always activated when the JOptionPane closes.
I have noticed that all JOptionPane method "interfere" with ActionListeners.
I need ActionListener to remain active after a JOptionPane has been opened.
For example:
I have a JButton, I register the mouse being pressed and draw the button red. Upon being released, I draw it blue.
If I just click it, the button will turn blue. Ok
If I hold it clicked, the button will stay red. Ok
If I click it and set it to open a JOptionPane dialog, it stays red, even though I have released the mouse. Not Ok
I haven't been able to find any documentation on this specific behaviour, can someone point me in the right direction?
I do really need to use JOptionPane.
One option -- queue the call to open the JOptionPane on the Swing event queue. This will delay the opening of the modal JOptionPane just a little bit, allowing other button actions to be performed.
Another option is to extract the JDialog from the JOptionPane, and call it in a non-modal way.
For example:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TestOptionPane extends JPanel {
private static final Color FOREGROUND = Color.RED;
private static final Color PRESSED_FG = Color.BLUE;
private JButton button1 = new JButton(new Button1Action());
private JButton button2 = new JButton(new Button1Action());
public TestOptionPane() {
setPreferredSize(new Dimension(600, 450));
button1.getModel().addChangeListener(new ButtonModelListener(button1));
button1.setForeground(FOREGROUND);
add(button1);
button2.getModel().addChangeListener(new ButtonModelListener(button2));
button2.setForeground(FOREGROUND);
add(button2);
}
private class Button1Action extends AbstractAction {
public Button1Action() {
super("Queue JOptionPane on Swing event thread");
}
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(() -> {
JOptionPane.showMessageDialog(TestOptionPane.this, "hello");
});
}
}
private class Button2Action extends AbstractAction {
public Button2Action() {
super("Show non-modal JOptionPane");
}
#Override
public void actionPerformed(ActionEvent e) {
SwingUtilities.invokeLater(() -> {
Component parentComponent = TestOptionPane.this;
JOptionPane optionPane = new JOptionPane("Hello", JOptionPane.PLAIN_MESSAGE);
JDialog dialog = optionPane.createDialog(parentComponent, "Fubars Rule!");
dialog.setModalityType(ModalityType.MODELESS);
dialog.setLocationRelativeTo(parentComponent);
dialog.setVisible(true);
});
}
}
private class ButtonModelListener implements ChangeListener {
private JButton button;
public ButtonModelListener(JButton button) {
this.button = button;
}
#Override
public void stateChanged(ChangeEvent e) {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isPressed()) {
button.setForeground(PRESSED_FG);
} else {
button.setForeground(FOREGROUND);
}
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("TestOptionPane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TestOptionPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
I am trying to test a Swing component that shows a menu when the context menu key is pressed. I simply get a focus in the component and send a press & release events by the awt Robot. It works on Windows but not on Linux. Here is an example Java code that shows the key event integer code. When this is run, the key event is set to 0. However when you press the key physically, it appears correctly as 525.
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class ContextMenuKeyTest {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel("Type something");
JTextField comp = new JTextField("Hello World!");
comp.setPreferredSize(new Dimension(300,100));
panel.add(label);
panel.add(comp);
comp.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyPressed(KeyEvent e) {
label.setText("Pressed: " + e.getKeyCode());
}
#Override
public void keyReleased(KeyEvent e) {}
});
frame.add(panel);
frame.pack();
frame.setVisible(true);
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_CONTEXT_MENU);
robot.keyRelease(KeyEvent.VK_CONTEXT_MENU);
} catch (AWTException e) {
e.printStackTrace();
}
}
});
}
}
The windows key also doesn't work properly when pressed by the robot and works just fine when pressed physically. Other keys work just fine.
Tested on Ubuntu with IceWM and on Debian with Xfce with Java 8.
I am not sure why the key doesn't work when using Robot. As a workaround I am using a xdotool that sends the key by calling:
xdotool key Menu
In my JFrame I have this listener:
this.addKeyListener(new KeyAdapter(){
#Override
public void keyPressed(KeyEvent arg0) {
//do stuff
}
});
This has been working fine until about 10 minutes ago. Now when I press a key, keyPressed() never even gets called. I tested this with eclipse debugger.
I have no idea what happened. Any ideas?
EDIT #1: This is also happening with a button that I have set up. the action listener does not recognize when the button is clicked.
EDIT #2: Ok So I was able to narrow it down. I have a JFrame, this frame has a main panel and also 2 action listeners (keyListener, the problematic one, and a mouse click listener, working fine). The main panel has two subpanels a and b. panel a has 2 buttons, one is not set up yet. It seems that these buttons are somehow conflicting with the keylistener making it so the key listener and buttons do not work. either way the mouse click listener still works.
Edit #3: Ok here is some simplified code:
The button is working, but the keyListener is not. I am hearing about focus a lot, if this is the problem how can I fix it?
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ListenerTest extends JFrame {
private JPanel mainPanel;
private JPanel panelA;
private JPanel panelB;
private JButton buttonA;
private JButton buttonB;
public ListenerTest() {
mainPanel = new JPanel();
panelA = new JPanel();
panelB = new JPanel();
buttonA = new JButton("Button A");
buttonB = new JButton("Button B");
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyChar());
}
});
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
System.out.println(e.getX() + ", " + e.getY());
}
});
buttonA.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button A pressed!");
}
});
panelA.add(buttonA);
panelA.add(buttonB);
mainPanel.add(panelA);
mainPanel.add(panelB);
this.add(mainPanel);
this.setSize(300, 300);
this.setVisible(true);
}
public static void main(String args[]) {
new ListenerTest();
}
}
When you add an action for a specific key (for example: F2) to panelA, this seems to work:
public ListenerTest() {
// Create components...
panelA.getActionMap().put("saveAction", new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("F2");
}
});
//panelA.getInputMap().put(KeyStroke.getKeyStroke("F2"), "saveAction");
panelA.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
.put(KeyStroke.getKeyStroke("F2"), "saveAction");
//this.addKeyListener(new KeyAdapter() {
//mainPanel.addKeyListener(new KeyAdapter() {
panelA.addKeyListener(new KeyAdapter() {
//buttonA.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
System.out.println("Panel A: " + arg0.getKeyChar());
}
});
// Rest of the code...
}
Adding the action also seems to have a side effect: the KeyListener seems to work again for panelA. The focus no longer goes to one of the buttons by default.
Note: when using panelA.getInputMap(), the key action only works when the buttons do not have the focus. Use panelA.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) to make sure the action works when one of the buttons has the focus.
On my Mac, fullscreen JFrames initially have key bindings that do not work, and the computer outputs alert beeps each time I try to type. There is a workaround, though, after fully initializing my JFrame, I added these lines of code and all the errors stopped:
setVisible(false);
setVisible(true);
Here's the source of this workaround: http://mail.openjdk.java.net/pipermail/macosx-port-dev/2012-November/005109.html
Another problem which is yet to be solved is adding a mouse adapter to my full screen JFrame application. Whenever I clicked, the focus changed--to where, I couldn't quite tell, but setting the inputmap of my keybindings to each one of the three options didn't help.
I even tried redoing the workaround when the mouse was clicked by adding this:
event.getComponent().setVisible(false);
event.getComponent().setVisible(true);
but to no avail.
Here is an SSCCE of the problem (it will only show up on a mac):
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class FocusTest extends JFrame{
private static final int PREF_W = 400;
private static final int PREF_H = PREF_W;
public FocusTest() {
MyPanelDescendent myPanelDescendent = new MyPanelDescendent();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(myPanelDescendent);
pack();
setLocationByPlatform(true);
setVisible(true);
KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
Action escapeAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}
};
getRootPane().getInputMap(JComponent.WHEN_FOCUSED).put(escapeKeyStroke, "ESCAPE");
getRootPane().getActionMap().put("ESCAPE", escapeAction);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
gs.setFullScreenWindow(this);
setVisible(false);
setVisible(true);
}
private class MyPanelAscendent extends JPanel{
public MyPanelAscendent() {
setFocusable(true);
requestFocusInWindow();
getInputMap(0).put(KeyStroke.getKeyStroke("pressed A"), "pressed");
getActionMap().put("pressed", new AbstractAction() {
#Override public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("a")) {
System.out.println("a was pressed");
}
}
});
addMouseListener(new MyAdapter());
}
}
private class MyPanelDescendent extends MyPanelAscendent {
public MyPanelDescendent() {
super();
}
}
private class MyAdapter extends MouseAdapter {
#Override
public void mouseClicked(MouseEvent event) {
event.getComponent().setVisible(false);
event.getComponent().setVisible(true);
System.out.println("clicked");
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(PREF_W, PREF_H);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FocusTest();
}
});
}
}
If you press the a key, then click, then do it again, it will not work. The same goes for the escape key: if you click then try to use it, it won't work.
here is an example for fullscreen posted by trashgod which I've also found impossible to make work with both keybindings, fullscreen, and a mouse adapter at the same time.