简体   繁体   中英

is it possible to have multiple JOptionPane dialogs?

有谁知道我们如何在另一个JOptionPane对话框上面有一个JOptionPane对话框?

I would use JDialogs for this as I think that this gives you a bit more control over how code gets run and displayed. But it could be done with JOptionPanes as well. For instance if you displayed a JButton in the JOptionPane whose ActionListener caused the display of another JOptionPane.

For eg,

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class NestedJOptions {
   public static void main(String[] args) {
      final JPanel panel = new JPanel();
      panel.add(new JButton(new AbstractAction("Push Me") {
         public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(panel, "hello world!");
         }
      }));

      JOptionPane.showMessageDialog(null, panel);
   }
}

EDIT: @Hovercraft Full of Eels provide a better solution; more likely what the OP was looking for.

From JOptionPane javadocs :

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.

So, no, you won't be able to achieve the desired functionality with JOptionPane . But JOptionPane is a convenience class to create few commonly encountered JDialog s. Since the functionality you want is not directly supported by JOptionPane s, you should consider implementing it directly using JDialog s. For instance:

public class DialogTest {
    static final SimpleDateFormat SDF = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");

    public static void main(String[] args) {

        final JFrame frame = new JFrame("Dialog test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createPanelToPopDialog(frame));
        frame.setSize(500, 200);
        frame.show();
    }

    static JPanel createPanelToPopDialog(final JFrame parent) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JButton button = new JButton("Pop a Dialog");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JDialog dialog = new JDialog(parent, true);
                dialog.add(createPanelToPopDialog(parent));
                dialog.setSize(500, 200);
                dialog.show();
            }
        });
        panel.add(button, BorderLayout.SOUTH);
        panel.add(new JLabel("Created at " + SDF.format(new Date())));

        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        return panel;
    }
}

have you tried it and ran into some problem or you're just asking? JOptionPane is modal, so it blocks the thread it was created in. You can spawn several threads that show several JOptionPanes:

for (int i = 0; i < 5; i++) {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    JOptionPane.showMessageDialog(null, "I'm thread " + Thread.currentThread().getId());
                }
            });
            t.start();
        }

But only one message dialog will be able to receive swing events. You'd have to close them in the order they appeared (which is random).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM