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
Solved

Button click? - very simple

Posted on 1998-07-14
7
221 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
  • 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 20 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 42
JavaFX TableView not displaying correctly 3 58
eclipse console opening separately 2 29
jsp error 6 26
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

829 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