This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

handling button events

 
Andy Woods
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic