File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes handling button events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "handling button events" Watch "handling button events" New topic

handling button events

Andy Woods

Joined: Nov 29, 2001
Posts: 5
I know i might be beating a dead horse, but I cannont make some button events work, even looking through Sun's Swing book.
Ok, I click on a meu item in my swing application. That brings up a JDialog box with 2 buttons on the bottom, Display and Close. When i clik close, i would like that JDialog box to be closed. Im just having problems handling button events.. here's my method

void HelpTopics_actionPerformed(java.awt.event.ActionEvent event)

JPanel buttonpanel = new JPanel();
JButton displayButton = new JButton("Display");
JButton closeButton = new JButton("Close");
displayButton.setToolTipText("Display highlighted topic");
closeButton.setToolTipText("Close Window");

final JDialog helpDialog = new JDialog(this, "Help Topics...", true);
helpDialog.setSize(400, 500);
JLabel soon = new JLabel("Coming Soon");

Container dialogContentPane = helpDialog.getContentPane();
Container topsection = getContentPane();

JPanel buttons = new JPanel();

dialogContentPane.add(buttons, BorderLayout.SOUTH);



closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
helpDialog.dispose(); } } );

} // end of method
Any help will be GREATLY appreciated
Andy Woods
Andy Woods

Joined: Nov 29, 2001
Posts: 5
Also, if I declare implements ActionListener at the top of my class, i get an error saying that my main class should be declared abstract, does not define action performed. Yet, i have other methods that handle my stuff... ill just copy most of the file..

SymWindow aSymWindow = new SymWindow();
SymAction lSymAction = new SymAction();
class SymAction implements java.awt.event.ActionListener
public void actionPerformed(java.awt.event.ActionEvent event)
Object object = event.getSource();
if (object == Exit)
else if (object == AboutUs)
else if (object == HelpTopics)
else if (object == CreateStartingLineups)

void CreateStartingLineups_actionPerformed(java.awt.event.ActionEvent event)
CreateLineups lineup = new CreateLineups();
this one above works...

Please HELP!!!
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Andy,
I don't know about your second problem, but your first is that you should setup everything to do with the dialog before you call the show method. The call to the show method should be the last thing in your actionPerformed method.
Why? Because show goes ahead and manages all children (i.e., placing them, registering events, etc). Since you are setting events up afterwards the dialog doesn't know about these changes until you tell it to either show itself again or you call the validate method. This is why the cleanest approach would be to simply call the show method last!
If you move your show method call last, the code works great!
I agree. Here's the link:
subject: handling button events
jQuery in Action, 3rd edition