event code not working

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2006
Posts: 3
Reputation: stonemonolith is an unknown quantity at this point 
Solved Threads: 0
stonemonolith stonemonolith is offline Offline
Newbie Poster

event code not working

 
0
  #1
Sep 12th, 2006
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); }
}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: event code not working

 
0
  #2
Sep 13th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: stonemonolith is an unknown quantity at this point 
Solved Threads: 0
stonemonolith stonemonolith is offline Offline
Newbie Poster

Re: event code not working

 
0
  #3
Sep 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 216
Reputation: hooknc is an unknown quantity at this point 
Solved Threads: 8
hooknc hooknc is offline Offline
Posting Whiz in Training

Re: event code not working

 
0
  #4
Sep 14th, 2006
Finally figured it out. It took a while...

bExitButton is being declaired in the class twice.

Changed
  1. JButton bExitButton = new JButton("Exit");
to
  1. bExitButton = new JButton("Exit");

I'm suprised the compiler didn't have an issue with that. Oh well.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: stonemonolith is an unknown quantity at this point 
Solved Threads: 0
stonemonolith stonemonolith is offline Offline
Newbie Poster

Re: event code not working

 
0
  #5
Sep 14th, 2006
Thank you for your help - I really appreciate it! It's finally working.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC