good day everyone. I have many java files in a project in netbeans. One file is named mainFile while some are addSale, addAttendance. In my mainFile.java, I created an actionPerformed method to check if a button is clicked. But the buttons that I want to checked if clicked is on the other java files.
I've added this code in my mainFile.java
AddSales addSaleButton;
Login logButton;
public void actionPerformed(ActionEvent ae){
if (ae.getSource() == addSaleButton.getButton()){
System.out.print("sample add");
}else if (ae.getSource() == logButton.getButton()){
System.out.print("sample log");
}
}
public void setButtonAction(Action action) {
(addSaleButton.getButton()).setAction(action);
}
then I added this in my addSales.java
public JButton getButton() {
return confirmAddSales;
}
From what i understand you want your button in one class and the ActionListener in another. I'm not 100% sure if this is what you want but here is some code for that:
Main class Btn:
Button class Button:
ActionListener class ActionListenerClass:
Frame class Frame:
and the output:
What you are doing here is simply putting the action listener in a separate class and then telling the button to look for the action listener in that class
using this line of code:
this.addActionListener(new ActionListenerClass());
Hope this helps, Luke.
EDIT:
try use
e.paramString():
this will print something like this:
or
e.getActionCommand():
this will print the button name:
Full actionListener code block:
output:
SECOND EDIT:
Ok use the
getActionCommand();
method and then use a switch stricture to check witch button was pressed.i have added another button, one called "LOL" and the other called "Hello world" both use the same action listener.
the output from pressing both:
now use a switch stricture to tell the difference between the buttons:
output with switch:
I hope this answers your question.
Yes this is possible and is often done, but the devil is in the details. Often you'll have a Control class that responds to user interaction that is completely separate from the View class, the GUI. Options include:
addButtonXActionListener(ActionListener l)
method.Edit
For example, here is a small program with 3 files, 1 for the View that holds the JButton, 1 for the Control, and a 3rd main class just to get things running.
Note that there are two JButtons and they both use 2 different ways of notifying outside classes that they've been pressed.
public void setButton1Action(Action action)
, that allows outside classes to set the Action of button1, The Control then does this, injecting an AbstractAction that notifies the Control of when button1 has been pressed.public void addPropertyChangeListener(PropertyChangeListener l)
wrapper method that allows outside classes to add in their PropertyChangeListener which then is added to the property change support of the mainPanel object. Then in button2's anonymous inner ActionListener class, the mainPanel's PropertyChangeSupport is asked to notify all listeners of a change in the state of the BUTTON2 property. View then adds a PropertyChangeListener and listens for changes the state of this property and responds.