SetModal problem with 2 Jdialogs in Jframe

I have problems when I install my first JDialogmodal and the second non-modal.

This is the functionality I'm trying to implement:

  • When you click the "Check Dialog!" Button, a JDialogCustom Dialog Main will open.
  • If you click Yes in the Custom Dialog Main, another JDialognamed Custom Dialog Search will open.
  • If you click "Yes" in the user dialog search, the User Main dialog should appear in front.
  • And I have to choose any JDialog. For example, if I select Custom Search Dialog, another dialog should go backward and vice versa.

The problem I am facing is when I click “yes” in the user’s “Home” dialog box, then “Custom Search by Dialog” is displayed after the main dialog.

This is because I install Custom Dialog Search modeless. If I have this modal dialog, it displays correctly, but after I click Yes, the Custom Dialog Main will not appear in front.

I even tried to set the parent component of CustomDialogSearch as CustomDialog, the behavior is still not correct.

Below is an example of the code I'm testing.

import java.awt.event.ActionListener;  
import javax.swing.JFrame;  
import javax.swing.JButton;  
import java.awt.event.WindowAdapter;  
import java.awt.event.WindowEvent;  
import java.awt.event.ActionEvent;  
import java.awt.Dimension;   

public class TestTheDialog implements ActionListener {  
    JFrame mainFrame = null;  
    JButton myButton = null;  

    public TestTheDialog() {  
        mainFrame = new JFrame("TestTheDialog Tester");  
        mainFrame.addWindowListener(new WindowAdapter() {  
                public void windowClosing(WindowEvent e) {System.exit(0);}  
            });  
        myButton = new JButton("Test the dialog!");  
        myButton.addActionListener(this);  
        mainFrame.setLocationRelativeTo(null);  
        mainFrame.getContentPane().add(myButton);  
        mainFrame.pack();  
        mainFrame.setVisible(true);  
    }  

    public void actionPerformed(ActionEvent e) {  
        if(myButton == e.getSource()) {  
            System.err.println("Opening dialog.");  
            CustomDialog myDialog = new CustomDialog(mainFrame, true, "Custom Dialog Main?");  
            System.err.println("After opening dialog.");  
            if(myDialog.getAnswer()) {  
                System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");  
            }  
            else {  
                System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");  
            }  
        }  
    }  

    public static void main(String argv[]) {  

        TestTheDialog tester = new TestTheDialog();  
    }  
}  

import javax.swing.JDialog;   
import java.awt.event.ActionListener;  
import javax.swing.JPanel;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JButton;  
import java.awt.event.ActionEvent;  

public class CustomDialog extends JDialog implements ActionListener {  
    private JPanel myPanel = null;  
    private JButton yesButton = null;  
    private JButton noButton = null;  
    private boolean answer = false;  
    private JFrame parentFrame;  
    public boolean getAnswer() { return answer; }  

    public CustomDialog(JFrame frame, boolean modal, String myMessage) {  
        super(frame, modal);  
        parentFrame = frame;  
        myPanel = new JPanel();  
        getContentPane().add(myPanel);  
        myPanel.add(new JLabel(myMessage));  
        yesButton = new JButton("Yes");  
        yesButton.addActionListener(this);  
        myPanel.add(yesButton);   
        noButton = new JButton("No");  
        noButton.addActionListener(this);  
        myPanel.add(noButton);    
        pack();  
        setLocationRelativeTo(frame);  
        setVisible(true);  
    }  

    public void actionPerformed(ActionEvent e) {  
        if(yesButton == e.getSource()) {  
            CustomDialogSearch myDialog = new CustomDialogSearch(parentFrame, false, "CustomDialog Search?");  
            System.err.println("User chose yes.");  
            answer = true;  
            myDialog.getAnswer();  
            System.out.println("myDialog.getAnswer()="+myDialog.getAnswer());  
            myDialog.show();  

            if(myDialog.getAnswer()==true)  
            {  
                System.out.println("tofront");  
                this.toFront();  
            }  
            //setVisible(false);  
        }  
        else if(noButton == e.getSource()) {  
            System.err.println("User chose no.");  
            answer = false;  
            setVisible(false);  
        }  
    }  

}

import javax.swing.JDialog;   
import java.awt.event.ActionListener;  
import javax.swing.JPanel;  
import javax.swing.JFrame;  
import javax.swing.JLabel;  
import javax.swing.JButton;  
import java.awt.event.ActionEvent;  

public class CustomDialogSearch extends JDialog implements ActionListener {  
    private JPanel myPanel = null;  
    private JButton yesButton = null;  
    private JButton noButton = null;  
    private boolean answer = false;  
    public boolean getAnswer() { return answer; }  

    public CustomDialogSearch(JFrame frame, boolean modal, String myMessage) {  
        super(frame, modal);  
        myPanel = new JPanel();  
        getContentPane().add(myPanel);  
        myPanel.add(new JLabel(myMessage));  
        yesButton = new JButton("Yes");  
        yesButton.addActionListener(this);  
        myPanel.add(yesButton);   
        noButton = new JButton("No");  
        noButton.addActionListener(this);  
        myPanel.add(noButton);    
        pack();  
        setLocationRelativeTo(frame);  
        setVisible(true);  
    }  

    public void actionPerformed(ActionEvent e) {  
        if(yesButton == e.getSource()) {  
            System.err.println("Search User chose yes.");  
            answer = true;  
            //setVisible(false);  
        }  
        else if(noButton == e.getSource()) {  
            System.err.println("Search User chose no.");  
            answer = false;  
            setVisible(false);  
        }  
    }  

} 
+1
source share
1 answer

CustomDialogSearch CustomDialog. - .

, , . :

.

, :

import java.awt.Dialog;
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.SwingUtilities;

public class Demo {

    private void createAndShowGUI() {
        JButton button = new JButton("Create Parent modal dialog");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton)e.getSource();
                JFrame owner = (JFrame)SwingUtilities.windowForComponent(button);
                Demo.this.createAndShowParentDialog(owner);                
            }
        });

        JFrame frame = new JFrame("Demo");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(button);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private void createAndShowParentDialog(JFrame owner) {
        JButton button = new JButton("Create Child non-modal dialog");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton)e.getSource();
                JDialog parent = (JDialog)SwingUtilities.windowForComponent(button);
                Demo.this.createAndShowChildrenDialog(parent);                
            }
        });

        JDialog parentDialog = new JDialog(owner, "Parent dialog");
        parentDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        parentDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        parentDialog.getContentPane().add(button);
        parentDialog.pack();
        parentDialog.setLocationRelativeTo(null);        
        parentDialog.setVisible(true);
    }

    private void createAndShowChildrenDialog(JDialog parent) {        
        JButton backButton = new JButton("Back to parent dialog");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JButton button = (JButton)e.getSource();
                Window dialog = SwingUtilities.windowForComponent(button);
                dialog.getOwner().toFront();
            }
        });

        JDialog childDialog = new JDialog(parent, "Child dialog");
        childDialog.setModalityType(Dialog.ModalityType.MODELESS);
        childDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        childDialog.getContentPane().add(backButton);
        childDialog.pack();
        childDialog.setLocationRelativeTo(null);        
        childDialog.setVisible(true);
    }


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

JDialogs, JDialog, JDialog JDialog.

, , . , . , , , toFront(), , , . toBack(). . Javadocs.

Windows 7 (32 , - ), , ( ) . , , .

+3

Source: https://habr.com/ru/post/1530681/


All Articles