Java modal window with maximize button
Asked Answered
B

3

11

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?

Blucher answered 9/10, 2010 at 21:24 Comment(0)
A
12

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);
Anarthrous answered 9/10, 2010 at 21:36 Comment(5)
So there isn't any "legal" way to do this? :SBlucher
@János Harsányi, No, as I said, they're not supposed to have a maximize button.Anarthrous
As I said, I don't recommend to use that, in any circumstances.Anarthrous
What exactly are the dangers of using this?Algeria
Yeah, what dangers are there? I have a set of requirements that make me eye that code snippetEstelaestele
P
2

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();}
Pilloff answered 17/6, 2014 at 3:23 Comment(1)
Solution 2 is "super modal", no other window can be selected, no matter if is Java, native, whatever. Be careful!Pilloff
G
1

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);
    }
}
Ganglion answered 25/4, 2012 at 17:23 Comment(1)
@KarlRichter Thanks for letting me know, I've updated the link and added more details including the most relevant code.Ganglion

© 2022 - 2024 — McMap. All rights reserved.