Java modal window with maximize button

后端 未结 3 2004
灰色年华
灰色年华 2021-01-02 06:26

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 maxim

相关标签:
3条回答
  • 2021-01-02 06:32

    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);
    
    0 讨论(0)
  • 2021-01-02 06:34

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-02 06:36

    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();}
    
    0 讨论(0)
提交回复
热议问题