aspose file tools*
The moose likes Beginning Java and the fly likes Event Handler Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Event Handler" Watch "Event Handler" New topic
Author

Event Handler

Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
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

Joined: Sep 06, 2004
Posts: 41
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?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Mitch Krah
Ranch Hand

Joined: Sep 06, 2004
Posts: 41
OK. I will try the GUI Forum. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event Handler