1

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?

Johnydep
  • 6,027
  • 20
  • 57
  • 74
  • 2
    how do you set setModal or ModalityTypes, for better help sooner please post http://sscce.org/, – mKorbel Jan 23 '12 at 18:45

3 Answers3

4

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);
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

You can achieve this as follows:

enter image description here

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.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
  • @Eng. Fouad, thanks for the example. It was helpful but i am more interested in using custom design rather the JOptionPane as i need to take multiple values as input. – Johnydep Jan 23 '12 at 23:32
0

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

  • 1
    Please add more detail with your solution, this is very vague. How is this achieved? it would be a good idea to provide a code sample. Please read the SO guidelines before posting. – sparkitny Jan 27 '18 at 05:10
  • Agree. This sounds perfect but doesn't get the job done. – Higgs Oct 22 '19 at 23:30