Why? - Problem with JDialog

Hello to everyone!! I have these 2 classes:

import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.event.ActionEvent;

import java.awt.Dimension;

publicclass TestTheDialogimplements ActionListener{

JFrame mainFrame =null;

JButton myButton =null;

public TestTheDialog(){

mainFrame =new JFrame("TestTheDialog Tester");

mainFrame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

CustomDialog cust =new CustomDialog(mainFrame, true,"Do u wanna leave?");

mainFrame.setVisible(cust.getAnswer());}

});

myButton =new JButton("Test the dialog!");

myButton.addActionListener(this);

mainFrame.setLocationRelativeTo(null);

mainFrame.getContentPane().add(myButton);

mainFrame.pack();

mainFrame.setVisible(true);

}

publicvoid actionPerformed(ActionEvent e){

if(myButton == e.getSource()){

System.err.println("Opening dialog.");

CustomDialog myDialog =new CustomDialog(mainFrame, true,"Do u wanna leave?");

System.err.println("After opening dialog.");

if(myDialog.getAnswer()){

System.err.println("The answer is true.)");

}else{

System.err.println("The answer is false");

}

}

}

publicstaticvoid main(String argv[]){

TestTheDialog tester =new TestTheDialog();

}

}

This one:

import javax.swing.JDialog;

import java.awt.event.ActionListener;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

publicclass CustomDialogextends JDialogimplements ActionListener{

private JPanel myPanel =null;

private JButton yesButton =null;

private JButton noButton =null;

privateboolean answer =false;

publicboolean getAnswer(){return answer;}

public CustomDialog(JFrame frame,boolean modal, String myMessage){

super(frame, modal);

myPanel =new JPanel();

getContentPane().add(myPanel);

myPanel.add(new JLabel(myMessage));

yesButton =new JButton("Yes");

yesButton.addActionListener(this);

myPanel.add(yesButton);

noButton =new JButton("No");

noButton.addActionListener(this);

myPanel.add(noButton);

pack();

setLocationRelativeTo(frame);

setVisible(true);

}

publicvoid actionPerformed(ActionEvent e){

if(yesButton == e.getSource()){

System.err.println("User chose yes.");

answer =true;

setVisible(false);

System.exit(0);

}

elseif(noButton == e.getSource()){

System.err.println("User chose no.");

answer =false;

setVisible(false);

}

}

}

The real problem is in this part:

mainFrame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

CustomDialog cust =new CustomDialog(mainFrame, true,"Do u wanna leave?");

mainFrame.setVisible(cust.getAnswer());}

});

Why is it not working correctly?!I mean, if I click on "x" button, and I click on the "no " button, it's automatically close my frame too!

[7215 byte] By [MrSmyllea] at [2007-10-3 4:20:01]
# 1
Works fine for me on XP using JDK1.4.2You could try using a [url http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html]JOptionPane[/url] to see if it still has a problem.
camickra at 2007-7-14 22:21:56 > top of Java-index,Desktop,Core GUI APIs...
# 2
Have you tried to click on the "x" button of JFrame?It calls the jdialog, right?!If you click on the "no" button, it simply closes the jdialog and also the jframe!!But I just want to close the JDialog!!Not the Jframe either!!
MrSmyllea at 2007-7-14 22:21:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

public TestTheDialog() {

mainFrame = new JFrame("TestTheDialog Tester");

mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//<-need this line

mainFrame.addWindowListener(new WindowAdapter() {

and you need to reverse your boolean answer - where you have true, it should be false

and for false - the main culprit - should be true (this sets mainframe visibility to false)

probably make more sense to change the variable name from 'answer' to 'doNotQuit'

Michael_Dunna at 2007-7-14 22:21:56 > top of Java-index,Desktop,Core GUI APIs...
# 4
Sorry, misread the question. Here is a working version: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=695964Note the default close operation.
camickra at 2007-7-14 22:21:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks michael!!The way I wanted it!!Thanks!! And Camirck, I could use that!!But I'm brazilian!!I can't use buttons with the texts "Yes" or "no" in the buttons. That doesn't make any sense!!But thanks!!You've helped me a lot!!
MrSmyllea at 2007-7-14 22:21:56 > top of Java-index,Desktop,Core GUI APIs...