Solved

Button click? - very simple

Posted on 1998-07-14
7
225 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
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!

 

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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
What's wrong with this code? 4 34
Java Restore security prompts not working 10 132
Setup GlassFish 4 35
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
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 …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

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