| | |
event code not working
Thread Solved |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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); }
}
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); }
}
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
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.

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.
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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
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
Last edited by stonemonolith; Sep 13th, 2006 at 1:13 pm. Reason: forgot a command
•
•
Join Date: Aug 2005
Posts: 216
Reputation:
Solved Threads: 8
Finally figured it out. It took a while...
bExitButton is being declaired in the class twice.
Changed
to
I'm suprised the compiler didn't have an issue with that. Oh well.
bExitButton is being declaired in the class twice.
Changed
Java Syntax (Toggle Plain Text)
JButton bExitButton = new JButton("Exit");
Java Syntax (Toggle Plain Text)
bExitButton = new JButton("Exit");
I'm suprised the compiler didn't have an issue with that. Oh well.
![]() |
Similar Threads
- KeyPress event with holding down the key (Python)
- Java Applet MouseDragged Event (Java)
- my html code isnt working please help (JavaScript / DHTML / AJAX)
- Code is written, but not working. (Java)
Other Threads in the Java Forum
- Previous Thread: undo - redo function in a text editor
- Next Thread: Dice Rolls:TroubleShooting
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows





