JavaFX - method that waits for user input - java

I am writing a simple text game and I've dacided to move from displaying
in cmd to displaying in a custom window, that constists of one TextArea used
for output and user input. So I need a method that waits for the String that
user writes and then returns it.
In Swing i would do something like this (in a Window class):
public String nextToken() {
synchronized (nextToken) {
while (nextToken.isEmpty())
try {
nextToken.wait();
} catch (InterruptedException e) {
print(e.getLocalizedMessage());
}
String tmp = nextToken.remove(0);
lastToken = text.getText();
newLine();
return tmp;
}
}
And the ActionListener for hitting enter:
public void actionPerformed(ActionEvent arg0) {
synchronized (nextToken) {
nextToken.add(text.getText().substring(lastToken.length(), text.getText().length()));
nextToken.notify();
} }; };
But when I create a Window using JavaFX and then try to use this method the window freezes.
What to do so that the window would display correctly and would wait for user input?
I'd be thankful for any advice

Sounds like you're waiting for input on the main thread, which will block it thus your window freezes. Try putting the waiting code (nextToken method) in a separate thread.

Related

How to utilize sleep method for calculator error

I am trying to set the calculator text to an error message, wait for 2 seconds, then clear the field text. Below is my current code.
public static void wait(int ms) {
try {
Thread.sleep(ms);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
// TODO: 6/30/22 if decimal is clicked multiple times -> setText("Error")
if (field.getText().contains(".") && e.getSource() == decButton) {
field.setText("error text here");
wait(1000);
field.setText("blank here");
} else if (e.getSource().equals(decButton)) {
field.setText(field.getText().concat("."));
}
So far, the field text sets directly to ("blank here") and completely skips the error message. I have tried moving the error message to different areas of the program (within the if statement) but have yet to find a conclusion.
Are you using Swing? Is the code you show running from an event handler (triggered by keypress, mouseclick or something)? Then this cannot work.
The code is running in the Event Dispatcher Thread (EDT). Once you use something like field.setText() you have to exit your code and allow the EDT to fire the updates into the UI. But instead, you 'keep it busy' by waiting a second, then requesting the next update into the UI. Only then your method exits, and the user did not see the first message.
What you need to do is to set the update, then free up the EDT. How do you get the message to disappear one second later? Use a Swing Timer to trigger the action:
field.setText("error text here");
ActionListener errorHider = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
field.setText("blank here");
}
};
Timer t = new Timer(1000, errorHider);
t.setRepeats(false);
t.start();

Thread.sleep() Not Working Sometimes

Apologies for the bad title. I am working on a game that uses a JTextPane as the main text view. I use the following code to print to the pane:
public void write(String txt, MessageType t) {
try {
for (char c : txt.toCharArray()) {
text.getDocument().insertString(text.getDocument().getLength(), String.valueOf(c), t.getAttributes());
Utils.pause(30);
}
text.getDocument().insertString(text.getDocument().getLength(), "\n", t.getAttributes());
}
catch (Exception e) { e.printStackTrace(); }
}
That code writes each character one at a time, sleeping for 30 milliseconds between each writing. It then writes a new line character. Here's the pause() method if you're interested:
public static void pause(int millis) {
try { Thread.sleep(millis); }
catch (InterruptedException e) { e.printStackTrace(); }
}
Most of the time, this works fine. It writes each character with some space in between, giving the impression of typing. However, there is one instance where it does not work.
I have a JTextField that handles input. I added this KeyListener to it:
input.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
CommandParser.getInstance().parse(input.getText());
input.setText("");
}
}
});
At the end of the parse() method of CommandParser, if a command isn't found, this line runs:
Game.getInstance().getGUI().write("Invalid command.", GUI.MessageType.BAD);
However, for that line only, the program waits a second, then prints everything at once, instead of printing one character every 30 milliseconds. Why is this? Does this have to do with inner classes?
This is because your action is happening on the EDT thread. The GUI will not update at all until you return from that method. You need to use something like SwingWorker or SwingTimer to farm the updates out to another thread.

