Hello. I'm new to Java and, I'm trying to set up a frame with an Exit Button that uses an event handler. But, it isn't working - here is the code, am I missing something? It isn't even getting inside the if statement. Any help would be appreciated

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.util.EventObject;

JButton bExitButton = new JButton("Exit");
panel.add(bExitButton);
bExitButton.addActionListener(this);

public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == bExitButton) {
JOptionPane.showConfirmDialog(null, "Are you sure you want to Exit?", "Alert", JOptionPane.YES_NO_OPTION);
System.exit(0); }
}

could you post more of your code please. there doens't even seem to be a complete class there. :)

Also, do a System.out.println(bExitButton) right after the bExitButton is created and then do a System.out.println(source) after getting the source from the event to make sure you're working with the same object.

Here is the complete text

package test;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.util.EventObject;
import javax.swing.JOptionPane;
public class AutoTest extends JFrame implements ActionListener
{
// instance variables
private javax.swing.JButton bExitButton;

public AutoTest() // This is the Constructor
{
setTitle("Test");

// center frame in screen
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
int width = 400;
int height = 400;
setBounds((d.width - width)/2, (d.height - height)/2, width, height);

// add a button to the frame
JPanel panel = new JPanel();
JButton bExitButton = new JButton("Exit");
System.out.println(bExitButton);

panel.add(bExitButton);
bExitButton.addActionListener(this);
Container contentPane = getContentPane();
panel.setLayout(new GridLayout(0, 1, 15, 1));
contentPane.add(panel);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showConfirmDialog(null,
"Are you sure you want to Exit?", "Alert",
JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
});
} // end of constructor

public void actionPerformed(java.awt.event.ActionEvent evt)
{
System.out.println("Inside actionPerformed routine");
Object source = evt.getSource();
System.out.println(source);
if (source == bExitButton)
{
System.out.println("Inside if statement");
JOptionPane.showConfirmDialog(null,
"Are you sure you want to Exit?", "Alert",
JOptionPane.OK_CANCEL_OPTION);
System.exit(0);
}
}

public static void main(String args[])
{
JFrame frame = new AutoTest();
frame.setVisible(true);
frame.setResizable(false);
}

} // end of class

Finally figured it out. It took a while...

bExitButton is being declaired in the class twice.

Changed

JButton bExitButton = new JButton("Exit");

to

bExitButton = new JButton("Exit");

I'm suprised the compiler didn't have an issue with that. Oh well.

Thank you for your help - I really appreciate it! It's finally working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.