943,831 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2483
  • Java RSS
Sep 12th, 2006
0

event code not working

Expand Post »
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); }
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonemonolith is offline Offline
3 posts
since Sep 2006
Sep 13th, 2006
0

Re: event code not working

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.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 13th, 2006
0

Re: event code not working

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
Last edited by stonemonolith; Sep 13th, 2006 at 1:13 pm. Reason: forgot a command
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonemonolith is offline Offline
3 posts
since Sep 2006
Sep 14th, 2006
0

Re: event code not working

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

bExitButton is being declaired in the class twice.

Changed
Java Syntax (Toggle Plain Text)
  1. JButton bExitButton = new JButton("Exit");
to
Java Syntax (Toggle Plain Text)
  1. bExitButton = new JButton("Exit");

I'm suprised the compiler didn't have an issue with that. Oh well.
Reputation Points: 11
Solved Threads: 8
Posting Whiz in Training
hooknc is offline Offline
216 posts
since Aug 2005
Sep 14th, 2006
0

Re: event code not working

Thank you for your help - I really appreciate it! It's finally working.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonemonolith is offline Offline
3 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: undo - redo function in a text editor
Next Thread in Java Forum Timeline: Dice Rolls:TroubleShooting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC