Results 1 to 2 of 2
  1. #1
    Dcalladi is offline Member
    Join Date
    Oct 2011
    Posts
    27
    Rep Power
    0

    Default 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
    Java 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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

  1. Replies: 8
    Last Post: 08-10-2010, 02:59 AM
  2. Buttons and Method
    By imagiro1 in forum New To Java
    Replies: 2
    Last Post: 05-03-2010, 04:09 AM
  3. multiple submit buttons on webpage
    By samanthamaryhorgan in forum Advanced Java
    Replies: 1
    Last Post: 03-04-2010, 02:51 AM
  4. multiple submit buttons on webpage
    By samanthamaryhorgan in forum New To Java
    Replies: 0
    Last Post: 03-04-2010, 01:19 AM
  5. One controller with multiple buttons and colors
    By Cutter in forum New To Java
    Replies: 5
    Last Post: 01-24-2010, 06:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •