?
Solved

Adding a start and stop function/button

Posted on 2005-04-26
42
Medium Priority
?
226 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:pigmentarts
  • 17
  • 15
  • 10
42 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13870744
use an inner class, eg:

bnStart.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
      doStart();
   }
});
0
 
LVL 92

Expert Comment

by:objects
ID: 13870755
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13870767
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(ActionEvent event)
{
   Object source = event.getSource();
   if (source==bnStart)
   {
      doStart();
   }
   if (source==bnStop)
   {
      doStop();
   }
}

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:pigmentarts
ID: 13871113
How do I add the method?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871127
In your case (Applet) you want to call the Applet start and stop methods.
0
 
LVL 92

Expert Comment

by:objects
ID: 13871210
> 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
}
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871328
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(ActionEvent event)
   {
      doStart();
   }
});





0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871340
I think you should put:
bnStart.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent 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();
0
 
LVL 92

Expert Comment

by:objects
ID: 13871350
> 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.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871351
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/
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871356
>> No they are different things, start() is called by the browser when the applet starts.
There is nothing wrong by calling it directly yourself.
0
 
LVL 92

Expert Comment

by:objects
ID: 13871363
If you want to perform the same action then you can have doStart() call start() (or visa versa).
0
 
LVL 92

Expert Comment

by:objects
ID: 13871374
> 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.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871376
haha not sure if its my laptop being slow or what, but all my buttons have now disappeard
0
 
LVL 92

Expert Comment

by:objects
ID: 13871382
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 :)
0
 
LVL 92

Expert Comment

by:objects
ID: 13871387
> 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.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871395
yes they have :P
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871396
pigmentarts, make sure that your UI drawing is NOT part of the start method.
0
 
LVL 92

Expert Comment

by:objects
ID: 13871417
> 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.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871425
>> 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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13871435
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.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871448
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....
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871453
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 13871455
cAN you post the changes you made?
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871463
I mean );}}
0
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 13871469
you need to break things up like this:


public void init()
{
   // setup your gui here
   // including adding the listeners as I posted earlier
}

public void start()
{
   // add anything here you want done everytime the applet loads
}

public void doStart()
{
   // add anbything here you want done when the start button is pressed
}
0
 
LVL 92

Expert Comment

by:objects
ID: 13871477
> 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
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871505
Ah right, one sec having computer troubles :)
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871522
This comes under the jbInit after i've named and called all my clips

bnStart.addActionListener(new ActionListener()
        { public void actionPerformed(ActionEvent 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(new ActionListener() {
            public void actionPerformed(ActionEvent 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(pickedClips);

                }
            }
        });

    }


    public void start() {


        thread = new Thread() {
            public void run() {
                Random random = new Random(System.currentTimeMillis());
                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(ActionEvent e) {

    }
}
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871537
its very strange my clips still play but all the buttons have gone
0
 
LVL 92

Expert Comment

by:objects
ID: 13871544
you don't appear to add your buttons to your applet, or is that done elsewhere?
0
 
LVL 92

Expert Comment

by:objects
ID: 13871547
i am referring to bnSTart
0
 
LVL 92

Expert Comment

by:objects
ID: 13871551
>  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);
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871554
I've called bnStart and bnStop under the main class

Should i create them by

Button bnStart = new button("*") {}  ??
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871557
ah i did wonder about that :)
0
 
LVL 15

Accepted Solution

by:
aozarov earned 1000 total points
ID: 13871625
>> Button bnStart = new button("*") {}  ??
Button bnStart = new button("*");
No need for the {}

So, I see that you actually managed to take the jbInit to the init method so you can re-use start/stop. I should give you more credit :-)
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871640
hmmm I still have no buttons showing at all...the applet still plays tunes but gives java.lang.NullPointerException errors.
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871643
oH is that what I did aozarov :P
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13871673
stackTrace?
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871688
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(ActionEvent event)
            { start();
            }});
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 13871856
>>stackTrace?   ??   :)

the error?

there are quite a few things that only show up when i try running the applet.



java.lang.NullPointerException

   at jasounds.TestSounds.jbInit
  at jasounds.TestSounds.init
at com.borland.jbuilder.runtime.applet.AppletTestbed.startApplet
at com.borland.jbuilder.runtime.applet.AppletTestbed.main
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13872803
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(ActionEvent 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)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question