Solved

Button click? - very simple

Posted on 1998-07-14
7
195 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 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…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now