Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Pause of the execution of a method until a button is pressed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i stop the exceuction of a method(specifically the main method) until a GUI button is pressed?
Is there a way with the wait() and notify methods?
Thank you!
 
Saloon Keeper
Posts: 11189
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can do it like that, but typically there's a better approach. Why not just move the part of the main method to the button handler?
 
Saloon Keeper
Posts: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GUI apps are typically event-driven, not flow driven. As Stephen has said, instead of pausing code, it's preferable to attach an event handler to the button. The event handler will then be called when the button is pressed.

You could pause a thread until an event handler notified it to resume, but that's generally not really what you'd be after anyway.
 
Rancher
Posts: 3111
26
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How do i stop the exceuction of a method(specifically the main method)


Well, you really shouldn't be executing any code in the main() method when you create a GUI.

All Swing components should be created on the Event Dispatch Thread (EDT).

So the code in the main() method is really only used to start the execution of the code on the EDT.

Read the section from the Swing tutorial on How to Make Frames.

The FrameDemo.java example code will show you how to structure your code so the frame is created on the EDT.

Then if you need user information you can use a JOptionPane to prompt for data. Execution will pause until the option pane is closed.
 
Squanch that. And squanch this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!