Hi,
I am trying to make a code that has multiple buttons, but the only way i know how to make action listeners are to either use inner classes (which require variable to be final, which isnt good for me), or to use AddActionListener(this), which only allows for one action listener.
Ive heard somehting about getSource(), but cant find any good examples of how its used... Are there any other ways to use multiple action listeners?
This is a code i was experimenting with getSource on, but i kept on getting an error:
import java.awt.*; import javax.swing.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class FindAddPage implements ActionListener { public static void main (String[] args) { int counter = 0; //set up frame GridLayout myLayout = new GridLayout (2,1); JFrame myFrame = new JFrame ("Add/Find"); myFrame.setSize(400,200); JPanel myPanel = new JPanel(); myPanel.setLayout(myLayout); //make buttons JButton add = new JButton ("Add A Tutor"); JButton find = new JButton ("Find A Tutor"); if(event.getSource()==add){ counter++; } myFrame.setVisible(true); } }