Disabling the close-button on a JDialog

Is this possible? I know you can use setDefaultClosingOperation, but I want to make the button “disabled/grayed out” so that it actually looks disabled.
I have googled for a while now and still haven’t found an answer to this.

Assuming you have a custom JDialog with some contents you control, can’t you just call:


cancelButton.setEnabled(false);

?

If you’re talking about the X button in the top left corner that kills a window on windows, I’ve never seen it greyed out in a java app or seen an option to make it do that.

It is possible. Look at the child components, find this button and call setEnabled(false) on it.

Have you tried this? I doubt it is that easy because the button is in the title bar which is platform specific and thus the buttons are abstracted away and hidden from us programmers.

Hm… I know you can setUndecorated(true) on a window to hide that entire bar, but I’m not sure how to gray it out. However, I’ve seen it done so I know it’s possible. Just look around in JContainer, JWindow, etc. and see if there is some method that looks like that.

I believe it’s possible with LookAndFeels that support painting their own window decorations, such as Metal and perhaps Substance, but for others (WindowsLookAndFeel for example), you’re right that the native window frame and widgets are used, and in that case they cannot be modified. Code to grab components from the title bar in Metal can be found on the old forum.java.sun.com Swing forum.

At this point you need to step back and ask yourself if your actually going to achieve what you want to achieve - graying out a closing button is going to break user expectations and users might even think your application is unresponsive. I suggest to either have it and use it or remove it.

Good point.

An application should be cleanly user-interruptible at any stage.

Thanks for your answers, guess I’m stuck with the normal close-button :frowning:

[quote=“BoBear2681,post:7,topic:36151”]
Actually I believe this is not entirely true (at least it is for some components). For example there is a well known MacOS Swing look and feel bug where it fails to render the JTree 100% correctly (I believe it’s missing an animation), and pre-Vista versions of Java fail to render correctly on Vista and Win7 (I believe because they didn’t fully support the new Windows look and feel). It’s a copyright restriction that makes the various platform specific Swing look and feels platform specific.

So you might be able to hack the different platform specific look and feels to look greyed out. Making the close window action do nothing can be done without hacking it.

Although I agree with the others that you should not hack it. Whatever your ‘no’ or ‘cancel’ option is, have closing the dialog perform the same thing.

[quote=“JL235,post:11,topic:36151”]

Yeah, I guess I meant “native window frame and that frame’s decorations,” i.e. the close/restore/minimize buttons on Windows, were “native” when using the WindowsLookAndFeel, not other standard components such as JTree. For example, call UIManager.getLookAndFeel().getSupportsWindowDecorations() when using the MetalLookAndFeel vs. WindowsLookAndFeel. Metal is capable of rendering a JFrame’s title bar itself, and when it does, you can walk the component hierarchy and indirectly access a Component used for its “close” button. With WindowsLookAndFeel, you cannot do that.