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?
 
ksivananthCommented:
because you have setVisible(true) in two places line# 63 and 11

0
 
CEHJCommented:
actionPerformed returns every time it's called. Your problem is that you only call getAnswer
0
 
allelopathAuthor Commented:
111
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.