Java Tips: Disabling the Maximize Button of a JFrame
Join the DZone community and get the full member experience.Join For Free
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.
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
|file: maximizebuttondisabler.java (4kb)|
Opinions expressed by DZone contributors are their own.