# Overriding closeButtonPressed event so JUCEApplication is minimized instead of getting closed

**URL:** <https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089>\
**Category:** Development\
**Created:** [November 30, 2016, 9:22am UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089 "2016-11-30T09:22:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amitai](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/amitai/32/1922_2.png) [@Amitai](https://forum.juce.com/u/Amitai)\
**Post date:** [November 30, 2016, 9:22am UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089/1 "2016-11-30T09:22:39Z")

</div>

Hello,

I’m trying to implement minimization of an application when ‘X’ button is being pressed from the toolbar (on both Mac & Windows platforms).  
I see that closeButtonPressed() method is being called as a result of **any** application closure flow, without any clear way to override/filter the exact action being taken for closure. I want to filter out the toolbar close button pressing from the other terminating actions and call setMinimised(true) instead.

Thanks in advance,  
Amitai

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 1, 2016, 11:24am UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089/2 "2016-12-01T11:24:23Z")

</div>

If you’re not using the native title bar in your application then you can use the DocumentWindow::getCloseButton() method to add a listener to the button that is returned and then minimise your window in the buttonClicked() callback.

Ed

---

<div class="post-metadata">

**Author:** ![Amitai](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/amitai/32/1922_2.png) [@Amitai](https://forum.juce.com/u/Amitai)\
**Post date:** [December 1, 2016, 12:17pm UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089/3 "2016-12-01T12:17:17Z")

</div>

Hi Ed,

Thanks for answering. I’ve tried that already. The pointer I get is always Null for some reason. It seems impossible to use getCloseButton() for this purpose. By the way, while doing that I saw that JUCE throws an assertion when trying to register as a listener to buttons from a DocumentWindow-derived object.

Thanks,  
Amitai

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [December 1, 2016, 2:14pm UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089/4 "2016-12-01T14:14:44Z")

</div>

Are you using the native title bar? The DocumentWindow only returns a valid pointer to a Button object if you’re using JUCE’s title bar otherwise it will return nullptr. Try calling setUsingNativeTitleBar (false) in the constructor of your DocumentWindow subclass.

Ed

---

<div class="post-metadata">

**Author:** ![Amitai](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/amitai/32/1922_2.png) [@Amitai](https://forum.juce.com/u/Amitai)\
**Post date:** [December 4, 2016, 8:58am UTC](https://forum.juce.com/t/overriding-closebuttonpressed-event-so-juceapplication-is-minimized-instead-of-getting-closed/20089/5 "2016-12-04T08:58:37Z")

</div>

I tried to use setUsingNativeTitleBar (false) and it is working indeed. Why does it work like this? I want to use the native title bar and still get the close button event.
