Java – how to set JFrame as the parent of jdialog

I cannot set the frame as the owner of the dialog box Usually, when I extend the jdialog class to create a dialog box, I use the super (framework) to specify the owner of the dialog box so that when you press the alt tab, they will not be disconnected But when I create a dialog using the new jdialog dialog = new jdialog(), I cannot specify the frame as the owner of the dialog

The above examples show the above two methods Click the button at the top to open a dialog box without extending jdialog The bottom click button opens a dialog box that extends jdialog

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class DialogEx {

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                new DialogEx().createUI();
            }
        };
        EventQueue.invokelater(r);
    }   

    private void createUI() {
        final JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());

        JButton button1 = new JButton("Top Click");
        JButton button2 = new JButton("Bottom Click");

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                new DialogExtend(frame).createUI();
            }
        });

        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                new DialogWithoutExtend(frame).cretaUI();
            }
        });

        frame.setTitle("Test Dialog Instances.");
        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.soUTH);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(new Dimension(300,200));
        frame.setVisible(true);
    }

    class DialogExtend extends JDialog {
        private JFrame frame;
        public DialogExtend(JFrame frame) {
            super(frame);
            this.frame = frame;
        }

        public void createUI() {
            setLocationRelativeTo(frame);
            setTitle("Dialog created by extending JDialog class.");
            setSize(new Dimension(400,100));
            setModal(true);
            setVisible(true);
        }
    }

    class DialogWithoutExtend {

        private JFrame frame;
        public DialogWithoutExtend(JFrame frame) {
            this.frame = frame;
        }

        public void cretaUI() {
            JDialog dialog = new JDialog();
            dialog.setTitle("Dialog created without extending JDialog class.");
            dialog.setSize(new Dimension(400,100));
            dialog.setLocationRelativeTo(frame);
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    }
}

Solution

The owner of the dialog box (or window) can only be set in the constructor, so the only way to set it is to use the constructor with the owner as a parameter, such as:

class DialogWithoutExtend {

    private JFrame frame;
    public DialogWithoutExtend(JFrame frame) {
        this.frame = frame;
    }

    public void cretaUI() {
        JDialog dialog = new JDialog(frame);
        dialog.setTitle("Dialog created without extending JDialog class.");
        dialog.setSize(new Dimension(400,100));
        dialog.setLocationRelativeTo(frame);
        dialog.setModal(true);
        dialog.setVisible(true);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>