Android :UI Thread Blocked

In my application I have created one customized dialog box ,which is showed in both webview and normal android application,and also I doing some background operation when the dialog box is showed, when ever I called the mydialog function it shows my customized dialog box and also it return some values,it is perfectly working when I use webview+javainterface but it doesn't work with ordinary applications, the flow is
first I will get my dialog, after I do some process(here the main thread will wait ,dialog need to show,) then I will return the string ,the problem is dialog doesn't show when I called this function instead of that the dialog will showed after my background process finished.
I call this my dialog box like:
String sample=mydialog();
public String mydialog() {
String mystring = null;
try {
myactivity.this.runOnUiThread(ShowDialog);
while (customizeddialog.Getvalue() == null) {
}
mystring = customizeddialog.Getvalue();
customizeddialog.Setvalue(null);
} catch (Exception e) {
return mystring;
}
private Runnable ShowDialog = new Runnable() {
public void run() {
try {
customizeddialog m_dialog = new customizeddialog(myactivity.this);
m_dialog.setCancelable(false);
m_dialog.show();
} catch (Exception e) {
}
}
};
When you enter the synchronized block in mydialog() you acquire this's lock. Inside this synchronized block, you run ShowDialog() on the UI thread, and try to acquire this's lock again when you enter the synchronized block in ShowDialog.
Since the lock has already been acquired, it will wait until it is released in mydialog(), which will never happen because ShowDialog never executes past synchronized(this). What you have is deadlock.

How to make the main method wait for input on GUI without using Listener as a direct trigger?

I am working on a webscraping tool that should perform various operations with the scraped data.
Because of this, I need various different GUIs to work in an orderly manner and because of that, I need the main method to wait before each has completed it's purpose.
After searching for a while, I have found the following StackOverflow questions that provided some clues on how to solve the problem, but that I could not implement because they have some differences to my case:
How to wait for input in a text field
How to make main thread wait a different thread to finish
I know I can trigger code using a Listener to a/the GUI's components (a button, for example), but i'm having a hard time making the main-thread wait for that listener to wake it up, while the code for the GUI's thread (when there is one) is initialized by the main thread...
This is an simplified code to demonstrate how the program is supposed to work:
public class Main {
/*
* Waiter is a simple GUI with just an "Start" button in it. Here in place of my actual GUIs.
*/
private static Waiter auth; //Represents my NTLM-authentication form.
private static Waiter status; //Represents a status-feedback GUI that will be displayed during processing.
private static Waiter operation; //Represents a GUI in with the user choses what to do with the gathered data.
public static void main(String[] args) throws InterruptedException {
auth = new Waiter();
auth.setVisible(true);
System.out.println("NTLM Authentication form. Should wait here until user has filled up the GUI and clicked \"Start\".");
System.out.println("Authenticates WebClient's NTLM using data inputed to the GUI...");
auth.dispose();
Thread srt = new Thread(status = new Waiter());
srt.start();
status.setVisible(true);
//Performs webscraping operations...
System.out.println("Prepares the webscraped data here...Things like downloading files and/or parsing text...");
System.out.println("Keeps the user aware of the progress using the \"status\" GUI.");
status.setVisible(false);
//Clears the status GUI.
operation = new Waiter();
operation.setVisible(true);
System.out.println("Operation selection form. Should wait here until user selects an option.");
System.out.println("Starts performing the operation(s)...");
operation.dispose();
status.setVisible(true);
System.out.println("Performs the operation(s), while giving status-feedback to the user.");
status.setVisible(false);
System.out.println("Displays a file-save dialog to save the results.");
System.out.println("And finally, displays a \"End of operations\" dialog before ending.");
}
}
UPDATE 1:
The main difficulty I'm having is to implement something like this (this is what I want to do):
//Main method...code...
Thread srt = new Thread(status = new Waiter());
//Before "srt.start();"...
status.startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
main.continueExecution();
}
});
//Thread's run() being something like "status.setVisible(true); main.waitGUI();"
srt.start();
//continues here after the Listener is triggered...more code...
Instead of this (what is being the solution to most other people, if I'm understanding it right...) (this is what I don't want to do, if possible):
//GUI before this one...
//code...
Thread srt = new Thread(status = new Waiter());
status.startButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
/*
* Code that should come after this GUI.
*/
}
});
//Thread's run() being something like "status.setVisible(true);"
srt.start();
//"ends" here...(Initial code or GUI before this "status")
In other words, I'm having trouble implementing the GUIs and Listeners in a way to trigger main's thread's "sleep" and "wake up" actions, instead of triggering actual processing code.
UPDATE 2:
Following #JB_Nizet 's tip on SwingUtilities.invokeLater(), I took a good look at the SwingUtilities docs, and after I found out about how the SwingUtilities.invokeAndWait() method works, and I think I've found how to do it, using a combination of Semaphore and invokeAndWait().
I need someone with a better understanding of multi-threading and/or GUIs to confirm if it's a safe, valid solution or not. (I'll then edit the question and clean it up, and if confirmed, post this in proper "answer format")
Anyways, here goes the modified code, which seems to be working for me:
public class Main_Test {
//Semaphore:
public static Semaphore semaphore;
//GUIs:
private static Waiter auth; //Represents my NTLM-authentication form.
public static void main(String[] args) {
try {
semaphore = new Semaphore(1);
// semaphore.acquire();
auth = new Waiter() {
#Override
public void run() {
try {
System.out.println(Main_Test.getThread() + this.getName() + " has been created and is now running.");
semaphore.acquire(); //Makes main pause.
this.setVisible(true);
} catch (InterruptedException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
auth.jButton1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(getThread() + "NTLM has been hypothetically authenticated.");
semaphore.release(); //Makes main continue after GUI is done.
auth.dispose();
}
});
// semaphore.release();
SwingUtilities.invokeAndWait(auth);
semaphore.acquire(); //<- Where the main effectively gets paused until the permit is released.
/*
* GUI's run() will accquire the semaphore's permit.
* The invokeAndWait() garantees (?) it will happen before main's acquire().
* This causes the main to pause when trying to acquire the permit.
* It stays paused until the actionListener release() that permit.
*/
System.out.println(getThread() + "This message represents the processing, and should come only after the hypothetical NTLM authentication.");
} catch (InterruptedException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Main_Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String getThread() {
return String.format("%-32s --- ", Thread.currentThread().toString());
}
}
I'm not sure I have completely understood what you want to do, but it seems to me that you have a consumer thread (the main thread, waiting for events from the event dispatch thread), and a producer thread (the event dispatch thread).
The typical way to implement this is to use a blocking queue as a communication mechanism:
Create a blocking queue
Create your GUI and pass it the blocking queue
start a loop which gets data from the queue. Since the queue is blocking, the main thread will be blocked untile there is something in the queue
Have your event listeners, running in the EDT, post data to the blocking queue

Delay is affecting entire Block

I have a code right below...take a look.
enter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (enter.getText().length()>0){
addToChat("You: "+enter.getText());
enter.setText("");
delay(1000);
addToChat("oie");
}
}
});
And here is the delay void.
public static void delay(int delayTime){
try
{
Thread.sleep(delayTime);
} catch (InterruptedException ie)
{
}
}
The problem is whoever I type something into the text box and hit enter, it takes one second for not only the one to show up in the text area, but also the "You: " text block to show up, which is before the delay. Why is this delay affecting things BEFORE it and how can I fix this?
The UI does not get a chance to update before your action listener is finished. If you would like to change something after the delay, you should schedule it on a different thread, rather than wait inside the event handler:
addToChat("You: "+enter.getText());
enter.setText("");
new Thread(
new Runnable() {
public void run() {
delay(1000);
addToChat("oie");
}
}
).start();
You're sleep()ing in the Event Dispatch Thread, which means your UI is frozen and can't repaint itself, or accept input, or anything. You should only perform very quick actions in the EDT to avoid this effect. Check out the Graphical User Interfaces and following tutorial trails for the basics of UI programming.

Categories

Resources