How could I create a window which is modal and has a maximize button?
So is it possible to create a modal JFrame
or create a JDialog
with maximize button?
On most look and feels, modal windows (such as JDialog
) do not have a maximise button simply because they're not supposed to be maximised (or minimised) at all.
It's possible with some tricks to add a maximise
button, but it would be completly against the way JDialog
is supposed to work.
If you need a maximise button, the best solution would be using a JWindow
or a JFrame
instead of a JDialog
. Those windows support maximisation and minimisation.
WARNING: You shouldn't do that, no matter what.
A trick to do this in JDialog
:
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
Solution 1: Tested on Windows
I used a JFrame for the 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 a 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();}
Here is an alternate / slightly more detailed answer.
Try Are You Missing Maximize Button? (formerly here). This is a github archive of blog articles and code by Santhosh Kumar Tekturi from the now defunct JRoller site.
It is a complete utility class that makes a Frame mimic a Dialog, similar to the other answers. It involves adding a WindowListener
to the Frame to keep the frame on top of its owner and keep its owner frame disabled (warning: in the windowClosed
method it should probably be frame.removeWindowListener(this);
, and a WindowListener
to the owner to keep the frame on top of it and to remove the listener. It also uses its own EventQueue
to process events. Note this is an old post, so as mentioned in the code there may be newer APIs to deal with this code better.
Here is the core function. See the link for the rest. Note: the code in the repository differs from the article; I believe the repository is more developed.
// show the given frame as modal to the specified owner.
// NOTE: this method returns only after the modal frame is closed.
public static void showAsModal(final Frame frame, final Frame owner){
frame.addWindowListener(new WindowAdapter(){
public void windowOpened(WindowEvent e){
owner.setEnabled(false);
}
public void windowClosing(WindowEvent e) {
owner.setEnabled(true);
}
public void windowClosed(WindowEvent e){
frame.removeWindowListener(this); // originally called on owner
}
});
owner.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent e){
if(frame.isShowing()){
frame.setExtendedState(JFrame.NORMAL);
frame.toFront();
}else
owner.removeWindowListener(this);
}
});
owner.toFront();
frame.setVisible(true);
try{
new EventPump(frame).start();
} catch(Throwable throwable){
throw new RuntimeException(throwable);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With