Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Button click? - very simple

Why doesn't terminate this program when I click on the "Clear"-Button?

public class FirstClass extends Applet
{
      Button clear_button;
      boolean b;

      public void init()
      {
            b=true;
            Frame f=new Frame("Start");
            clear_button = new Button("Clear");
            f.add("North",clear_button);
            f.show();
            while(b);
      }

      public boolean action(Event  evt, Object arg)
      {
            if (evt.target == clear_button)
            {
                  b=false;
                  return true;
            }
            else return false;
      }
      

}

I call the applet from an application in stand-alone mod:

public static void main (String args[])
{
//      Frame f=new Frame("Start");
      FirstClass mac=new FirstClass();
      mac.init();
      mac.start();
//      f.add(mac);
//      f.show();

}


}
0
Hugi
Asked:
Hugi
  • 3
  • 3
1 Solution
 
gadioCommented:
Hugi, whenever you start a UI application in java there is another thread running for you (the AWT thread). You can't terminate this thread, so if you want to exit from the program use the System.exit( number ) command.
0
 
HugiAuthor Commented:
I don't want to stop the program - i want to leave the while-loop by click on the button
0
 
gadioCommented:
The while is a *very* bad way of doing a 'wait'. It takes up huge amout of cpu while doing nothing. There is a good chance that this code will not work at all as an applet since there you get only one thread and doing a busy wait (while) will get you UI 'stuck'. I suspect that the problem is the while. try this instead:
while(b) Thread.currentThread().sleep(5);
or:
while(b) Thread.currentThread().yield();

Tell me if it works. In any case, this code is not suitable to function in an applet.
G.
0
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.

 
HugiAuthor Commented:
Sorry, it doesn't works - the program doesn't enter in my event methode.
0
 
gadioCommented:
Hugi, please explain waht you want to do so that we can find an alternative way of doing it.
0
 
HugiAuthor Commented:
This is simple:
I want to start a dialogbox from a stand-alone Java-Application, where i can overwrite the action-method.
0
 
ammorrisCommented:
The reason that it is not ending is that you are not inside the while loop.  You are still inside the show statement of the frame object.  The event loop for the frame operates within that show statement.  And it will keep going until you kill the app with a System.exit() or close the frame.
0

Featured Post

Independent Software Vendors: 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!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now