I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Java Tips: Disabling the Maximize Button of a JFrame

  • submit to reddit

image The truth is that there is no direct way to remove the maximize button off the JFrame as it is created by Windows and it is not painted using Swing so U can’t touch this.

Still many posts on the net are giving “creative” suggestions on how to remove the maximze button of “JFrame”. The simplest and yet effective solution is to replace JFrame with JDialog as the latter does not have a maximize button. Other feasible “Java-based” solution is  remove the title bar and painstakingly implement customized title bar.

Neutralizing the effect of maximization

The solution here neutralizes the effect of maximization and flashing a error message.

There are pros and cons to this approach. The pros is that it is still using JFrame and has minimise button (JDialog has no minimize button) and can be implemented quickly with few lines of code. The cons is that according to GUI guide rule, GUI component shouldn’t be displayed on screen if it doesn’t get used at all in under any circumstances, and beside the solution actually flash the screen as the JFrame maximize and get revert to previous size immediately.

Anyway, if JFrame is to be used and maximizing should prevented, the following does the trick.

- If maximize button clicked, the JFrame will maximize and then immediate revert back to previous size. Event is only fired after resizing not before, therefore there is no way to stop the resizing (eg ComponentListener, WindowListener). At the same time, a tooltip will be flashed just on the top right corner of the JFrame.

Video demonstration


The trick is the following code segment.


So when the frame is maximized, it will immediate revert back to “restore” size while at same time giving a beep and flashing a tooltip.

So here is the source code

image File: MaximizeButtonDisabler.java (4kb)

From http://geekycoder.wordpress.com/

Your rating: None


Jesse Sightler replied on Tue, 2009/07/21 - 3:43pm

What is wrong with calling Frame.setResizable(false)?

GeekyCoder coder replied on Tue, 2009/07/21 - 9:34pm

Jesse Sightler,
setting setResizeable will work but JFrame will not be resizable anymore. My actual intent for this code is actually to create a Resizable JFrame that does not allow maximizing. I have omitted the Resizable word in my original post and have now updated it into blog post. thx

Dennis Müller replied on Wed, 2009/07/22 - 8:20am

I do not understand why somebody would want to do this with good reasons. Maybe I am not getting it, but perhaps you can point me to some situations where this is usefull.

Rael G.C. replied on Wed, 2009/07/22 - 2:51pm

I didn't understand the reason. Maximize a window is the same as resize it to fit the desktop dimension. So, just disable the resize.

GeekyCoder coder replied on Fri, 2009/07/24 - 10:37am

Well, this behaviour is based on a project I working on music system that display lyrics in jtable within resizable Window. It is manned to playback audio with lyrics in certain  font size (13pt) in a window, hence it does not make sense to display to maximize screen because the text will make the maximize screen look weird (Text on left side, and 2/3 width of the screen is blank screen) Still, whether you find it useful will be based on what your application requirement. As for my case, that is how I use this behaviour.

GeekyCoder coder replied on Fri, 2009/07/24 - 10:41am

Still, the argument may be the user still can resize the window and the text look weird. Well, the choice is up to them to resize intentionally, at least this behaviour prevent the user from accidentially clicking to maximize or double-clicking on title-bar to maximize, which something that happen quite frequently for my "unsteady" hand.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.