Problem with JDialog

In the code below, the Save and Cancel buttons don't work the first time they are clicked, actionPeformed() is called, but it doesn't return. The 2nd time either is clicked, they do return. Why?
import javax.swing.JDialog; 

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.UIManager;


public class TestDialog extends JDialog implements ActionListener {

	private JPanel myPanel = null;
	private JButton saveButton = null;
	private JButton cancelButton = null;
	private boolean answer = false;

	/**
	 * constructor
	 * @param frame
	 * @param modal
	 * @param myMessage
	 */
	public TestDialog (JFrame frame, boolean modal, String myMessage) {

		super(frame, modal);
		
		this.setTitle(myMessage);

		myPanel = new JPanel(new BorderLayout());
		getContentPane().add(myPanel);
		myPanel.add(new JLabel(myMessage));

		JPanel textPanel = new JPanel();

		JLabel nameLabel = new JLabel("Name");
		JTextField name = new JTextField("Name");

		textPanel.add(nameLabel);
		textPanel.add(name);

		myPanel.add(textPanel, BorderLayout.CENTER);

		JPanel buttonPanel = new JPanel();

		saveButton = new JButton("Save");
		saveButton.addActionListener(this);
		buttonPanel.add(saveButton); 

		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(this);
		buttonPanel.add(cancelButton);  

		myPanel.add(buttonPanel, BorderLayout.PAGE_END);

		pack();
		setLocationRelativeTo(frame);

		setVisible(true);
	}

	/**
	 * 
	 * @param event
	 */
	public void actionPerformed(ActionEvent e) {

		if (saveButton == e.getSource()) {
			System.out.println("TestDialog.actionPerformed(): Save");
			answer = true;
			setVisible(false);
		}
		else if (cancelButton == e.getSource()) {
			System.out.println("TestDialog.actionPerformed(): Cancel");
			answer = false;
			setVisible(false);
		}
	}

	/**
	 * 
	 * @return
	 */
	public boolean getAnswer() { 
		return answer; 
	}

	/**
	 * 
	 * @param args
	 */
	public static void main(String args[]) {

		try{
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		}
		catch (Exception e){
			e.printStackTrace();
		}

		try {

			final JFrame frame = new JFrame();
			TestDialog dialog = new TestDialog(frame, true, "TestDialog");

			dialog.pack();
			dialog.setVisible(true);

			boolean answer = dialog.getAnswer();
			System.out.println ("TestDialog: " + answer);
			if (answer) {
				System.out.println("TestDialog: User clicked save button.");
				dialog.setVisible(false);
			}
			else {
				System.out.println("TestDialog: User clicked cancel button.");
				dialog.setVisible(false);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Open in new window

LVL 1
allelopathAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
actionPerformed returns every time it's called. Your problem is that you only call getAnswer
0
ksivananthCommented:
because you have setVisible(true) in two places line# 63 and 11

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
allelopathAuthor Commented:
111
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.