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: - public static void main (String args[])
-
{
-
//create frame and button
-
....
-
-
frame.setVisible(true);
-
frame.add(button);
-
-
do {
-
....
-
// wait for a button click
-
-
//code to be executed after a button click
-
.....
-
-
}while (true);
-
}
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.
10 16993
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: - public static void main (String args[])
-
{
-
//create frame and button
-
....
-
-
frame.setVisible(true);
-
frame.add(button);
-
-
do {
-
....
-
// wait for a button click
-
-
//code to be executed after a button click
-
.....
-
-
}while (true);
-
}
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.
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: - ...
-
boolean clicked = false;
-
...
-
while (!clicked) {
-
Thread.sleep(1000);
-
}
-
-
...
-
}
-
-
actionPerformed() {
-
clicked = true;
-
}
But this would hang the application every time it sleeps.
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: - ...
-
boolean clicked = false;
-
...
-
while (!clicked) {
-
Thread.sleep(1000);
-
}
-
-
...
-
}
-
-
actionPerformed() {
-
clicked = true;
-
}
But this would hang the application every time it sleeps.
Just don't do anything until the actionPerformed method is called.
I'm not sure what you mean by do nothing. If the the code is: - x = 1 + 1;
-
//wait for button click
-
x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
I'm not sure what you mean by do nothing. If the the code is: - x = 1 + 1;
-
//wait for button click
-
x = 1 + 2;
how can i do nothing? I have to put something in between to keep the app waiting, don't i?
goes into the actionPerformed method.
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?
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? - while(true) {
-
//some stuff
-
if(clicked) {
-
//do stuff
-
}
-
}
and you set the clicked flag in actionPerformed
P.S Careful that you don't run yourself into an infinite loop
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.
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 ...
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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.
|
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?
|
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...
|
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
.......
.......
| |
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...
|
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...
|
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...
|
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
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |