1

When dialog window displayed it does not focused if other window is active (i.e. you need to focus it by mouse pointer to be able to deal with it). How can I make a focus to displayed dialog?

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class GuiTest {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {

        JFrame frame = new JFrame("frame");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setAlwaysOnTop(true);

        JOptionPane.showMessageDialog(frame, "test info", "test header",
                JOptionPane.INFORMATION_MESSAGE);

        //frame.toFront();
        //frame.requestFocus();

        frame.dispose(); // When a frame is disposed, the exit action will be
                            // called.

    }

}
  • 1
    you need to call `setVisible(true)` on it – Emerson Cod Mar 06 '18 at 14:04
  • Presumably it's not the frame but the option pane that needs focus. See [this answer](https://stackoverflow.com/a/6626803/418556). – Andrew Thompson Mar 06 '18 at 14:08
  • 1
    It looks like you are not using `frame` for anything here. Why do you have it in your code at all? – Pshemo Mar 06 '18 at 14:23
  • @Pshemo to make the dialog on top when other window is active and now the dialog need to be focused – user9172919 Mar 06 '18 at 14:27
  • What other window? If I remember correctly by default dialog box should be focused and on top of screen (`null` passed as first argument should ensure that it should be in center of desktop). If it is not happening then possibly you are doing something changing that behavior which you are not showing us. If that is the case then to help you properly we will need to see [mcve]. – Pshemo Mar 06 '18 at 14:37
  • @Pshemo sorry there were an error, now `frame` is used and if you run it when other window is active than it will be clear that dialog is not active – user9172919 Mar 06 '18 at 14:49
  • did you followed this link :https://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Component.html#requestFocus() – Elarbi Mohamed Aymen Mar 06 '18 at 14:51
  • 1
    Sorry, but that code still makes dialog box on top and with focus (I can press space/enter and close it). – Pshemo Mar 06 '18 at 14:51
  • 1
    @Pshemo try to run it and right after that make any other window active. maybe you need increase Thread.sleep amount – user9172919 Mar 06 '18 at 15:05
  • @ElarbiMohamedAymen I used `frame.getRootPane().requestFocusInWindow();` but seems it does not change the behaviour – user9172919 Mar 06 '18 at 15:08
  • OK, I see the problem now. – Pshemo Mar 06 '18 at 15:10

2 Answers2

0

JOptionPane.showMessageDialog stops the EDT (Event Dispatching Thread) until the dialog is closed.

You could instead use another JFrame instead of a JOptionPane

package util;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuiTest {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GuiTest window = new GuiTest();
                    window.frame.setVisible(true);

                    Dialog d = new Dialog();
                    d.show();

                    window.frame.requestFocus();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

    /**
     * Create the application.
     */
    public GuiTest() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

class Dialog extends JFrame {
    private JFrame frame;

    public void show() {

        if (frame == null) {
            frame = new JFrame();
            frame.setTitle("Dialog");

            frame.setBounds(100, 100, 450, 300);
            frame.add(new JTextField("Hello"));
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        }

        frame.setVisible(true);
    }

}
Alde
  • 428
  • 1
  • 5
  • 20
0

try to run it and right after that make any other window active

Once a window loses focus you need to either:

  1. click on the window to give it focus
  2. Use the desktop manager to access the application. In Windows this is done by using Alt+Tab to cycle through open applications.

The problem is that only a JFrame is added to the desktop manager so if you want the ability to use Alt+Tab then you need to make the frame visible BEFORE showing the option pane.

Of course the JFrame will now be visible on the screen so you can use the setLocation(...) method with a negative value to hide the frame from the visible desktop.

camickr
  • 321,443
  • 19
  • 166
  • 288