aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "handling button events" Watch "handling button events" New topic
Author

handling button events

Andy Woods
Greenhorn

Joined: Nov 29, 2001
Posts: 5
Hey,
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");
soon.setHorizontalAlignment(JLabel.CENTER);

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


JPanel buttons = new JPanel();
buttons.add(displayButton);
buttons.add(closeButton);


dialogContentPane.add(soon);
dialogContentPane.add(buttons, BorderLayout.SOUTH);

helpDialog.setLocationRelativeTo(this);
helpDialog.show();

closeButton.setActionCommand("Close");

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

} // end of method
Any help will be GREATLY appreciated
Andy Woods
woods_a@acadmn.mercer.edu
Andy Woods
Greenhorn

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..

//{{REGISTER_LISTENERS
SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);
SymAction lSymAction = new SymAction();
Exit.addActionListener(lSymAction);
AboutUs.addActionListener(lSymAction);
HelpTopics.addActionListener(lSymAction);
CreateStartingLineups.addActionListener(lSymAction);
class SymAction implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == Exit)
Exit_actionPerformed(event);
else if (object == AboutUs)
AboutUs_actionPerformed(event);
else if (object == HelpTopics)
HelpTopics_actionPerformed(event);
else if (object == CreateStartingLineups)
CreateStartingLineups_actionPerformed(event);

}
}
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!
Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling button events