Best Way to Run a method on multiple buttons
i have a key pad of buttons and when i press any of these buttons i want it to make a sound i have done all the sound part in another class i am just wondering which is most efficient
Code:
private void keypad1ActionPerformed(java.awt.event.ActionEvent evt) {
/*Used to add a 1 to the end of the barcode string*/
barcode = totalTF.getText();
barcode = barcode + "1";
totalTF.setText(barcode);
Sound run = new Sound();
run.music();
}
putting that on every actionPerformed for each button
or having a actionListener that if any of the buttons are pressed it runs the method
if the second one is more efficient how would i go about doing that
Thank you
Re: Best Way to Run a method on multiple buttons
You would likely want to make the sound in a background thread so as not to lock up the main Swing thread, the event dispatch thread (EDT). You could probably use one listener for all number buttons and get the number String from the ActionEvent's actionCommand property (i.e., calling getActionCommand() on the ActionEvent object passed into the actionPerformed method).