• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Event Handler

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to setup an array of buttons and have a sound play when each button is selected. I have the button array displaying, but cannot get the sound to play when a button is selected (the synthesizer class is provided and I call the class passsing (note, instrument, duration). However, I never appear to got to the actionPerformed method of button (that I set up). Can anyone provide any help? The followining is my code:

package unit2;


// import Swing package to support Components
import javax.swing.*;

// import awt package for the Container class, in support of the Content Pane
import java.awt.*;

// import event package
import java.awt.event.*;

class Events extends JFrame implements ActionListener {

// declare the JButton component as an attribute
JButton[] buttons;
int i;



// create constructor with no arguments
public Events ( ) {
super ( );
init ();
}

// create constructor with string argument for the frame's title
public Events (String title) {
super (title);
init ();
}

// create the init method to initialize the application
public void init ( ) {
Container c = this.getContentPane ();
c.setBackground(Color.white);
c.setLayout (new GridLayout (8, 16, 5, 5));

buttons = new JButton [128];


for ( int i = 0; i < 128; i++) {
buttons[i] = new JButton ("" + i);

c.add (buttons[ i ]);
buttons[i].addActionListener(this) ;
}

this.setSize (1000, 500);
this.setVisible ( true );
this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );


}

public void actionPerformed (ActionEvent e) {
SynthPlayer syn = new SynthPlayer();
int count;
for(count = 0; count < 128; count++) {
if(e.getSource() == buttons[count])
syn.playNote(50, count, 1000);

// System.exit is necessary when working with the synth
System.exit(1);
}

}

// main method that creates a GridAssign
public static void main(String[] args) {
Events g = new Events ("Assignment Unit 2 - EVENTS");
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I got the program to work by removing the system exit from the synthesizer call. However, now it only works for about ten times and then when I pick the button, no sound is played. Anyone think of why the sound stops playing?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does not sound like a "beginner" question to me. Perhaps you should visit the GUI forum where folks around here discuss topics like yours. You may also want to visit the other APIs forum if you are using the java.midi package for the sound stuff. Someone there may be able to help with that part, in case it is causing the problem.

HTH

Layne
 
Mitch Krah
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I will try the GUI Forum. Thank you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic