473,587 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Waiting for a button click.

78 New Member
Hi guys. I don't know how to implement this so i was hoping if any of you can help me.

I have a code like this one:
Expand|Select|Wrap|Line Numbers
  1. public static void main (String args[])
  2. {
  3.     //create frame and button
  4.     ....
  5.  
  6.     frame.setVisible(true);
  7.     frame.add(button);
  8.  
  9.     do {
  10.          ....
  11.         // wait for a button click
  12.  
  13.         //code to be executed after a button click
  14.          .....
  15.  
  16.         }while (true);
  17. }
So i want to do some stuff and wait for a button click to continue. I set up a mouse listener and add it to the button (although the listener does nothing). The problem is, how can i wait for a button click. If this were a non-GUI application, i'd use a scanner and wait for an input from the user, which automatically halts the program. I figured using sleep would hang the application.

Any thoughts?

thanks a lot.
Oct 24 '07 #1
10 16993
r035198x
13,262 MVP
Hi guys. I don't know how to implement this so i was hoping if any of you can help me.

I have a code like this one:
Expand|Select|Wrap|Line Numbers
  1. public static void main (String args[])
  2. {
  3.     //create frame and button
  4.     ....
  5.  
  6.     frame.setVisible(true);
  7.     frame.add(button);
  8.  
  9.     do {
  10.          ....
  11.         // wait for a button click
  12.  
  13.         //code to be executed after a button click
  14.          .....
  15.  
  16.         }while (true);
  17. }
So i want to do some stuff and wait for a button click to continue. I set up a mouse listener and add it to the button (although the listener does nothing). The problem is, how can i wait for a button click. If this were a non-GUI application, i'd use a scanner and wait for an input from the user, which automatically halts the program. I figured using sleep would hang the application.

Any thoughts?

thanks a lot.
You don't use a mouselistener for listening for button clicks. Use an ActionListener instead. The code for handling the button click goes into the actionPerformed method.
Oct 24 '07 #2
Sebouh
78 New Member
Yes, you're right. I forgot about that.

But still, how would i get the application to wait for the click. I thought of something like this:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     boolean clicked = false;
  3.     ...
  4.     while (!clicked) {
  5.         Thread.sleep(1000);
  6.     }
  7.  
  8.     ...
  9. }
  10.  
  11. actionPerformed() {
  12. clicked = true;
  13. }
But this would hang the application every time it sleeps.
Oct 24 '07 #3
r035198x
13,262 MVP
Yes, you're right. I forgot about that.

But still, how would i get the application to wait for the click. I thought of something like this:
Expand|Select|Wrap|Line Numbers
  1. ...
  2.     boolean clicked = false;
  3.     ...
  4.     while (!clicked) {
  5.         Thread.sleep(1000);
  6.     }
  7.  
  8.     ...
  9. }
  10.  
  11. actionPerformed() {
  12. clicked = true;
  13. }
But this would hang the application every time it sleeps.
Just don't do anything until the actionPerformed method is called.
Oct 24 '07 #4
Sebouh
78 New Member
I'm not sure what you mean by do nothing. If the the code is:
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 1;
  2. //wait for button click
  3. x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
Oct 24 '07 #5
r035198x
13,262 MVP
I'm not sure what you mean by do nothing. If the the code is:
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 1;
  2. //wait for button click
  3. x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 2
goes into the actionPerformed method.
Oct 24 '07 #6
Sebouh
78 New Member
Expand|Select|Wrap|Line Numbers
  1. x = 1 + 2
goes into the actionPerformed method.
I understand, but my original code had a while loop. I can't divide the while loop and put part of it in actionPerformed (), can i?
Oct 24 '07 #7
r035198x
13,262 MVP
I understand, but my original code had a while loop. I can't divide the while loop and put part of it in actionPerformed (), can i?
Like this?
Expand|Select|Wrap|Line Numbers
  1. while(true) {
  2.     //some stuff
  3.     if(clicked) {
  4.          //do stuff
  5.     }
  6. }
and you set the clicked flag in actionPerformed

P.S Careful that you don't run yourself into an infinite loop
Oct 24 '07 #8
Sebouh
78 New Member
I'm afraid i will run to an infinite loop. If the player takes his time to click the button, then i'll constantly loop over the other part.

I was hoping to some sort of a "wait" solution that halts the application. I mean, how does the scanner work when i request input? Does it loop too, or blocks the program? I'm thinking more in terms of block and sleep - click button - interrupt - wake up thing.
Oct 24 '07 #9
r035198x
13,262 MVP
I'm afraid i will run to an infinite loop. If the player takes his time to click the button, then i'll constantly loop over the other part.

I was hoping to some sort of a "wait" solution that halts the application. I mean, how does the scanner work when i request input? Does it loop too, or blocks the program? I'm thinking more in terms of block and sleep - click button - interrupt - wake up thing.
Even then someone/thread has to keep checking to see if the button was clicked.
You ran into an infinite loop because you had not handled all the conditions and included a break point from the loop.
Perhaps if you post the full code you are using and explain what you want to achieve with it ...
Oct 24 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3928
by: Dicky Cheng | last post by:
Hi, I am using .net remoting technology. I set up a .net remoting client and server in IIS. When the client calls the server, the server will run a long duration method (30-60seconds). I have a test on it that if the network broken at the time the client have already send the remoting request and waiting for the server, the client side will...
5
3029
by: Anonieko | last post by:
Scenario: This is a reporting scenario where it takes a while to get back a response ( report page ) after submit. I want my user to see some kind of temporary message like 'Processing...' or 'Waiting.. ' after I clicked on a SUBMIT button to post a web form.
1
3937
by: ad | last post by:
I have a time-consuming job in button's click event; I wnat to show a "Please waiting.. " message on the form. I put a Label (named labResult ) on the form , and write the codes below, but the message did not appear. How can I do?
12
2359
by: Raymond Lewallen | last post by:
How to wait for a process to stop completion is my goal. Obviously, the looping while waiting for the HasExited property is not a solution.. but thats the best I can come up off the top of my head. Natuarally it will not work. I expect it to use up all resources looping, which will not allow the process to complete. The process takes about...
3
1423
by: andreas | last post by:
Hi, Is it possible to stop a sub in the middle waiting for a button clic to go further ? F.e. dim blnToStop as boolean private sub test ....... .......
4
2238
by: Jeff | last post by:
Hi - I'm building a VB.NET Windows application with an MSDE database. Anticipating the possibility that the MSDE service may have been stopped outside of my app, I'm trying to code a way to start it when my app is launched. I can't seem to get the code to wait for MSDE to start before continuing (the next step is opening a connection). My...
2
1687
by: D | last post by:
I am trying to do the following using Python and Tkinter: 1) Display a window with 1 button 2) When user clicks the button, Python attempts to call a function that opens a socket and listens for a connection - what I want to do is, if the socket has been successfully opened and the system is waiting for a connection, to turn the button...
9
2735
by: Jonathan Wood | last post by:
Does anyone know of any reason a button on a master page would have no effect? I have a complex HTML page that I'm converting to ASP.NET, and acknowledge I could have something odd that is affecting this. But I'm stuck. My handler is in C# code and the code looks right. But absolutely nothing happens when I click the button. protected...
1
1095
by: Marine | last post by:
Hi, I have two pages : Page1 and Page2. On page1, I have a Response.Redirect("Page2?id=test"). On Page2, on the Page_Load event, I'm connecting to a Sql Server so it can take a while. I would like to show a waiting message. For the moment, the Page1 remains until the Page2 is loaded, and a
0
8205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.