473,836 Members | 2,175 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 17041
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
3947
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 wait infinitely by default, even if i already set the executionTimeout to 90seconds in...
5
3040
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
3999
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
2380
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 60 seconds, because the .bat file it is calling is rebuilding mulitple .NET solutions and projects...
3
1432
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
2254
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 development machine is WinXP Pro. I'm using the ServiceController class to start and check the...
2
1696
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 green. The problem I'm having is when the button is clicked, the color never changes and the...
9
2759
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 void Button1_Click(object sender, EventArgs e)
1
1104
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
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10821
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10527
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9358
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6973
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5642
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4441
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4001
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.