Action listener to JDialog for clicked button?

If the dialog will disappear after the user presses confirm.

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I have main application where is table with values. Then, I click "Add" button, new CUSTOM (I made it myself) JDialog type popup comes up. There I can input value, make some ticks and click "Confirm".

So I need to read that input from dialog, so I can add this value to table in main application. How can I listen when "confirm" button is pressed, so I can read that value after that? AddISDialog = new AddISDialog(); addISDialog.

SetVisible(true); addISDialog. SetLocationRelativeTo(null); //somekind of listener... //after "Confirm" button in dialog was pressed, get value value = addISDialog. ISName; java swing jbutton actionlistener jdialog link|improve this question edited Dec 15 '11 at 17:44Hovercraft Full Of Eels40k41341 asked Dec 15 '11 at 16:36PooLaS355 71% accept rate.

If so, you could forward the ActionEvent to another class that implements the ActionListener interface and that class can do what you want. – mre Dec 15 '11 at 16:47 I made AddISDialog myself (public class AddISDialog extends JDialog implements ActionListener) so yes, I can edit it. What do you mean forwarding ActionEvent to another class?

How I can I do it? – PooLaS Dec 15 '11 at 16:51 1 One way to do it is to register a PropertyChangeListener to the JDialog instance and have the JDialog instance use a PropertyChangeSupport instance that will fire a property change event indicating that the confirm button was pushed. – mre Dec 15 '11 at 16:54 "add this value to table" You might pass the table model to the dialog in the constructor (or implement a setModel() method in the custom dialog).

BTW - For better help sooner, post an SSCCE. – Andrew Thompson Dec 15 '11 at 17:14.

If the dialog will disappear after the user presses confirm: and you wish to have the dialog behave as a modal JDialog, then it's easy, since you know where in the code your program will be as soon as the user is done dealing with the dialog -- it will be right after you call setVisible(true) on the dialog. So you simply query the dialog object for its state in the lines of code immediately after you call setVisible(true) on the dialog. If you need to deal with a non-modal dialog, then you'll need to add a WindowListener to the dialog to be notified when the dialog's window has become invisible.

If the dialog is to stay open after the user presses confirm: Then you should probably use a PropertyChangeListener as has been suggested above. Either that or give the dialog object a public method that allows outside classes the ability to add an ActionListener to the confirm button. For more detail, please show us relevant bits of your code, or even better, an sscce.

For example to allow the JDialog class to accept outside listeners, you could give it a JTextField and a JButton: class MyDialog extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); and a method that allows outside classes to add an ActionListener to the button: public void addConfirmListener(ActionListener listener) { confirmBtn. AddActionListener(listener); } Then an outside class can simply call the `addConfirmListener(...) method to add its ActionListener to the 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); } } }); //! Add a listener to the dialog's button myDialog. AddConfirmListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = myDialog.

GetTextFieldText(); textField. SetText(text); } }); 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 OutsideListener("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 MyDialog extends JDialog { private JTextField textfield = new JTextField(10); private JButton confirmBtn = new JButton("Confirm"); public MyDialog(JFrame frame, String title) { super(frame, title, false); JPanel panel = new JPanel(); panel.

Add(textfield); panel. Add(confirmBtn); add(panel); pack(); setLocationRelativeTo(frame); } public String getTextFieldText() { return textfield.getText(); } public void addConfirmListener(ActionListener listener) { confirmBtn. AddActionListener(listener); } } Caveats though: I don't recommend subclassing JFrame or JDialog unless absolutely necessary.

It was done here simply for the sake of brevity. I also myself prefer to use a modal dialog for solving this problem and just re-opening the dialog when needed. Edit 2 An example of use of a 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); //! Made into a modal dialog JPanel panel = new JPanel(); panel. Add(new JLabel("Please enter a number between 1 and 100:")); panel.

Add(textfield); panel. Add(confirmBtn); add(panel); pack(); setLocationRelativeTo(frame); ActionListener confirmListener = new ConfirmListener(); confirmBtn. AddActionListener(confirmListener); // add listener textfield.

AddActionListener(confirmListener ); } public String getTextFieldText() { return textfield.getText(); } private class ConfirmListener implements ActionListener { public void actionPerformed(ActionEvent e) { String text = textfield.getText(); if (isTextValid(text)) { MyDialog2.this. SetVisible(false); } else { // show warning String warning = "Data entered, \"" + text + "\", is invalid. Please enter a number between 1 and 100"; JOptionPane.

ShowMessageDialog(confirmBtn, warning, "Invalid Input", JOptionPane. ERROR_MESSAGE); textfield. SetText(""); textfield.

RequestFocusInWindow(); } } } // true if data is a number between 1 and 100 public boolean isTextValid(String text) { try { int number = Integer. ParseInt(text); if (number > 0 && number.

I think I got your idea, but now there is another connected problem: Before sending action to listener, I need to confirm data in dialog when pressed Confirm button. Just when data is valid (when valid - dialog closes, else - doesn't), value has to be read for further actions. I can fire some kind of notification to listener from dialog class, but I don't know how – PooLaS Dec 15 '11 at 18:42 Then don't go by the code above.

What you need to do is to make the dialog modal, and have its confirm button's action validate the data and only close the dialog if the data is OK. Then have your main program query the dialog class for the data in the code immediately after calling setVisible(true) on the dialog. – Hovercraft Full Of Eels Dec 15 '11 at 20:51 @PooLaS: see edit for an example of a modal dialog solution above.

– Hovercraft Full Of Eels Dec 15 '11 at 22:29 I made few public methods in dialog class, so I verify data twice - in dialog for closing and in main for entering new data. Thanks guys – PooLaS Dec 15 '11 at 16:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions