Java – how to create a modal jdialog over another modal jdialog

I have a mode setting dialog that is a jdialog In this setup window, I put some components (including a button) into another mode setup dialog box, which is also a jdialog I make them jdialogs because it's the only way I know to have modal conversations@ H_ 502_ 2 @ the problem is this: when I create the main settings dialog, I have to build jdialog without parent frame or parent frame Since my main window is a JFrame, I can pass it to the constructor of the main settings dialog However, when I want to create the second modal settings dialog, I should parent the main settings dialog. I can't find a way to get the (J) framework of jdialog I really want to use this main settings dialog as a parent so that the second settings dialog appears on it Let's assume that the second setup dialog has no constructor for passing location, just the constructor of jdialog

public class CustomDialog extends JDialog {
    public CustomDialog(String title) {
        setModal(true);
        setResizable(false);
        setTitle(title);

        buildGUI();
    }

    public Result showDialog(Window parent) {
        setLocationRelativeTo(parent);
        setVisible(true);
        return getResult();
    }
}

Solution

I'm not sure what your problem is, but here's an example. You can have multiple modal dialog boxes:

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestDialog {

    protected static void initUI() {
        JPanel pane = newPane("Label in frame");
        JFrame frame = new JFrame("Title");
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(pane);
        frame.pack();
        frame.setVisible(true);

    }

    public static JPanel newPane(String labelText) {
        JPanel pane = new JPanel(new BorderLayout());
        pane.add(newLabel(labelText));
        pane.add(newButton("Open dialog"),BorderLayout.soUTH);
        return pane;
    }

    private static JButton newButton(String label) {
        final JButton button = new JButton(label);
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Window parentWindow = SwingUtilities.windowForComponent(button);
                JDialog dialog = new JDialog(parentWindow);
                dialog.setLocationRelativeTo(button);
                dialog.setModal(true);
                dialog.add(newPane("Label in dialog"));
                dialog.pack();
                dialog.setVisible(true);
            }
        });
        return button;
    }

    private static JLabel newLabel(String label) {
        JLabel l = new JLabel(label);
        l.setFont(l.getFont().deriveFont(24.0f));
        return l;
    }

    public static void main(String[] args) {
        SwingUtilities.invokelater(new Runnable() {

            @Override
            public void run() {
                initUI();
            }
        });
    }
}
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
分享
二维码
< <上一篇
下一篇>>