Search code examples
javawindowjpanelfullscreenjoptionpane

How to make JOptionPane on top of a Fullscreen Window?


I couldn't find any spesific solution for Fullscreen Window so I want to ask. How to make a JOptionPane on the top of a Fullscreen Window?

In here , Im taking a Fullscreen window as a constructor parameter. But when I press ESC , my window sending itself to background, like I pressed "Alt+Tab". Then if I click on if from start menu bar , I can see it on top of window. But I want to end this sending background problem.How can I fix it?

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

public class KeyListenerTest extends JFrame implements KeyListener {
  private Window windo;
  private ImageIcon quitImage;

  public KeyListenerTest(Window window)
  {
    quitImage = new ImageIcon("quitask.png");
    windo = window;
    addKeyListener(this);
  }

  public void keyPressed(KeyEvent e) {  
    if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
      JOptionPane opti = new JOptionPane();
      int choice = opti.showConfirmDialog(windo, "Do you really want to quit?", "QUIT", JOptionPane.YES_NO_OPTION , 
                                          JOptionPane.QUESTION_MESSAGE  , quitImage);
      opti.requestFocusInWindow();
      if(choice == JOptionPane.YES_OPTION)
      {
        System.exit(0);
      }
      else
      {
        dispose();
        System.out.println("ESC key typed");
      }
    }
  }  
  public void keyTyped(KeyEvent e) {
  }

  public void keyReleased(KeyEvent e) {
  } 
}

And here is my code for making my window fullscreen

  window=(Window)frame;
  window.setFocusable(true);

  KeyListenerTest keyo = new KeyListenerTest(window);
  frame.addKeyListener(keyo);

  g.setFullScreenWindow(window);

Solution

  • Use:

    frame.setSize(300, 200); //or any other size you want for JFrame after changeing from maximalized state
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    

    instead of:

    g.setFullScreenWindow(window);
    

    works for me.