pigmentarts
asked on
Adding a start and stop function/button
I have an applet which starts as soon as the screen is loaded.
I've added the start and stop buttons under the main class:
private Button bnStart, bnStop;
but how do I add actionEvents to them to stop and start the applet.
I'm already using one ActionEvent e
I've added the start and stop buttons under the main class:
private Button bnStart, bnStop;
but how do I add actionEvents to them to stop and start the applet.
I'm already using one ActionEvent e
that will call the doStart() method (which you'll need to add) when the start button is pressed.
Do the same (calling a different method) for the stop button.
Do the same (calling a different method) for the stop button.
The other alternative is to use your existing action listener and check the source of the event but using an inner class is neater.
public void actionPerformed(ActionEven t event)
{
Object source = event.getSource();
if (source==bnStart)
{
doStart();
}
if (source==bnStop)
{
doStop();
}
}
public void actionPerformed(ActionEven
{
Object source = event.getSource();
if (source==bnStart)
{
doStart();
}
if (source==bnStop)
{
doStop();
}
}
ASKER
How do I add the method?
In your case (Applet) you want to call the Applet start and stop methods.
> How do I add the method?
same way you add any method
private void doStart()
{
// add the code you want executed when start pressed here
}
private void doStop()
{
// add the code you want executed when stop pressed here
}
same way you add any method
private void doStart()
{
// add the code you want executed when start pressed here
}
private void doStop()
{
// add the code you want executed when stop pressed here
}
ASKER
you mean to replace my:
public void start() with private void doStart()
becasue thats where I put the inner class:
public void start(){
bnStart.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
doStart();
}
});
public void start() with private void doStart()
becasue thats where I put the inner class:
public void start(){
bnStart.addActionListener(
{
public void actionPerformed(ActionEven
{
doStart();
}
});
I think you should put:
bnStart.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEven t event)
{
doStart();
}
}
together with your jbinit (where you put all the rest of the UI code).
and you can change the call doStart(); to start();
bnStart.addActionListener(
{
public void actionPerformed(ActionEven
{
doStart();
}
}
together with your jbinit (where you put all the rest of the UI code).
and you can change the call doStart(); to start();
> you mean to replace my:
> public void start() with private void doStart()
No they are different things, start() is called by the browser when the applet starts.
And doStart() is called when the button is pressed.
Each method should then perform the required action.
> public void start() with private void doStart()
No they are different things, start() is called by the browser when the applet starts.
And doStart() is called when the button is pressed.
Each method should then perform the required action.
You need to seperate the part the builds your UI and the part the is responsible for the clips logic.
Make sure that your jbInit is called only once (I think you should call it from your init method and not from the start method).
The start method should be only responsible for your Thread and clip playing logic/
Make sure that your jbInit is called only once (I think you should call it from your init method and not from the start method).
The start method should be only responsible for your Thread and clip playing logic/
>> No they are different things, start() is called by the browser when the applet starts.
There is nothing wrong by calling it directly yourself.
There is nothing wrong by calling it directly yourself.
If you want to perform the same action then you can have doStart() call start() (or visa versa).
> There is nothing wrong by calling it directly yourself.
There is no need to either (nor is it stated both should do the same thing), and there will very likely be differences required in both. They are different callbacks and seperate methods provides a more flexible solution.
There is no need to either (nor is it stated both should do the same thing), and there will very likely be differences required in both. They are different callbacks and seperate methods provides a more flexible solution.
ASKER
haha not sure if its my laptop being slow or what, but all my buttons have now disappeard
In fact I'd suggest not even calling start() directly from anywhere, it is not intended to be called by the applet.
It is intended to be called by the browser to inform the applet to start.
btw, if you don't want to do anything when the applet starts then leave out the start() method altogether :)
It is intended to be called by the browser to inform the applet to start.
btw, if you don't want to do anything when the applet starts then leave out the start() method altogether :)
> but all my buttons have now disappeard
Did you remove any code? None of the additions I suggested should have affected what was already there and you shgouldn'ty have needed to remove anything.
Did you remove any code? None of the additions I suggested should have affected what was already there and you shgouldn'ty have needed to remove anything.
ASKER
yes they have :P
pigmentarts, make sure that your UI drawing is NOT part of the start method.
> yes they have :P
All i suggested was to add a listener and a new method, that would not affect the display of your existing buttons.
All i suggested was to add a listener and a new method, that would not affect the display of your existing buttons.
>> In fact I'd suggest not even calling start() directly from anywhere, it is not intended to be called by the applet.
You suggeset the oposite before "then you can have doStart() call start()"
>> In fact I'd suggest not even calling start() directly from anywhere, it is not intended to be called by the applet.
if he wants the same behaviour when the user press stop as when the user leaves the page and the browser calls stop then there is nothing wrong with that.
In his case he also creats the UI layout in the start method and this is his problem.
pigmentarts, as far as I remember your start method does three things
1. calls jBinit
2. load the clips
3. create the thread that plays the clips.
you want to call 1 and 2 only once and only call 3 per your start/stop button actions.
So you can either move 1 and 2 to the init method or move 3 to another method and call it from both start and from doStart.
You suggeset the oposite before "then you can have doStart() call start()"
>> In fact I'd suggest not even calling start() directly from anywhere, it is not intended to be called by the applet.
if he wants the same behaviour when the user press stop as when the user leaves the page and the browser calls stop then there is nothing wrong with that.
In his case he also creats the UI layout in the start method and this is his problem.
pigmentarts, as far as I remember your start method does three things
1. calls jBinit
2. load the clips
3. create the thread that plays the clips.
you want to call 1 and 2 only once and only call 3 per your start/stop button actions.
So you can either move 1 and 2 to the init method or move 3 to another method and call it from both start and from doStart.
you can have doStart() call start(), or start() call doStart(), or have them both call a 3rd method.
There is not enough details of the requirements posted to make that decision so can onlt post suggestions.
There is not enough details of the requirements posted to make that decision so can onlt post suggestions.
ASKER
ok i've lost myself, mainly becasue i've been doing 2 things at once :P
I've put the bnStart.addActionListener in the jbInit above where I made my buttons
But now I suddenly need to enter )}}; at the end of my code (is this ok or should they be written after the bnStart.addActionListener. ...
I've put the bnStart.addActionListener in the jbInit above where I made my buttons
But now I suddenly need to enter )}}; at the end of my code (is this ok or should they be written after the bnStart.addActionListener.
pigmentarts,
I don't want to confuse you, though both options are vaiable I think in your case the simplest
aproach, and probably cleaner, is to take out the Thread logic and put it outside the start method (e.g doStart as suggested by objects).
Just make sure your start function calls it.
I don't want to confuse you, though both options are vaiable I think in your case the simplest
aproach, and probably cleaner, is to take out the Thread logic and put it outside the start method (e.g doStart as suggested by objects).
Just make sure your start function calls it.
cAN you post the changes you made?
ASKER
I mean );}}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> But now I suddenly need to enter )}}; at the end of my code (is this ok or should they be written after the bnStart.addActionListener. ...
can you post how you added the action listener? should be exaclty as i posted above
can you post how you added the action listener? should be exaclty as i posted above
ASKER
Ah right, one sec having computer troubles :)
ASKER
This comes under the jbInit after i've named and called all my clips
bnStart.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEven t event)
{ start();
}});
Button button = new Button("ClickMe") { // add the button to the Applet
public Dimension getPreferredSize() {
return new Dimension(250, 100);
}
};
label = new Label("TestResults");
add(button, BorderLayout.CENTER); add(label, BorderLayout.CENTER);
button.addActionListener(n ew ActionListener() {
public void actionPerformed(ActionEven t e) {
button_actionPerformed(e);
pickedClips.add(clipNames[ index]);
if (index < 8) {
index = 8;
} else if (index < 15) {
index = 15;
} else if (index < 22) {
index = 22;
} else if (index < 29) {
index = 29;
} else if (index < 36)
{
label.setText(pickedClips. toString() );
System.out.println(pickedC lips);
}
}
});
}
public void start() {
thread = new Thread() {
public void run() {
Random random = new Random(System.currentTimeM illis());
while (index < array_of_clips.length) {
array_of_clips[index].play ();
int time = random.nextInt(1000) + 3000;
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
return;
}
array_of_clips[index].stop ();
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
return;
}
index++;
}
}
};
thread.start();
}
private Thread thread;
public void stop() {
if (thread != null) {
thread.interrupt();
}
}
public void button_actionPerformed(Act ionEvent e) {
}
}
bnStart.addActionListener(
{ public void actionPerformed(ActionEven
{ start();
}});
Button button = new Button("ClickMe") { // add the button to the Applet
public Dimension getPreferredSize() {
return new Dimension(250, 100);
}
};
label = new Label("TestResults");
add(button, BorderLayout.CENTER); add(label, BorderLayout.CENTER);
button.addActionListener(n
public void actionPerformed(ActionEven
button_actionPerformed(e);
pickedClips.add(clipNames[
if (index < 8) {
index = 8;
} else if (index < 15) {
index = 15;
} else if (index < 22) {
index = 22;
} else if (index < 29) {
index = 29;
} else if (index < 36)
{
label.setText(pickedClips.
System.out.println(pickedC
}
}
});
}
public void start() {
thread = new Thread() {
public void run() {
Random random = new Random(System.currentTimeM
while (index < array_of_clips.length) {
array_of_clips[index].play
int time = random.nextInt(1000) + 3000;
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
return;
}
array_of_clips[index].stop
try {
Thread.sleep(time);
} catch (InterruptedException ex) {
return;
}
index++;
}
}
};
thread.start();
}
private Thread thread;
public void stop() {
if (thread != null) {
thread.interrupt();
}
}
public void button_actionPerformed(Act
}
}
ASKER
its very strange my clips still play but all the buttons have gone
you don't appear to add your buttons to your applet, or is that done elsewhere?
i am referring to bnSTart
> add(button, BorderLayout.CENTER); add(label, BorderLayout.CENTER);
button is not appearing because you replace it with label.
Try instead:
add(button, BorderLayout.CENTER); add(label, BorderLayout.NORTH);
button is not appearing because you replace it with label.
Try instead:
add(button, BorderLayout.CENTER); add(label, BorderLayout.NORTH);
ASKER
I've called bnStart and bnStop under the main class
Should i create them by
Button bnStart = new button("*") {} ??
Should i create them by
Button bnStart = new button("*") {} ??
ASKER
ah i did wonder about that :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hmmm I still have no buttons showing at all...the applet still plays tunes but gives java.lang.NullPointerExcep tion errors.
ASKER
oH is that what I did aozarov :P
stackTrace?
ASKER
ok i havent actually created the start buttons with :
Button bnStart = new button("*");
I called them at the top:
Button bnStart, bnStop
I wanted to know the best way to do it.
Btw i'm in a big mess now, what should i do with:
bnStart.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEven t event)
{ start();
}});
Button bnStart = new button("*");
I called them at the top:
Button bnStart, bnStop
I wanted to know the best way to do it.
Btw i'm in a big mess now, what should i do with:
bnStart.addActionListener(
{ public void actionPerformed(ActionEven
{ start();
}});
ASKER
>>stackTrace? ?? :)
the error?
there are quite a few things that only show up when i try running the applet.
java.lang.NullPointerExcep tion
at jasounds.TestSounds.jbInit
at jasounds.TestSounds.init
at com.borland.jbuilder.runti me.applet. AppletTest bed.startA pplet
at com.borland.jbuilder.runti me.applet. AppletTest bed.main
the error?
there are quite a few things that only show up when i try running the applet.
java.lang.NullPointerExcep
at jasounds.TestSounds.jbInit
at jasounds.TestSounds.init
at com.borland.jbuilder.runti
at com.borland.jbuilder.runti
Based on the stack trace the problem seems to stem from your jbInit function.
Do you apply "Button bnStart = new button("*");"
before you call:
bnStart.addActionListener( new ActionListener()
{ public void actionPerformed(ActionEven t event)
{ start();
}});
Is the lines above also part of jbInit.
It would be good if you can paste the whole program (or at least jbInit, init and start methods)
Do you apply "Button bnStart = new button("*");"
before you call:
bnStart.addActionListener(
{ public void actionPerformed(ActionEven
{ start();
}});
Is the lines above also part of jbInit.
It would be good if you can paste the whole program (or at least jbInit, init and start methods)
bnStart.addActionListener(
{
public void actionPerformed(ActionEven
{
doStart();
}
});