Trendy Java window with maximize button

How can I create a window that is modal and has a maximize button?
Is it possible to create a modal JFrame or create a JDialog button with a maximize button?

+7
java modal-dialog
source share
3 answers

In most cases, the look and modal windows (for example, JDialog ) do not have a maximize button, simply because they should not be maximized (or minimized) at all.

It’s possible to add a maximise button with some tricks, but it will be completely against how JDialog should work. If you need a maximize button, the best solution would be to use JWindow or JFrame instead of JDialog . These windows support maximization and minimization.


WARNING: You must not do this, no matter what.

The trick for this in JDialog :

 setUndecorated(true); getRootPane().setWindowDecorationStyle(JRootPane.FRAME); 
+10
source share

Solution 1: tested on Windows

I used JFrame for modal window

 JFrame mainWindow = new JFrame; mainWindow.setVisible(true); JFrame modalWindow = new JFrame(); // The next two sentences gives modalWindow modal beahaviour mainWindow.setEnabled(false); mainWindow.setFocusable(false); modalWindow.setVisible(true); 

Solution 2: tested on Ubuntu

I added WindowFocusListener

 addWindowFocusListener(new java.awt.event.WindowFocusListener() { public void windowGainedFocus(java.awt.event.WindowEvent evt) {} public void windowLostFocus(java.awt.event.WindowEvent evt) { formWindowLostFocus(evt);} private void formWindowLostFocus(java.awt.event.WindowEvent evt) { this.requestFocus(); this.toFront();} 
+1
source share

Here is an alternative answer.

Try it. You missed the "Maximize Button" button from Santhosh Kumar.

This is a utility class that creates a frame that mimics a dialog.

0
source share

All Articles