JDialog action listener for the pressed button

I have a main application where there is a table with values. Then I click the "Add" button, a new CUSTOM (I did it myself) a pop-up window like JDialog appears. There I can enter a value, make a few ticks and click "Confirm." Therefore, I need to read this input from the dialog box, so I can add this value to the table in the main application. How can I listen when the β€œconfirm” button is pressed so that after that I can read this value?

addISDialog = new AddISDialog(); addISDialog.setVisible(true); addISDialog.setLocationRelativeTo(null); //somekind of listener... //after "Confirm" button in dialog was pressed, get value value = addISDialog.ISName; 
+7
source share
4 answers

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); } } }); // !! add a listener to the dialog 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); } } 

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); // !!!!! 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 <= 100) { return true; } } catch (NumberFormatException e) { // one of the few times it OK to ignore an exception } return false; } } 
+13
source
 //Why is this working so well even without the ActionListener interface, and all I'm //getting is minor format errors at the end brackets? Know how to fix it? final JButton newButton = new JButton("Send"); newButton.setActionCommand("Send"); textPane.add(newButton); newButton.setEnabled(true); newButton.addActionListener(new ActionListener(); public void actionPerformed(ActionEvent e){ // display/center the jdialog when the button is pressed JDialog digFree = new JDialog(digFree, "Hello", true); digFree.setLocationRelativeTo(newButton); digFree.setFocusable(true); digFree.setVisible(true); digFree.setBounds(20, 20, 100, 120); } 
0
source

Why don't you check if your jDialog is showing up?

 yourJD.setVisible(true); while(yourJD.isVisible())try{Thread.sleep(50);}catch(InterruptedException e){} 

it works too.

0
source
 import javax.swing.JOptionPane; 

or if you already download

 import javax.swing.*; 

you will be closed.

After conditionally starting JOptionPane to send your alert or any other modal message:

  JOptionPane.showMessageDialog( null, "Your warning String: I can't do that John", "Window Title", JOptionPane.ERROR_MESSAGE); 

check your options for JOptionPane. * to determine the type of message.

-one
source

All Articles