If the dialog disappears after the user confirms the confirmation:
- and you want the dialog to work as a modal JDialog, then it's easy, since you know where your program will be in the code as soon as the user works with the dialog - it will be right after calling
setVisible(true)
in the dialog box. Therefore, you simply request a dialog object for your state in the lines of code immediately after calling setVisible(true)
in the dialog box. - If you need to deal with a modeless dialog, you will need to add a WindowListener in the dialog box, which will be notified when the dialog box becomes invisible.
If the dialog remains open after confirmation by the user:
- Then you should probably use a PropertyChangeListener, as suggested above. Either this, or give the dialog object an open method that allows external classes to add an ActionListener to the confirmation button.
For more details, please show us the corresponding bits of your code or even better sscce .
For example, to allow the JDialog class to accept external listeners, you can give it JTextField and JButton:
class MyDialog extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm");
and a method that allows external classes to add an ActionListener to a button:
public void addConfirmListener(ActionListener listener) { confirmBtn.addActionListener(listener); }
Then the outer class can simply call the addConfirmListener (...) method to add its ActionListener to confirmBtn.
For example:
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class OutsideListener extends JFrame { private JTextField textField = new JTextField(10); private JButton showDialogBtn = new JButton("Show Dialog"); private MyDialog myDialog = new MyDialog(this, "My Dialog"); public OutsideListener(String title) { super(title); textField.setEditable(false); showDialogBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!myDialog.isVisible()) { myDialog.setVisible(true); } } });
Cautions: I do not recommend subclassing JFrame or JDialog if absolutely necessary. It was done here just for the sake of brevity. I also prefer to use a modal dialog to solve this problem and just reopen the dialog when necessary.
Edit 2
An example of using the Modal dialog:
import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class OutsideListener2 extends JFrame { private JTextField textField = new JTextField(10); private JButton showDialogBtn = new JButton("Show Dialog"); private MyDialog2 myDialog = new MyDialog2(this, "My Dialog"); public OutsideListener2(String title) { super(title); textField.setEditable(false); showDialogBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (!myDialog.isVisible()) { myDialog.setVisible(true); textField.setText(myDialog.getTextFieldText()); } } }); JPanel panel = new JPanel(); panel.add(textField); panel.add(showDialogBtn); add(panel); } @Override public Dimension getPreferredSize() { return new Dimension(400, 300); } private static void createAndShowGui() { JFrame frame = new OutsideListener2("OutsideListener"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } } class MyDialog2 extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); public MyDialog2(JFrame frame, String title) { super(frame, title, true);
Hovercraft full of eels
source share