-1

I know how to display text from thread in console, but how can I put it in TextArea? Also since my text is chars and numbers, how can I convert them to string since TextArea takes only String if I am right?

I need to append PrintNum, LetterSmall and LetterBig in TextArea. Does someone know the way? Any help would be appreciated!

public class pracice extends Application {


@Override
public void start(Stage primaryStage) {

    TextArea ta = new TextArea();

    Button btn = new Button();
    btn.setText("Show");
    btn.setOnAction(new EventHandler<ActionEvent>() {


        Runnable run = new PrintNum(25);
        Thread th1 = new Thread(run);

        char lett;
        char lettUp;

        Runnable let = new LetterSmall(lett);
        Thread th2 = new Thread(let);

        Runnable lUp = new LetterBig(lettUp);
        Thread th3 = new Thread(lUp);

        @Override
        public void handle(ActionEvent event) {
            System.out.append("\nBegin\n");

            th1.start();
            try{
                th1.join(2000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }

            th2.start();
            try{
                th2.join();
            } catch (InterruptedException e){
                e.printStackTrace();
            }

            th3.start();
        }
    });


    BorderPane root = new BorderPane();
    root.setTop(btn);
    root.setCenter(ta);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Practice");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {

    launch(args);
}

}

class PrintNum implements Runnable {

private int lastNum;
Random r = new Random();

public PrintNum(int n){
    lastNum = n;
}

public void run(){
    System.out.append("\n");
    for(int i = 1; i <= lastNum; i++){
        int rN = r.nextInt(25) + 1;
        System.out.append((lastNum - rN) + " ");
    }
}

}

class LetterSmall implements Runnable {

Random r = new Random();

private char lett;

public LetterSmall(char s){
    lett = s;
}

public void run(){
   System.out.append("\n");
    for(int i = 1; i <= 25; i++){
        char c = (char) (r.nextInt(26) + 'a');
        lett = c;
        System.out.append(lett + " ");
    }
}

}

class LetterBig implements Runnable {

Random r = new Random();

private char lettUp;

public LetterBig(char up){
    lettUp = up;
}

public void run(){
    System.out.append("\n");
    for(int i = 1; i <= 25; i++){
        char c = (char) (r.nextInt(26) + 'A');
        lettUp = c;
        System.out.append(lettUp + " ");
    }
}

}

Beansolder
  • 125
  • 1
  • 12
  • 1
    You've got a lot of questions here. I'll answer one: use `TextA.appendText(String)` to display text in TextArea – Ivan Pronin Jul 22 '17 at 19:48
  • Hmm I get the way you can append it usually. But how can I turn this char into a string? Because I cant append char in TA. @IvanPronin – Beansolder Jul 22 '17 at 20:00
  • 2
    I will give you a hint so you find the hundreds of answers about this subject. The UI can only be modified from the UI thread – bichito Jul 22 '17 at 20:08
  • 2
    To display text from a `Thread` do `Platform.runLater(TextA.appendText("someString")` – SedJ601 Jul 22 '17 at 22:30
  • 1
    Possible Duplicate: [Updating UI from a different thread](https://stackoverflow.com/questions/22772379/updating-ui-from-different-threads-in-javafx). – SedJ601 Jul 22 '17 at 22:31
  • Why are you using threads at all in this example? All of this code runs in a few milliseconds, at most, and your event handler waits for each of the threads, in turn, to complete. You may as well just execute the code in the same thread as the event handler. – James_D Jul 23 '17 at 12:51

1 Answers1

-2

So I found a solution for TextArea input. Instead of making three class threads, I moved them inside the primary JavaFX class. Thanks everyone for help.

It looks like this:

new Thread(new Runnable() {
        private int lastNum = 25;
        private char lett;
        private char lettUp;

        Random r = new Random();


        @Override
        public void run() {

            Thread th1 = new Thread();
            ta.appendText("\n");
            for(int i = 1; i <= lastNum; i++){
                int rN = r.nextInt(25) + 1;
                ta.appendText((lastNum - rN) + " ");
            }

            Thread th2 = new Thread();
            ta.appendText("\n\n");
            for(int i = 1; i <= 25; i++){
                char c = (char) (r.nextInt(26) + 'a');
                lett = c;
                ta.appendText(lett + " ");
            }

            Thread th3 = new Thread();
            ta.appendText("\n\n");
            for(int i = 1; i <= 25; i++){
                char c = (char) (r.nextInt(26) + 'A');
                lettUp = c;
                ta.appendText(lettUp + " ");
            }

            th1.start();
            try{
                th1.join(2000);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            th2.start();
            try{
                th1.join();
            } catch (InterruptedException e){
                e.printStackTrace();
            }
            th3.start();
            try{
                th1.join();
                th2.join();
                th3.join();
            } catch (InterruptedException e){
                e.printStackTrace();
            }

        }
    }).start();
Beansolder
  • 125
  • 1
  • 12
  • 1
    This code is flawed. You are modifying a UI element from a background thread. While this may happen to work on your particular set up, it violates the JavaFX threading rules, so there is absolutely no guarantee it will work anywhere else. – James_D Jul 23 '17 at 02:28
  • I am not saying its the best or good but it works for me. Can you change the code and add it as answer? I will gladly change right answer for this case to your implementation/solution. :) @James_D – Beansolder Jul 23 '17 at 03:04
  • 1
    I don't understand what the code in this answer is trying to do (what are the three threads without a `Runnable` for?). However it is incorrect to call, for example, `ta.appendText(...)` from a background thread, and you should not post this code and claim it answers the question, as other users may copy it. No-one really cares that it happens to work on your particular set-up: you are violating the threading rules of JavaFX, which are very [well-documented](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html). Do not use this site to spread misinformation. – James_D Jul 23 '17 at 12:26
  • Maybe look at my answer to [this question](https://stackoverflow.com/questions/45203267/updating-an-imageview-from-another-thread-javafx/45203400#45203400). The question is asking something different, but the answer has examples of what I think you might be trying to do. – James_D Jul 23 '17 at 12:38