|
I want to make it so that when the user launches the app, it will display the GUI but at the same time it will launch other threads (multithreading) that will do some calculations and when those are finished they will update some GUI elements (which are always created before the thread with calculations is launched, that is not a problem of getting null), also when a button is pressed same as before using multithreading it will do some calculation and update the GUI after each thread is done.
Now the problem I have is that if you are using Thread(new Runnable)/start(); to launch multiple threads (even one) you get into the error Not on FX application thread . As possible solutions I read about JavaFX Task and Service but those were freezing the GUI thread until they were done.
This is what I tried ("Functions.functionX" is the placeholder, the real function takes between 0-20 seconds to finish):
button.setOnAction(event -> {
Service<Void> doSomething = new Service<Void>() {
@Override
protected Task<Void> createTask() {
Functions.functionX("parameter");
return null;
}
};
doSomething.start();
});
Now the problem with this is like I said, when the button is pressed, the GUI is unusable until the function finishes. Using straight Task had the same effect unfortunately.
What do I need to change/add to make it so that I can launch multiple threads at a time, and inside them to change elements from GUI (right now I only want to update some Labels with .setText() inside those threads, each thread updates one Label)?
modified 21-Jul-23 13:02pm.
|
|
|
|
|
Where do you get the "Not on FX application thread" error? When trying to launch all those threads or when you try to update the GUI from within one of those threads? If it's the former, I can't help. If it's the latter, you simply need to run your update code on the UI thread. I don't use JavaFX but in my Java / Android, this works fine.
Thread worker = new Thread (new Runnable ()
{
@Override public void run ()
{
activity.runOnUiThread (new Runnable ()
{
@Override public void run ()
{
}
});
}
});
worker.start ();
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
modified 21-Jul-23 15:11pm.
|
|
|
|
|
I get the error when I call .setText() . Everything related to the GUI must run on FX thread, but if you are running something intensive on that, then the interface will freeze, that is why you should run that on a different thread and make the updates later.
|
|
|
|
|
And that is exactly what my code does. It creates the thread worker from the UI thread and starts it. It then exits that UI function, effectively releasing the UI thread. Meanwhile, worker is executing its Run method separately from the UI. After it finishes doing stuff, worker then creates and runs a UI thread that updates the UI. JavaFX may have a different method to call instead of runOnUiThread , but the logic is the same. JavaFX has to have an equivalent.
Don't be confused by the code appearing to all be in the initial UI function. Each of those Run methods is a chunk of code that executes in a separate and distinct thread.
One caveat -- when running on Android, I have found that this doesn't always work to release the UI when it is done from within view creation / fragment startup. I don't know if JavaFX has the same issue. What I've shown works when launched from the UI once the screen is up and established; for example, from a menu item click handler.
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
I managed to make it work but not with Task or Service. On button action I created a new Thread thread = new Thread(new Runnable() {...}); , and after I was done with the calculation, still inside thread run function I called:
Platform.runLater(new Runnable() {
@Override
public void run() {
label.setText(newValue);
}
});
I am still interested what I should do to make it work with those too, if someone can explain what I was doing wrong.
|
|
|
|
|
I am complete fresher at Java. Could You help me with https:
|
|
|
|
|
Asking the same question will not change the answers.
No, we're not doing your work for you, and you haven't described a problem you're having.
|
|
|
|
|
I am complete fresher at Java. Could You help me with https:
|
|
|
|
|
No, nobody here is going to help you cheat on a test.
If you can't answer the questions after taking the course, then go back and review the course again. If you still can't answer the questions, then find a better course.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
My solution:
public class Exercise {
double a = 234.89;
double b = 789.456;
double c = 567.89;
double d = 78.345;
double e = System.out.println(((a+b) * (c-d)) / (a/d));
double f = System.out.println(((a*b) + (c/d)) - (a-d));
if(e>f):
println(e);
else:
println(f);
}
|
|
|
|
|
Your solution to what? Can you describe your problem? and no, I'm not going to follow a link.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
public class Exercise {
double a = 234.89;
double b = 789.456;
double c = 567.89;
double d = 78.345;
double e = System.out.println(((a+b) * (c-d)) / (a/d));
double f = System.out.println(((a*b) + (c/d)) - (a-d));
if(e>f):
println(e);
else:
println(f);
}
|
|
|
|
|
And what happens when you run this code?
|
|
|
|
|
It's going to crash as he cannot assign the result of his method to a variable like his trying to do with 'double e' and 'double f'.
For him to assign the result to variables, he need to separate his calculation and printing steps.
|
|
|
|
|
It won't crash, since it will not even compile.
|
|
|
|
|
Hello everyone, I need to develop a Java software that takes a .PDF file as input and signs it in PAdeS format using a CNS or a SmartCard containing one's own PIN-protected certificate.
I've been trying to understand how to do this for several days, but I can't figure out the correct workflow to follow. I also don't understand how to interact with the SmartCard, it's not clear to me whether I need to extract the certificates to load them into my Java application, or if it's possible to proceed simply by using the reader driver to access the card. In both cases, I don't know where to start.
Do you have any advice or code snippets/classes to share? Thanks.
|
|
|
|
|
Relative Sorting
Ramesh is given two arrays, arr1 and arr2. He wants to sort arr1 in such a way that the relative order of arr2 is maintained in arr1. For elements that are not present in arr2, he wants to add the these elements at the end of the array in sorted fashion.
Can you help Ramesh achieve this task?
Input Format
First line contains two integers n and m denoting size of arrays arr1 and arr2 respectively.
Second line contains n space separated integers denoting the array arr1.
Third line contains m space separated integers denoting the array arr2.
Output Format
Print the relatively sorted arr1
Example 1
Input
11 4
2 1 2 5 7 1 9 3 6 8 8
2 1 8 3
Ramesh is given two arrays, arr1 and arr2. He wants to sort arr1 in such a way that the relative order of arr2 is maintained in arr1. For elements that are not present in arr2, he wants to add the these elements at the end of the array in sorted fashion.
Can you help Ramesh achieve this task?
Input Format
First line contains two integers n and m denoting size of arrays arr1 and arr2 respectively.
Second line contains n space separated integers denoting the array arr1.
Third line contains m space separated integers denoting the array arr2.
Output Format
Print the relatively sorted arr1
Example 1
Input
11 4
2 1 2 5 7 1 9 3 6 8 8
2 1 8 3
|
|
|
|
|
I can help Ramesh achieve these tasks, if Ramesh is willing to pay for someone to do Ramesh's homework for Ramesh. Alternatively, Ramesh could do some studying, and thinking, and do this all for himself.
|
|
|
|
|
|
You gave the input but you didnt gave the output ,,for us to undertand exactly what he wants.
What does he mean with 'relative order'?
2 1 2 5 7 1 9 3 6 8 8
will become
1,1,2,2,3,5,6,7,8,8,9
But i dont undertand what we want arr2 to become...so to help as i am resting in summer.
|
|
|
|
|
So,,, even if he mean that
2 must be first ,1 second ,8 3rd and 3 4th,,,
then lets see steps from input 2 1 2 5 7 1 9 3 6 8 8
arr2[2 1 8 3]
2 ->[2]
1->[2,1] because 1 is after 2 in arr2
2->[2,2,1] or [2,1] depends if he wants/accept same that also didnt clarified.
5->[2,2,1,5] and now arr2->[21835] but if this is the case what will happen if after 5 come 0?
will again start from beginning?..because say
" add the these elements at the end of the array in sorted fashion"
'Sorted fashion' here mean sorted only for
NEW numbers like ar2[21835]->ar2[218305] ???
|
|
|
|
|
Given an array arr of size N. Your task is to determine that if by reversing any one subarray can the given array be sorted or not.
You have to complete solve function which consists of arr array of size N as inputs and you have to return boolean answer as output.
Input Format
The first line of input contains a single integer N.
The second line of input contains N space seperated integers.
Output Format
Return true, if by reversing any one subarray sorted array can be formed as else false , "Yes" or "No" will be taken care by driver code.
|
|
|
|
|
|
Member 16037934 wrote: Given an array arr of size N. Your task is The key words being Your task, not someone else's
|
|
|
|