27

I have a window (derived from JFrame) and I want to disable the close button during certain operations which are not interruptible. I know I can make the button not do anything (or call a handler in a WindowListener) by calling

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

but I would like to make it clear visually that it is pointless to click it.

florin
  • 13,986
  • 6
  • 46
  • 47
  • Just a suggestion, you might want to consider avoiding such GUI behavior due to poor usability. – Josh Nov 09 '08 at 19:24
  • I agree with Josh. At the very least map close to a popup that says "Operation in Progress..." with a "Cancel" (hides popup) and "Close Anyway" (forces quit) button. When the background operation completes the app closes if that popup is still visible (i.e. they haven't clicked Cancel) – SCdF Nov 10 '08 at 05:49
  • possible duplicate of [How to hide the default minimize/maximize and close buttons on JFrame window in Java?](http://stackoverflow.com/questions/9101418/how-to-hide-the-default-minimize-maximize-and-close-buttons-on-jframe-window-in) – Nathan Nov 13 '14 at 00:29
  • What about Frame, instead of JFrame? – Bianca Balan Apr 25 '23 at 13:19

6 Answers6

24

This is probably the best you are going to get:

setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.NONE);

This will remove the entire titlebar, java doesn't really specify a way to remove individual components of the titlebar

edit:

There may be a way, check out these threads:

Dharmesh
  • 132
  • 1
  • 12
Malfist
  • 31,179
  • 61
  • 182
  • 269
10

If I understand it correctly, this bug report indicates that this is currently not possible.

bdumitriu
  • 1,313
  • 9
  • 12
8

This will help you:

frame.setDefaultCloseOperation(0);
Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • 1
    Why the hell was this downvoted without any comment? It blocks the close operation as requested. Although better expressed as `setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)` – Tomáš Zato Apr 18 '16 at 03:02
  • 2
    I had to use those three lines to avoid java shutting down when I have several PApplet (Processing) launched: `this.jframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE; this.jframe.setUndecorated(true); this.jframe.getRootPane().setWindowDecorationStyle(JRootPane.NONE);` – emilie zawadzki Aug 09 '16 at 13:09
  • As Tomas Zato said, It helped me ;) – Furkan Jan 09 '18 at 23:24
  • 3
    Downvoted because the original question specifically said, "I know I can make the button not do anything....by calling setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE) but I would like to make it clear visually that it is pointless to click it." – Marnee Feb 05 '20 at 17:54
5

For those coming to this later than 2008, there has been a change making it possible to do this. See this link

Second response from the bottom shows how to do it by name.

Dharmesh
  • 132
  • 1
  • 12
Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45
  • 1
    Like many other things in Swing, this too is a complete PITA. – Spencer Kormos Feb 02 '12 at 18:27
  • 1
    *Second response from the bottom*, which one is it now, 5 years later? Can link to it ("share" link at the bottom of it) or quote the core principle? Also, could you detail a bit more the "change" you're referring to? – Matthieu Aug 17 '17 at 10:50
0

To simply make them disappear, try the following:

setUndecorated(true);
0xCursor
  • 2,242
  • 4
  • 15
  • 33
-1

Please try this

frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        e.getWindow().setVisible(false);
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(WindowsActions.class.getName()).log(Level.SEVERE, null, ex);
            }
      }
    });
Ab Hin
  • 9
  • 1
  • 4
  • This not only has nothing to do with the question, but it's not even sound logic. This is a horrible thing to do in the code. NEVER do this. – searchengine27 Sep 22 '21 at 23:06