Super26
asked on
Problem with Closing Application
I am having a slight problem with my frame. I have an Exit button in the frame and when the user clicks it, I ask them if they are sure they want to exit, if they click no nothing happens, else the program exits. I tried the same thing when the user clicks the 'X' on the frame window. However, regardless of what the user selects the program ends. I have this in my Frame's constructor. I was hoping someone could point out what I am doing wrong. Thanks.
// Close window event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
int result =
JOptionPane.showConfirmDia log(
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
this.setDefaultCloseOperat ion(JFrame .DO_NOTHIN G_ON_CLOSE );
// Close window event
this.addWindowListener(new
{
public void windowClosing(WindowEvent we)
{
int result =
JOptionPane.showConfirmDia
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION)
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
this.setDefaultCloseOperat
i tested your code .. it worked fine ...
:)
-=[sKySh@DoW]=-
:)
-=[sKySh@DoW]=-
ASKER
Hmm, it's weired I tested it in another frame too and it seems to be working fine. But in this particular frame it doesn't work. I have a few panels in the frame but I don't think that seems to be the problem. I have a few menu items too. I just don't know what is wrong with it.
why don't you paste the whole code for us to see ? it would be easier for us to see where ur error is
:)
-=[sKySh@DoW]=-
:)
-=[sKySh@DoW]=-
ASKER
Here's my Constructor:
public MainFrame()
{
// Close window event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
int result =
JOptionPane.showConfirmDia log(
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
setDefaultCloseOperation(J Frame.DO_N OTHING_ON_ CLOSE);
//Initialize frame
Toolkit kit = Toolkit.getDefaultToolkit( );
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
//Center frame in user's screen
setSize(440, 315);
setLocation(screenWidth / 4, screenHeight / 4);
//Set title
setTitle("Test1");
//Add mnemonics
menuFile.setMnemonic('F');
menuReport.setMnemonic('R' );
menuHelp.setMnemonic('H');
itemMainPage.setMnemonic(' M');
itemVideoRental.setMnemoni c('I');
itemCustomer.setMnemonic(' T');
itemSupplier.setMnemonic(' S');
itemVideo.setMnemonic('V') ;
itemExit.setMnemonic('X');
itemRetrieveOverdueRentals .setMnemon ic('N');
itemRetrieveVideos.setMnem onic('E');
itemSummaryReport.setMnemo nic('Y');
itemAbout.setMnemonic('B') ;
itemWeb.setMnemonic('W');
itemSubMenu.setMnemonic('U ');
itemRetrieveCustomers.setA ccelerator (
KeyStroke.getKeyStroke(Key Event.VK_1 , ActionEvent.ALT_MASK));
itemRetrieveSuppliers.setA ccelerator (
KeyStroke.getKeyStroke(Key Event.VK_2 , ActionEvent.ALT_MASK));
itemRetrieveVideos.setAcce lerator(
KeyStroke.getKeyStroke(Key Event.VK_3 , ActionEvent.ALT_MASK));
itemRetrieveVideoRentals.s etAccelera tor(
KeyStroke.getKeyStroke(Key Event.VK_4 , ActionEvent.ALT_MASK));
//Add menu items to submenu
itemSubMenu.add(itemRetrie veCustomer s);
itemSubMenu.add(itemRetrie veSupplier s);
itemSubMenu.add(itemRetrie veVideos);
itemSubMenu.add(itemRetrie veVideoRen tals);
//Add menu items to main menu
menuFile.add(itemMainPage) ;
menuFile.add(new JSeparator());
menuFile.add(itemVideoRent al);
menuFile.add(itemCustomer) ;
menuFile.add(itemSupplier) ;
menuFile.add(itemVideo);
menuFile.add(new JSeparator());
menuFile.add(itemExit);
menuReport.add(itemSubMenu );
menuReport.add(itemRetriev eOverdueRe ntals);
menuReport.add(itemSummary Report);
menuHelp.add(itemAbout);
menuHelp.add(itemWeb);
//Add menu to menu bar
menuBar.add(menuFile);
menuBar.add(menuReport);
menuBar.add(menuHelp);
//Add listeners
this.itemMainPage.addActio nListener( this);
this.itemVideoRental.addAc tionListen er(this);
this.itemCustomer.addActio nListener( this);
this.itemSupplier.addActio nListener( this);
this.itemVideo.addActionLi stener(thi s);
this.itemSubMenu.addAction Listener(t his);
this.itemExit.addActionLis tener(this );
this.itemRetrieveOverdueRe ntals.addA ctionListe ner(this);
this.itemSummaryReport.add ActionList ener(this) ;
this.itemRetrieveCustomers .addAction Listener(t his);
this.itemRetrieveSuppliers .addAction Listener(t his);
this.itemRetrieveVideos.ad dActionLis tener(this );
this.itemRetrieveVideoRent als.addAct ionListene r(this);
this.itemAbout.addActionLi stener(thi s);
this.itemWeb.addActionList ener(this) ;
//Add the menu bar to the top of the frame
this.setJMenuBar(menuBar);
//Add the main panel to the frame
Container contentPane = getContentPane();
contentPane.add(panel);
}
public MainFrame()
{
// Close window event
this.addWindowListener(new
{
public void windowClosing(WindowEvent event)
{
int result =
JOptionPane.showConfirmDia
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION)
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
setDefaultCloseOperation(J
//Initialize frame
Toolkit kit = Toolkit.getDefaultToolkit(
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
//Center frame in user's screen
setSize(440, 315);
setLocation(screenWidth / 4, screenHeight / 4);
//Set title
setTitle("Test1");
//Add mnemonics
menuFile.setMnemonic('F');
menuReport.setMnemonic('R'
menuHelp.setMnemonic('H');
itemMainPage.setMnemonic('
itemVideoRental.setMnemoni
itemCustomer.setMnemonic('
itemSupplier.setMnemonic('
itemVideo.setMnemonic('V')
itemExit.setMnemonic('X');
itemRetrieveOverdueRentals
itemRetrieveVideos.setMnem
itemSummaryReport.setMnemo
itemAbout.setMnemonic('B')
itemWeb.setMnemonic('W');
itemSubMenu.setMnemonic('U
itemRetrieveCustomers.setA
KeyStroke.getKeyStroke(Key
itemRetrieveSuppliers.setA
KeyStroke.getKeyStroke(Key
itemRetrieveVideos.setAcce
KeyStroke.getKeyStroke(Key
itemRetrieveVideoRentals.s
KeyStroke.getKeyStroke(Key
//Add menu items to submenu
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
//Add menu items to main menu
menuFile.add(itemMainPage)
menuFile.add(new JSeparator());
menuFile.add(itemVideoRent
menuFile.add(itemCustomer)
menuFile.add(itemSupplier)
menuFile.add(itemVideo);
menuFile.add(new JSeparator());
menuFile.add(itemExit);
menuReport.add(itemSubMenu
menuReport.add(itemRetriev
menuReport.add(itemSummary
menuHelp.add(itemAbout);
menuHelp.add(itemWeb);
//Add menu to menu bar
menuBar.add(menuFile);
menuBar.add(menuReport);
menuBar.add(menuHelp);
//Add listeners
this.itemMainPage.addActio
this.itemVideoRental.addAc
this.itemCustomer.addActio
this.itemSupplier.addActio
this.itemVideo.addActionLi
this.itemSubMenu.addAction
this.itemExit.addActionLis
this.itemRetrieveOverdueRe
this.itemSummaryReport.add
this.itemRetrieveCustomers
this.itemRetrieveSuppliers
this.itemRetrieveVideos.ad
this.itemRetrieveVideoRent
this.itemAbout.addActionLi
this.itemWeb.addActionList
//Add the menu bar to the top of the frame
this.setJMenuBar(menuBar);
//Add the main panel to the frame
Container contentPane = getContentPane();
contentPane.add(panel);
}
i think you're currently implementing the ActionListener class.... but maybe instead of implementing the action listener, why not try this pattern:
//Add listeners
this.itemMainPage.addActio nListener( new java.awt.event.ActionListe ner() {
public void actionPerformed(ActionEven t e) {
itemMainPage_actionPerform ed(e);
}
});
// now, you must have a method:
void itemMainPage_actionPerform ed(ActionE vent e) {
// your implementation here....
}
//Add listeners
this.itemMainPage.addActio
public void actionPerformed(ActionEven
itemMainPage_actionPerform
}
});
// now, you must have a method:
void itemMainPage_actionPerform
// your implementation here....
}
nothing looks wrong with this code either.. is this frame enclosed in something else??
try debugging your windowClosing method like for example who is the WindowEvent that invokes it (event.getSource() )
and what is the value of OptionPane before it exits..
vemul
try debugging your windowClosing method like for example who is the WindowEvent that invokes it (event.getSource() )
and what is the value of OptionPane before it exits..
vemul
there seems to be no error
:)
-=[sKySh@DoW]=-
:)
-=[sKySh@DoW]=-
ASKER
Yes, I am implementing action listener, but as you suggested, I commented out the implements part and ran it. It still did the same thing however. I just have no idea what could be wrong.
paste the whole program's code
maybe it will help
-=[sKySh@DoW]=-
maybe it will help
-=[sKySh@DoW]=-
ASKER
import java.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MainFrame extends JFrame implements ActionListener
{
//Main panel
private MainPanel panel = new MainPanel();
//Menu
public static JMenuBar menuBar = new JMenuBar();
public static JMenu menuFile = new JMenu("File");
public static JMenuItem itemMainPage = new JMenuItem("Main");
public static JMenuItem itemVideoRental = new JMenuItem("Video Rental");
public static JMenuItem itemCustomer = new JMenuItem("Customer");
public static JMenuItem itemSupplier = new JMenuItem("Supplier");
public static JMenuItem itemVideo = new JMenuItem("Video");
public static JMenuItem itemExit = new JMenuItem("Exit");
public static JMenu menuReport = new JMenu("Report");
public static JMenu itemSubMenu = new JMenu("Retrieve Lists");
public static JMenuItem itemRetrieveOverdueRentals =
new JMenuItem("Retrieve Overdue Rentals");
public static JMenuItem itemRetrieveCustomers =
new JMenuItem("Retrieve Customer List");
public static JMenuItem itemRetrieveSuppliers =
new JMenuItem("Retrieve Supplier List");
public static JMenuItem itemRetrieveVideos =
new JMenuItem("Retrieve Video List");
public static JMenuItem itemRetrieveVideoRentals =
new JMenuItem("Retrieve Video Rental List");
public static JMenuItem itemSummaryReport = new JMenuItem("Summary Report");
public static JMenu menuHelp = new JMenu("Help");
public static JMenuItem itemAbout = new JMenuItem("About");
public static JMenuItem itemWeb = new JMenuItem("On the Web");
/**
* MyPanel constructor comment.
*/
public MainFrame()
{
// Close window event
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
Object source = event.getSource();
int result =
JOptionPane.showConfirmDia log(
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION) ;
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
setDefaultCloseOperation(J Frame.DO_N OTHING_ON_ CLOSE);
//Initialize frame
Toolkit kit = Toolkit.getDefaultToolkit( );
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
//Center frame in user's screen
setSize(440, 315);
setLocation(screenWidth / 4, screenHeight / 4);
//Set title
setTitle("Super Inc. Video Rental");
//Add mnemonics
menuFile.setMnemonic('F');
menuReport.setMnemonic('R' );
menuHelp.setMnemonic('H');
itemMainPage.setMnemonic(' M');
itemVideoRental.setMnemoni c('I');
itemCustomer.setMnemonic(' T');
itemSupplier.setMnemonic(' S');
itemVideo.setMnemonic('V') ;
itemExit.setMnemonic('X');
itemRetrieveOverdueRentals .setMnemon ic('N');
itemRetrieveVideos.setMnem onic('E');
itemSummaryReport.setMnemo nic('Y');
itemAbout.setMnemonic('B') ;
itemWeb.setMnemonic('W');
itemSubMenu.setMnemonic('U ');
itemRetrieveCustomers.setA ccelerator (
KeyStroke.getKeyStroke(Key Event.VK_1 , ActionEvent.ALT_MASK));
itemRetrieveSuppliers.setA ccelerator (
KeyStroke.getKeyStroke(Key Event.VK_2 , ActionEvent.ALT_MASK));
itemRetrieveVideos.setAcce lerator(
KeyStroke.getKeyStroke(Key Event.VK_3 , ActionEvent.ALT_MASK));
itemRetrieveVideoRentals.s etAccelera tor(
KeyStroke.getKeyStroke(Key Event.VK_4 , ActionEvent.ALT_MASK));
//Add menu items to submenu
itemSubMenu.add(itemRetrie veCustomer s);
itemSubMenu.add(itemRetrie veSupplier s);
itemSubMenu.add(itemRetrie veVideos);
itemSubMenu.add(itemRetrie veVideoRen tals);
//Add menu items to main menu
menuFile.add(itemMainPage) ;
menuFile.add(new JSeparator());
menuFile.add(itemVideoRent al);
menuFile.add(itemCustomer) ;
menuFile.add(itemSupplier) ;
menuFile.add(itemVideo);
menuFile.add(new JSeparator());
menuFile.add(itemExit);
menuReport.add(itemSubMenu );
menuReport.add(itemRetriev eOverdueRe ntals);
menuReport.add(itemSummary Report);
menuHelp.add(itemAbout);
menuHelp.add(itemWeb);
//Add menu to menu bar
menuBar.add(menuFile);
menuBar.add(menuReport);
menuBar.add(menuHelp);
//Add listeners
this.itemMainPage.addActio nListener( this);
this.itemVideoRental.addAc tionListen er(this);
this.itemCustomer.addActio nListener( this);
this.itemSupplier.addActio nListener( this);
this.itemVideo.addActionLi stener(thi s);
this.itemSubMenu.addAction Listener(t his);
this.itemExit.addActionLis tener(this );
this.itemRetrieveOverdueRe ntals.addA ctionListe ner(this);
this.itemSummaryReport.add ActionList ener(this) ;
this.itemRetrieveCustomers .addAction Listener(t his);
this.itemRetrieveSuppliers .addAction Listener(t his);
this.itemRetrieveVideos.ad dActionLis tener(this );
this.itemRetrieveVideoRent als.addAct ionListene r(this);
this.itemAbout.addActionLi stener(thi s);
this.itemWeb.addActionList ener(this) ;
//Add the menu bar to the top of the frame
this.setJMenuBar(menuBar);
//Add the main panel to the frame
Container contentPane = getContentPane();
contentPane.add(panel);
}
/**
* Insert the method's description here.
* Creation date: (10/9/2002 12:46:52 PM)
* @param event java.awt.event.ActionEvent
*/
public void actionPerformed(ActionEven t event)
{
Object source = event.getSource();
//Main page menu was pressed
if (source == itemMainPage)
{
panel.displayMainPage();
}
//Video Rental menu was pressed
else if (source == itemVideoRental)
{
panel.displayVideoRentalPa ge("New Video Rental");
}
//Customer menu was pressed
else if (source == itemCustomer)
{
panel.displayCustomerPage( "New Customer");
}
//Supplier menu was pressed
else if (source == itemSupplier)
{
panel.displaySupplierPage( "New Supplier");
}
//Video menu was pressed
else if (source == itemVideo)
{
panel.displayVideoPage("Ne w Video");
}
//List All Customers menu was pressed
else if (source == itemRetrieveCustomers)
{
panel.getCustomerFileDirec tory();
}
//List All Suppliers menu was pressed
else if (source == itemRetrieveSuppliers)
{
panel.getSupplierFileDirec tory();
}
//List All Videos menu was pressed
else if (source == itemRetrieveVideos)
{
panel.getVideoFileDirector y();
}
//List All Video Rentals menu was pressed
else if (source == itemRetrieveVideoRentals)
{
panel.getVideoRentalFileDi rectory();
}
//OverDue Rentals menu was pressed
else if (source == itemRetrieveOverdueRentals )
{
panel.getOverDueRentalFile Directory( );
}
//Summary Report menu was pressed
else if (source == itemSummaryReport)
{
panel.getSummaryReportFile Directory( );
}
//Exit menu was pressed
else if (source == itemExit)
{
panel.exitProgram();
}
//About menu was pressed
else if (source == itemAbout)
{
panel.displayAboutPage();
}
//Web menu was pressed
else if (source == itemWeb)
{
panel.displayWebsite();
}
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MainFrame extends JFrame implements ActionListener
{
//Main panel
private MainPanel panel = new MainPanel();
//Menu
public static JMenuBar menuBar = new JMenuBar();
public static JMenu menuFile = new JMenu("File");
public static JMenuItem itemMainPage = new JMenuItem("Main");
public static JMenuItem itemVideoRental = new JMenuItem("Video Rental");
public static JMenuItem itemCustomer = new JMenuItem("Customer");
public static JMenuItem itemSupplier = new JMenuItem("Supplier");
public static JMenuItem itemVideo = new JMenuItem("Video");
public static JMenuItem itemExit = new JMenuItem("Exit");
public static JMenu menuReport = new JMenu("Report");
public static JMenu itemSubMenu = new JMenu("Retrieve Lists");
public static JMenuItem itemRetrieveOverdueRentals
new JMenuItem("Retrieve Overdue Rentals");
public static JMenuItem itemRetrieveCustomers =
new JMenuItem("Retrieve Customer List");
public static JMenuItem itemRetrieveSuppliers =
new JMenuItem("Retrieve Supplier List");
public static JMenuItem itemRetrieveVideos =
new JMenuItem("Retrieve Video List");
public static JMenuItem itemRetrieveVideoRentals =
new JMenuItem("Retrieve Video Rental List");
public static JMenuItem itemSummaryReport = new JMenuItem("Summary Report");
public static JMenu menuHelp = new JMenu("Help");
public static JMenuItem itemAbout = new JMenuItem("About");
public static JMenuItem itemWeb = new JMenuItem("On the Web");
/**
* MyPanel constructor comment.
*/
public MainFrame()
{
// Close window event
this.addWindowListener(new
{
public void windowClosing(WindowEvent event)
{
Object source = event.getSource();
int result =
JOptionPane.showConfirmDia
null,
"Are you sure you want to close the window?",
"Close",
JOptionPane.YES_NO_OPTION)
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
setDefaultCloseOperation(J
//Initialize frame
Toolkit kit = Toolkit.getDefaultToolkit(
Dimension screenSize = kit.getScreenSize();
int screenHeight = screenSize.height;
int screenWidth = screenSize.width;
//Center frame in user's screen
setSize(440, 315);
setLocation(screenWidth / 4, screenHeight / 4);
//Set title
setTitle("Super Inc. Video Rental");
//Add mnemonics
menuFile.setMnemonic('F');
menuReport.setMnemonic('R'
menuHelp.setMnemonic('H');
itemMainPage.setMnemonic('
itemVideoRental.setMnemoni
itemCustomer.setMnemonic('
itemSupplier.setMnemonic('
itemVideo.setMnemonic('V')
itemExit.setMnemonic('X');
itemRetrieveOverdueRentals
itemRetrieveVideos.setMnem
itemSummaryReport.setMnemo
itemAbout.setMnemonic('B')
itemWeb.setMnemonic('W');
itemSubMenu.setMnemonic('U
itemRetrieveCustomers.setA
KeyStroke.getKeyStroke(Key
itemRetrieveSuppliers.setA
KeyStroke.getKeyStroke(Key
itemRetrieveVideos.setAcce
KeyStroke.getKeyStroke(Key
itemRetrieveVideoRentals.s
KeyStroke.getKeyStroke(Key
//Add menu items to submenu
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
itemSubMenu.add(itemRetrie
//Add menu items to main menu
menuFile.add(itemMainPage)
menuFile.add(new JSeparator());
menuFile.add(itemVideoRent
menuFile.add(itemCustomer)
menuFile.add(itemSupplier)
menuFile.add(itemVideo);
menuFile.add(new JSeparator());
menuFile.add(itemExit);
menuReport.add(itemSubMenu
menuReport.add(itemRetriev
menuReport.add(itemSummary
menuHelp.add(itemAbout);
menuHelp.add(itemWeb);
//Add menu to menu bar
menuBar.add(menuFile);
menuBar.add(menuReport);
menuBar.add(menuHelp);
//Add listeners
this.itemMainPage.addActio
this.itemVideoRental.addAc
this.itemCustomer.addActio
this.itemSupplier.addActio
this.itemVideo.addActionLi
this.itemSubMenu.addAction
this.itemExit.addActionLis
this.itemRetrieveOverdueRe
this.itemSummaryReport.add
this.itemRetrieveCustomers
this.itemRetrieveSuppliers
this.itemRetrieveVideos.ad
this.itemRetrieveVideoRent
this.itemAbout.addActionLi
this.itemWeb.addActionList
//Add the menu bar to the top of the frame
this.setJMenuBar(menuBar);
//Add the main panel to the frame
Container contentPane = getContentPane();
contentPane.add(panel);
}
/**
* Insert the method's description here.
* Creation date: (10/9/2002 12:46:52 PM)
* @param event java.awt.event.ActionEvent
*/
public void actionPerformed(ActionEven
{
Object source = event.getSource();
//Main page menu was pressed
if (source == itemMainPage)
{
panel.displayMainPage();
}
//Video Rental menu was pressed
else if (source == itemVideoRental)
{
panel.displayVideoRentalPa
}
//Customer menu was pressed
else if (source == itemCustomer)
{
panel.displayCustomerPage(
}
//Supplier menu was pressed
else if (source == itemSupplier)
{
panel.displaySupplierPage(
}
//Video menu was pressed
else if (source == itemVideo)
{
panel.displayVideoPage("Ne
}
//List All Customers menu was pressed
else if (source == itemRetrieveCustomers)
{
panel.getCustomerFileDirec
}
//List All Suppliers menu was pressed
else if (source == itemRetrieveSuppliers)
{
panel.getSupplierFileDirec
}
//List All Videos menu was pressed
else if (source == itemRetrieveVideos)
{
panel.getVideoFileDirector
}
//List All Video Rentals menu was pressed
else if (source == itemRetrieveVideoRentals)
{
panel.getVideoRentalFileDi
}
//OverDue Rentals menu was pressed
else if (source == itemRetrieveOverdueRentals
{
panel.getOverDueRentalFile
}
//Summary Report menu was pressed
else if (source == itemSummaryReport)
{
panel.getSummaryReportFile
}
//Exit menu was pressed
else if (source == itemExit)
{
panel.exitProgram();
}
//About menu was pressed
else if (source == itemAbout)
{
panel.displayAboutPage();
}
//Web menu was pressed
else if (source == itemWeb)
{
panel.displayWebsite();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh man, I am so sorry for making you guys go through all this trouble. I just figured out what I have been doing wrong all this time. Seeing Ovi's modified program reminded me that MainFrame was not my actual runner application. My main string was sitting in another class "Program", that is where I was calling my MainFrame. Instead of putting my windowClosing event there, stupid me put it in the MainFrame class. My brain was totally missing yesterday, I apologise for putting you guys through all this trouble. Thanks so much for all your help.
I tested your code, it looks ok.... here is my test code
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowClose extends JFrame
{
public WindowClose()
{
// Close window event
this.addWindowListener(new
{
public void windowClosing(WindowEvent we)
{
int result =
JOptionPane.showConfirmDia
null,"Are you sure you want to close the window?",
"Close", JOptionPane.YES_NO_OPTION)
if (result == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
this.setDefaultCloseOperat
setBounds( 10,10,100,100 );
show();
}
public static void main ( String[] argvs )
{
WindowClose wc = new WindowClose();
}
}
hth,
Kok Choon.