show two dialogs on top of each other using java swing

落花浮王杯 提交于 2019-12-31 04:50:10

问题


i have a situation where i show a dialog where user has to fill some menus and then press OK. It works fine, but now i have another button on this dialog that if user wants to add some certain value, i want another dialog to popup where user fills the additional value and while pressing ok, this dialog disappears and user comes back to the main dialog.

I have tried this, but every time i call the new dialog, the focus does not go away from the main dialog, how can i do such a task.

Is there any relevant example or what is the proper way of doing such things.

EDIT:

public static class EdgeMenu extends JPopupMenu {
        // private JFrame frame; 
        public MyMenu(final JFrame frame) {
            super("My Menu");
            // this.frame = frame;

            this.addSeparator();
            this.add(new EdgePropItem(frame));           
        }  
    }  



 //this shows the first dialog, another class because i have some other  
 //functions to be performed here  

    public static class EdgePropItem extends JMenuItem{
            //...       

            public  EdgePropItem(final JFrame frame) {  
                super("Edit Properties");
                this.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        EdgePropertyDialog dialog = new EdgePropertyDialog(frame, edge);
                        dialog.setVisible(true);
                    }

                });
            }

        }  

and now in other dialog, in the button even listener i am trying to call another dialog:

 private void newDialogHandler(java.awt.event.ActionEvent evt) {
        MyNewDialog rdialog = new MyNewDialog(edge);
        rdialog.setVisible(true);
    }  

It appears fine, but the previous dialog, does not leave the focus, and it goes away only if i press finish/done on that dialog, what i want is the new dialog to come in focus, while pressing ok on here, focus should come back to the old main dialog, but it is not working?


回答1:


maybe this code could be demonstate your issues,

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SuperConstructor extends JFrame {

    private static final long serialVersionUID = 1L;

    public SuperConstructor() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(300, 300));
        setTitle("Super constructor");
        Container cp = getContentPane();
        JButton b = new JButton("Show dialog");
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
            }
        });
        cp.add(b, BorderLayout.SOUTH);
        JButton bClose = new JButton("Close");
        bClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                System.exit(0);
            }
        });
        add(bClose, BorderLayout.NORTH);
        pack();
        setVisible(true);
    }

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                SuperConstructor superConstructor = new SuperConstructor();
            }
        });
    }

    private class FirstDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        FirstDialog(final Frame parent) {
            super(parent, "FirstDialog");
            setPreferredSize(new Dimension(200, 200));
            setLocationRelativeTo(parent);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
            JButton bNext = new JButton("Show next dialog");
            bNext.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    SecondDialog secondDialog = new SecondDialog(parent, false);
                }
            });
            add(bNext, BorderLayout.NORTH);
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
    private int i;

    private class SecondDialog extends JDialog {

        private static final long serialVersionUID = 1L;

        SecondDialog(final Frame parent, boolean modal) {
            //super(parent); // < --- Makes this dialog 
            //unfocusable as long as FirstDialog is visible
            setPreferredSize(new Dimension(200, 200));
            setLocation(300, 50);
            setModal(modal);
            setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            setTitle("SecondDialog " + (i++));
            JButton bClose = new JButton("Close");
            bClose.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent evt) {
                    setVisible(false);
                }
            });
            add(bClose, BorderLayout.SOUTH);
            pack();
            setVisible(true);
        }
    }
}



回答2:


You can achieve this as follows:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;

public class MultipleDialogs
{   
    public MultipleDialogs()
    {
        JButton btnOpen = new JButton("Open another dialog!");
        btnOpen.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                JOptionPane.showMessageDialog(null, "This is the second dialog!");
            }
        });
        Object[] options = {"OK", "Cancel", btnOpen};
        int selectedOption = JOptionPane.showOptionDialog(null,
                "This is the first dialog!", "The title",
                JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
                null, options, options[0]);

        if(selectedOption == 0) // Clicking "OK"
        {

        }
        else if(selectedOption == 1) // Clicking "Cancel"
        {

        }
    }

   public static void main(String[] args)
   {
      SwingUtilities.invokeLater(new Runnable()
      {
          @Override
          public void run()
          {
              new MultipleDialogs();    
          });
       }
    }
}

You can replace "This is the first dialog!" and "This is the second dialog!" with JPanel's which can contain any swing components you want.




回答3:


Create the second dialog from a constructor that takes dialog and boolean as parameters and that solves the problem.



来源:https://stackoverflow.com/questions/8976874/show-two-dialogs-on-top-of-each-other-using-java-swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!