How can I have multiple buttons and multiple listeners doing various operations in java swing. Here is an example of what I have, I can redirect to the AddStudent class but the button to redirect to the AddAdult class wont redirect to the AddAdult class.
private class ButtonHandler implements ActionListener {
// handle button event
public void actionPerformed( ActionEvent Student ) {
if ( Student.getSource() == button1 ){
try {
AddStudent newmember = new AddStudent();
newmember.setVisible( true );
} catch ( Exception e1 ) {
e1.printStackTrace();
}
}
}
public void actionPerformed( ActionEvent Adult ) {
if ( Adult.getSource() == button2 ){
try {
AddAdult newmember = new AddAdult();
newmember.setVisible( true );
} catch ( Exception e1 ) {
e1.printStackTrace();
}
}
}
Thanks for any help in advance.
You can attach an
ActionListenerto eachJButton, as explained in the Swing tutorial for buttonsSo you will end up with something like
For more specific questions about your program and your button you will need to provide us with more code then is currently available in your question (in other words, post an SSCCE)