How to stop program until a button is pressed in JAVA? - java

How do I make it so that the program doesn't keep reading in code until the button is clicked?
Why?: I have a 10x10 grid with buttons in each part and then code running depending on what is clicked. However, my program keeps reading in code so there is never a choice being made and it gives me error. I tried giving it a infinite loop until a button is pressed, but that doesn't work out so well
-edit
I'm a complete beginner with Java.
This is a picture of the GUI
http://imageshack.us/content_round.php?page=done&l=img843/5351/sascp.png
What I want is for the code to not keep running step by step until I click a button.
E.G.:
create gameGUI
wait until and check which button is pressed
if(buttonClicked[i][k] == something){
System.out.println("lool");
}
But what's happening in my code is that it creates the gameGUI and then because the user isn't fast enough to click it just skips over the if statement or gets a run-time error because nothing was pressed.

In both Android & Swing (& I'd expect J2ME), buttons fire events when told to do so (by activating them). You would generally just wait for that to happen before doing anything, and not bother with what the rest of the GUI is doing (or not doing) at the time.
Or in other words:
Add an ActionListener to the buttons.
In the actionPerformed() method, insert the code that you have above.
Also
The code snippet provides almost no useful information. For better help sooner, post an SSCCE.
That GUI looks like Swing to me. If it is not, then what is it?
Please always copy/paste run-time errors.

Related

Stuck trying to advance to the next loop in GUI class

I'm a long time observer, and first time poster. So forgive me if I seem to skip over some typical guidelines.
Posted below is a link to my GUI class, where I display different "screens"/JPanels in my client program. The client keeps track of these screens by referencing a state variable, which is documented at the top of the class.
Upon launching the client, the GUI builds the JFrame, then starts attaching things to it. First up is a splash screen, then after a short time it moves on to displaying a menu. This part should be clear in the code. The problem I'm experiencing occurs when a menu item component is clicked on. The GUI properly removes the menu from the window, however it doesn't move on to displaying the login screen. Both the menu and, login screens are attached in a similar fashion. Despite my best efforts to debug the code by printing messages at each step, I can't seem to figure out why after changing the state to "3", the GUI doesn't advance into the "Login" phase of the loop and, display the login screen.
http://pastebin.com/xWDpjD14
Also, I should note, the state is being changed by a Mouse Listener class attached to each menu item of the menu screen. I've already debug'd this part. I just can't figure out why the loops aren't working as expected.
Sorry I don't have a SCCE(sp?) to attach, It's my first post, and I'm not quite familiar with the sites posting options yet.

Java, KeyListener working while tabbed out

I am making a program for a friend of mine who is trying to break the world record in counting (yes, there is a world record for that). It's a simple program made in java with the swing library that is always on top (so that he can see it while he's gaming) with a simple JLabel in it that keeps track of how far he has counted. I added a hotkey that adds +1 to the counter in the JLabel every time he presses it.
My problem is that when the window is not selected i.e when you (if the program wouldn't be on top) otherwise would be tabbed out. The hotkey is not working. It seems you have to have the window focused for the keylistener to work.
Is there any way to get this to work?
Thanks in advance!

how to reuse a jframe without making it new?

I was wondering if there was a way to reuse a jframe without making it new. You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two). One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button.I forgot to mention that the first jtextarea contains a "back" jbutton, that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.
I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?
You state/and I reply:
I was wondering if there was a way to reuse a jframe without making it new.
Yes, it is quite possible to re-use components (the generalization of your question).
You might be wondering why. I have two JFrames (actually more, but for this question's purpose, two).
As has been stated, this is generally not a goood idea. Most Swing GUI applications use only one main window, the JFrame, and then either swap views such as with CardLayout or JTabbedPane, or show modal or non-modal dialog windows.
One contains a radio button(agree) with the terms and conditions written in a jtextarea. THe other JFrame contains a passwordtxtarea(password), jtextarea(username) and a radio button(read terms and conditions), as well as a "TermsAnd Conditions" button. I forgot to mention that the first jtextarea contains a "back" jbutton,
It's most unusual for a JTextArea to have a button of any kind. Also, there is no such thing as a "passwordtxtarea", perhaps you mean JPasswordField? If so, please be precise with your terms when asking questions here. It's hard enough to guess what someone's program is like based on a description, that you don't want to make it harder on us. Also, it's very unusual to use a JTextArea for a user name field, since usually you'd use a JTextField. Again, precision really matters. Else we'll likely give you the wrong advice.
that if i press on, I get JFrame2, when I press "Terms And Conditions", I get JFrame1. The problem is, that my code requires both "Agree" and "read the Terms" radio buttons to be clicked on, but whenever I press "back" or "Terms And Conditions", any input I had put in (username, password, clicks on radio button other than default) is lost. Therefore I cannot proceed in my program.
Yes, you should not be creating new components here but rather re-using previously created components. It's all do-able if you make your component a class field and if you make sure to create it only once. It's all how you code it.
I think it has to do with the fact that I have to make a NEW JFrame Form. Maybe it sets it back to default? Anyway, how do I fix this problem? I haven't seen a question like this, so is there a blatantly obvious answer I'm unable to see, except for "it's impossible"?
Again it's possible. The solution will all depend on the structure of your program.
A word of advice: gear your GUI code toward making JPanels, not JFrames. This way you can place them anywhere they are needed -- in a JFrame, a JDialog, another JPanel, or swapped with a CardLayout,... anywhere. It greatly increases the flexibility of your program.

Show a panel after x seconds after button click

i'm extremely new to java. i have a simple program here and what i want is to show a certain JPanel, after x seconds delay, when a button is clicked.
what i want in particular is something like a "start game" button, and upon clicking it, a panel that contains a "loading..." animation would be shown temporarily in the JFrame in x seconds, then another panel would show up after this.
i'm aware Timers would be the answer to this. but i've done all the thinking and research that needs to be done first. my last resort is to ask it here. i need at least a sample code upon which i can figure out the rest by myself
i'm badly in need. i have acquired poor java knowledge in a short period of time and my instructor expects a spectacular output from me. thanks in advance.
Simply write Thread.sleep(x*1000); after clicking the button.
It will make the application wait for x seconds. After completing x seconds application will proceed further.

Is Dialog.show() a non-blocking method?

I've got a button that kicks off a background thread to do some work and I am trying to use a ProgressDialog to prevent the user from double clicking that button (or any other ui elements) while that work is being done. The first thing I do in my buttons onClick code is to display the progress dialog, which takes over the screen. The problem I am seeing is that if I rapidly tap this button, sometimes two or more presses will register before the ProgressDialog is shown. This leads me to assume that ProgressDialog.show() is returning before the ProgressDialog is actually visible.
Can anybody confirm this? Also, is there a way to change this behavior, or at least get a notification of when the dialog is actually visible? I saw Dialog.onStart() but given the javadoc, this appears to be called before the Dialog is actually visible...
UPDATE:
While it appears that there is no good way of solving this problem in general, the following works for my situation where my work is done by an external thread and the amount of work to do takes longer than the time it takes for all the button clicks to be processed:
void myOnClickHandler() {
if(myButton.isEnabled()) {
myButton.setEnabled(False);
// do work here
// setEnabled(true) is invoked at the end of my spawned thread's run().
}
}
No.
The problem is you clicked many times before the click event is delivered. (i.e. it is queued before you run ProgressDialog.show().)
From what I've noticed in Android you can double click on a button rapidly and have the onClick listener fire twice (or even more) regardless of the code in the listener (even if you disable the button immediately).
I reported a bug a while ago here: http://code.google.com/p/android/issues/detail?id=20073 but of course these things tend to go "unnoticed" by Google. Feel free to star it in hopes of getting Google's attention

Categories

Resources