?
Solved

Button click? - very simple

Posted on 1998-07-14
7
Medium Priority
?
234 Views
Last Modified: 2011-09-20
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
Comment
Question by:Hugi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 6

Expert Comment

by:gadio
ID: 1226198
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
 

Author Comment

by:Hugi
ID: 1226199
I don't want to stop the program - i want to leave the while-loop by click on the button
0
 
LVL 6

Expert Comment

by:gadio
ID: 1226200
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Hugi
ID: 1226201
Sorry, it doesn't works - the program doesn't enter in my event methode.
0
 
LVL 6

Expert Comment

by:gadio
ID: 1226202
Hugi, please explain waht you want to do so that we can find an alternative way of doing it.
0
 

Author Comment

by:Hugi
ID: 1226203
This is simple:
I want to start a dialogbox from a stand-alone Java-Application, where i can overwrite the action-method.
0
 

Accepted Solution

by:
ammorris earned 40 total points
ID: 1226204
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

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!

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 …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

764 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