Method after a ActionListener [duplicate] - java

This question already has answers here:
Waiting for multiple SwingWorkers
(2 answers)
Closed 8 years ago.
I need to perform a method after the clicking of a JButton in a Java Project.
I'm making a client-server game and after the click of a button I need that the client/server start to wait untill the opponent perform a click. The problem is that at the end of the action listener code I start a loop end untill the opponent don't perform another click the jbutton stays clicked..
public void actionPerformed(ActionEvent e) {
JButton o = (JButton)e.getSource();
String name = o.getName().substring(3);
Click(Integer.parseInt(name));
if(isServer)
ListenServer();
else
ListeClient();
}
ListenServer() and ListenClient() are two loop function... How can I call this methods AFTER the click??? Thanks and sorry for the bad english

You can use threads and synchronization. See https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html for examples.

Related

Display of Java Component is delayed [duplicate]

This question already has answers here:
Will Runnables block the UI thread?
(4 answers)
How to update swing GUI from inside a long method?
(3 answers)
Closed 3 years ago.
I'm facing a strange behavior from the Java Runtime(VM) when I'm trying to display a label or progress bar on the screen when something happens.
The scenario is like this:
the user will press a button to do a certain action
this action is a kind of process on the DB which could take some seconds
I'm displaying a message in a Jlabel field which is already in the form but set to be invisible when the form opens.
Then when the user presses a button I'm setting this label to be visible and do some processing and then set it back again invisible as it was, which is very simple logic.
problem is the Jlable is displayed after the processing is done and not before.
The same problem with happens also a progress bar.
Any explanation why is this happening?
here a sample of the code
Mylable.setVisible(false); defaullt status when the form opens
Event Occurend (user clicked a button)
Mylable.setVisible(true);
....... do some process here
Mylable.setVisible(false);

OutputStream JTextArea Thread.Sleep Causes Freezing [duplicate]

This question already has answers here:
java thread.sleep puts swing ui to sleep too
(3 answers)
Swing - Thread.sleep() stop JTextField.setText() working [duplicate]
(3 answers)
Closed 4 years ago.
I'm working on a game project where I'm creating a GUI using JSwing. It looks something like this:
A lonely 'F'
I'm trying to create a 'typewriter' effect, and so have a Thread.sleep(100) in my custom PrintUtils method (let's call it printToGui). I've routed System.out to the TextArea in the center of the GUI, and the first time I run the program the 'typewriter' effect works great and the startup text I send through printToGui shows up as it should. However, whenever I click a button (which triggers more text getting sent through printToGui), the app freezes, and won't unfreeze until the length of time it would have taken to print with Thread.sleep() finishes, spitting out all the text at once.
What I've figured out is that the first time I start the app, Thread.sleep() is happening on the "main" thread, while every time I click a button it's happening on the AWT-EventQueue-0 thread. I assume this is the problem, but maybe it's not; how do I get future output to the GUI to happen on the "main" thread and not the new one? Thanks!

Jframe form setVisiblity(true) is having effect after method termination [duplicate]

This question already has an answer here:
using sleep() in swing
(1 answer)
Closed 5 years ago.
I am creating an object of Downloader JFrame class in the constructor of the main form and calling its setVisiblity(true) method on download button click.
The problem is Downloader frame is showing but when the method has termininated:
Downloading dn = new Downloading();
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
dn.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(GehuConnectMain.class.getName()).log(Level.SEVERE, null, ex);
}
}
The form is showing after 5 second what is the solution ?
Simple:
Thread.sleep(5000);
You are sleeping on the Event Dispatcher Thread. That will freeze your whole application. You have to step back and look into invokeLater to ensure "correct" threading within your UI.
Or, maybe more appropriate: why do you intend to sleep in the first place. The user clicks that button; and you raise that other frame. If you now have to "wait" for something else; then that needs to happen "in a different" way.
One typical answer would be to use a modal dialog for example. Don't try to re-invent the wheel here.

Java - Adding keyboard shortcut for JButton [duplicate]

This question already has answers here:
Allowing the "Enter" key to press the submit button, as opposed to only using MouseClick
(7 answers)
create hot keys for JButton in java using swing
(4 answers)
Closed 7 years ago.
I'm trying to add an accelerator/keyboard shortcut to a JButton in a GUI in Eclipse (Luna).
I'm trying to replicate what the program would do when a "Login" button is pressed, but I also want it to work when the "Enter" key on the keyboard is pressed.
I've tried "Add event handler > key > KeyPressed" for the JButton and even tried the JPasswordField/JTextField, but then the code I try doesn't end up working.
This is the auto-generated code for the GUI elements:
loginButton = new JButton("Login");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
checkPassword();
}
});
loginButton.setBounds(146, 117, 165, 23);
frmIpassAccountManager.getContentPane().add(loginButton);
I've spent a fair amount of time searching Stackoverflow and other websites for a solution but am yet to find a solution. I've added accelerators to JMenuItems before, but I believe it works differently to JButtons, ect.
If some one could please tell me what code to add to which GUI element that would be extremely helpful.
Thank you.

if statement only executes if I print to terminal before [duplicate]

This question already has an answer here:
Loop doesn't see value changed by other thread without a print statement
(1 answer)
Closed 7 years ago.
I am using the javax.swing packages to construct a gui, and have a while loop running forever with an if statement inside, to see if a button has been pressed. For some reason unbeknownst to me, this code only works if I print to the console inside the while loop.
Here is the algorithm structure:
while(true){
System.out.println(" ");
if (startOver && playPressed) {//set to true on JButton press
//do stuff
}
}
Possibly this is some sort of threading issue? Has anyone ever encountered such a problem before? Is there a method of waiting for a JButton to be pressed that doesn't involve an infinite while loop?
Thanks in advance!
Swing is event-based. You're not supposed to have infinite loops waiting for a button to be pressed. The loop is implemented by Swing internally. What you're supposed to do is to add an action listener to the event, which will be called by the button when it's clicked:
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("The button has been clicked!");
// do stuff
}
}
This is fundamental stuff when dealing with Swing. You should read the swing tutorial.
Any infinite loop in the swing event dispatch thread will freeze the GUI forever.
The only way I was able to overcome this problem without an infinite loop was by creating a new thread for the "//do stuff" portion of the algorithm.
very strange, and I still don't understand why I had to print to console to make it work within the while loop.

Categories

Resources