I am building a grpahic interface with different buttons,
and when user clicks a button I use :
actionPerformed(ActionEvent e) {
Object source = e.getSource();
if
else if
else if
...
}
The problem is that when the user clicks a button, actionPerformed(ActionEvent e) is called, and it enter in the if corresponding to e.getSource();.
And it executes all instructions in the corresponding if. But I want to make possible that the user clicks different buttons, so that a actionPerformed(ActionEvent e) { is called, even if the instructions of the previous actionPerformed(ActionEvent e) { is not finish yet.
I don't know if you understand, but thank you if you can help me !
You will need to use background threads if you want the GUI to be responsive while a long-running process is occurring. A SwingWorker works well for this in Swing GUI's. Please check out Concurrency in Swing for all the gory details on this.
Examples:
mvc-progress-bar-threading
why-does-swingworker-stop-unexpectedly
Google Search
You need to use SwingWorker . It does the work in background without freezing the UI.
Here is the question you can have a look for how to use them.
Or you can use official oracle documentation for SwingWorker.
Related
When the actionPerformed method is invoked I would like it to display the desired icon on the first button, delay for 1 second and then display the icon on the second button. The icons always display simultaneously? Not sure how to correct this.
#Override public void actionPerformed(ActionEvent e)
{
btnTest1.setIcon(img2);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
btnTest2.setIcon(img2);
}
I'm assuming this is for JavaFX / Java Swing. I'm not too familiar with it, but I believe the threads aren't working is because there are concurrency rules related to using threads in swing apps. I've had similar issues and wrote a question about how to delay something using threads. I found sources for using a timer object. It might be better to implement the timer by doing:
import javax.swing.Timer;
I'm sure other more advanced users may give more detail. This is maybe another option. Oracle might help too:
https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
The buttons' icons are set with setIcon, but that doesn't mean they are painted immediately. Both icons will only be painted after you return from your actionPerformed method. What you need to do is set the icon for the first button, then use Timer (see zOrigin_'s answer) and after the timer elapses, set the second button's icon.
i am doing a quiz in java and i figured out everything except i want to put in a progressbar.I want to exit my actual frame(or to show something) when the progress bar is filled(i used it as a timer), how can i do it? I don't know how to do something when the bar is filled. The code i have so far, it's a progress bar that fills for 30 secs or something very close to that.
al=new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(jProgressBar1.getValue()<100){
jProgressBar1.setValue(jProgressBar1.getValue()+5);
}
else{t.stop();}
}
};
t=new Timer(1500,al);
Simple, put more code into this else{t.stop();} block. Here is where you would exit whatever it is you wish to exit.
else {
t.stop();
// exit whatever window you wish to exit
}
Note that this is as about as detailed as we can go since you've not given more information about your current code.
Obligatory link: The Use of Multiple JFrames, Good/Bad Practice?
I know many people have asked this question before but I couldn't find any answer that solved my problem. My code is like this:
public void mouseClicked(MouseEvent arg0) {
TEXT.setText("ON");
myfunction(); //runs for a very long time
}
The original text of JLabel is "OFF". Now I want to change the text to "ON" when the mouse is clicked but the text doesn't set until myfunction() is complete (which may take several minutes).
I have tried the invalidate function, making a separate function for setting the text but nothing is working.
Please help me with this problem!
The problem is that mouseClicked(...) is executed on the UI Thread. That is the Thread that is responsible for handling all sorts of user actions (like a mouse click) and also the drawing of components (like updating the text of the label on screen). If you execute your long running method call on the UI thread, it will be blocked and can't draw anything until execution is complete. You'll have to use multi threading to get around this problem.
The following might not be the most elegant solution, but if you are new to multi threading it will get the job done:
public void mouseClicked(MouseEvent arg0) {
TEXT.setText("ON");
(new Thread() {
public void run() {
myfunction();
}
}).start();
}
It will spawn a new Thread that handles your method, which will let the UI Thread continue doing its thing. consider deactivating the button that has just been clicked, so the user can't start the execution while it is already in progress (which usually is what you want..)
I know the title didnt say much, but here I go with a clearer explanation:
I have made a tool/app in netbeans that extends JFrame. It has a button and i've added a keyListener when the button is pressed. It gets if the VK_DOWN button is pressed, if yes, it presses it 4 times and then presses enter. this works very fine, but I want to use this in a game. But when I have the app running, if i click on something else, it takes away the priority (i hope thats the right word here..), it doesnt work anymore unless I click on the app and make it top priority again..
Here is the code for the button:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jButton1.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
// TODO: Do something for the keyTyped event
}
public void keyPressed(KeyEvent e) {
// TODO: Do something for the keyPressed event
if (e.getKeyCode() == e.VK_L){
System.out.println("Works");
}
if (e.getKeyCode() == e.VK_DOWN){
System.out.println("Down Arrow Pressed!");
e.setKeyCode(e.VK_DOWN);
e.setKeyCode(e.VK_DOWN);
e.setKeyCode(e.VK_DOWN);
e.setKeyCode(e.VK_DOWN);
e.setKeyCode(e.VK_ENTER);
}
}
});
}
I want to press the VK_DOWN arrow key in the game and let the app press it 4 times and then press enter by itself. This works only when I'm running the app on top of everything else; but if Im running the game, the game takes the higher priority and so the app dont work ..
Please help.
Check out this API:
"JNativeHook is a library to provide global keyboard and mouse hooking for Java. The primary purpose is to provide a portable and reliable method for delivering keyboard and mouse events to a Java application that would otherwise be inaccessible. This is beneficial for applcatiions that run in the background but may require user interaction though hotkeys or mouse events."
http://code.google.com/p/jnativehook/
You cannot do what you are looking to do with Java unless you use some native code.
Uh, not sure whether it will work, but try the Robot class from awt package to generate your Key-Events. It shall work better than your code.
Alright, I tried this on CodeReview, but no one responded. I am an eight grader with a school project on finding the total permutations of two numbers. I have the guts of it done, but I want to incorporate GUI. And to use GUI, I need ActionListener's. The catch is, I don't know how to use ActionListener's. Any ideas? Note that if you want the code, I can post it.
Thank You!!!!
The Swing Java Tutorial is your friend - How to Write an Action Listener.
Wrtiting an ActionListener is very simple. For example:
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Do something here
}
});
Read more on event listeners here.