Jong
Jong

Reputation: 131

JOptionPane showing outside JFrame with GraphicsDevice

package javaapplication1;

import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
        frame.setVisible(true);

        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(frame, "JOptionPane");
            }
        });
    }
}

When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane that I triggered. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar. Looking forward for your answers. Thanks in advance. Or is there any other alternative to this?

Upvotes: 2

Views: 2090

Answers (2)

Jong
Jong

Reputation: 131

JOptionPane.showInternalMessageDialog(frame.getContentPane(), "JOptionPane");

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168815

That code works for me, though you might try this variant with 2 changes.

  1. It creates and shows the GUI on the EDT.
  2. It uses the content pane of the frame as the parent of the JOptionPane

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

public class JavaApplication1 {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            public void run() {
                final JFrame frame = new JFrame();
                frame.setTitle("Frame");
                frame.setSize(800, 600);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                device.setFullScreenWindow(frame);
                device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
                frame.setVisible(true);

                JButton btn = new JButton();
                btn.setText("Button");
                JPanel panel = new JPanel();

                panel.add(btn);
                frame.add(panel);

                btn.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        //JOptionPane.showMessageDialog(frame, "JOptionPane");
                        JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
                    }
                });
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

Update

When I add the following lines to the beginning of the source seen above..

System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));

..the output & result is as follows.

Running in 1.7

Result: Failure as described in the question.

1.7.0_09
23.5-b02

Running in 1.6

Result: Success with no unusual artifacts or behavior.

1.6.0
1.6.0-b105

Analysis

Note that other results from comments suggest the behavior changed some time between that early 1.6 version, and 1.6.0_25. It seems like a regression bug. The OP should check the bug database & if nothing likely shows up, lodge a new report.

Upvotes: 6

Related Questions