Results 1 to 2 of 2
- 11-07-2011, 03:40 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 27
- Rep Power
- 0
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
putting that on every actionPerformed for each buttonJava 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(); }
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).
Similar Threads
-
For some reason, my GUI is not recognizing multiple action listeners for buttons.
By JavaStudent1990 in forum New To JavaReplies: 8Last Post: 08-10-2010, 02:59 AM -
Buttons and Method
By imagiro1 in forum New To JavaReplies: 2Last Post: 05-03-2010, 04:09 AM -
multiple submit buttons on webpage
By samanthamaryhorgan in forum Advanced JavaReplies: 1Last Post: 03-04-2010, 02:51 AM -
multiple submit buttons on webpage
By samanthamaryhorgan in forum New To JavaReplies: 0Last Post: 03-04-2010, 01:19 AM -
One controller with multiple buttons and colors
By Cutter in forum New To JavaReplies: 5Last Post: 01-24-2010, 06:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks