JavaFX System out Println


Question

Im doing an interface to show the progress of my simulation (elevators simulating job on a building). The thing is, i already did this on console with SystemOutPrintlns and i wanted to show the exact same thing on a javaFX window, is there anyway where i can set the text of a TextAre or a label or something to match the output of the console? just printing the same thing but instead of printing on the console i wanted to print on a window. I was dumb enough to try and set the Text of a textArea to the toString of my simulator but ofc if it is System Out Prinln it shows on the console and not in the text Area.

EDIT: this is what i want to print

@Override public String toString() {

    for (int y = 0; y < 50; y++)
    {
        System.out.println("");
    }
      for (int i = pisos.size() - 1; i >=  0; i--)
    {

        System.out.print(pisos.get(i).getPiso() );
        System.out.print(pisos.get(i).pQueue().toString());
        System.out.print(" " + percorrerElevadores2(i));
        System.out.print(" " + pisos.get(i).pessoasServidas() + "\n");    
    }

      System.out.println("Numero total de passageiros à espera:" + " " + Predio.getPredio().getNPessoasEmEspera());
      System.out.println("Numero total de pessageiros servidos:" + " " + Predio.getPredio().getNPessoasServidas());
      for (int z = 0; z < getElevadores().size(); z++)
      {
      System.out.println("Distancia percorrida pelo elevador" + " " + z + ":" + " " + Predio.getPredio().getElevadores().get(z).getDistanciaPercorrida() + " " + "Pisos");
      System.out.println("Piso destino do elevador" + " " + z + ":" + " " + Predio.getPredio().getElevadores().get(z).getPisoDestino());
      }

    return "";
}

}

1
0
1/18/2014 4:36:10 PM

From what I understand, you just want to print text in a location to investigate things, ie the results. Everything you want is just like you print a text on the console, you also want to print this text somewhere in a JavaFX application. Despite its formatting done in their toString method, you can catch the return of the method and print to a JavaFX application (within a control node, for example), right?

If this is the case ...

I created a simple application that works with two text areas. In the middle of application, you will find buttons that manipulate both areas. Basically, the buttons sends the contents of a text area to another. Note:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class AreaTextual extends Application 
{
// #########################################################################################################
//                                                                                                      MAIN
// #########################################################################################################

public static void main(String[] args) 
{
    Application.launch(args);
}

// #########################################################################################################
//                                                                                                INSTÂNCIAS
// #########################################################################################################

// Controles.

private Label lab_receptor;
private Label lab_emissor;

private TextArea tArea_receptor;
private TextArea tArea_emissor;

private Button bot_enviar;
private Button bot_enviarLinha;
private Button bot_substituir;
private Button bot_apagar;

// Layout.

private HBox hbox_raiz;

private VBox vbox_oeste;
private VBox vbox_centro;
private VBox vbox_leste;

// #########################################################################################################
//                                                                                                 INÍCIO FX
// #########################################################################################################

@Override public void start(Stage estagio) throws Exception 
{
    this.iniFX();
    this.confFX();
    this.adFX();
    this.evFX();

    Scene cenario = new Scene(this.hbox_raiz , 640 , 480);

    estagio.setScene(cenario);
    estagio.setTitle("Programa JavaFX");
    estagio.show();
}

/** Inicia nós FX.*/
protected void iniFX()
{
    // Controles.

    this.lab_receptor = new Label();
    this.lab_emissor = new Label();

    this.tArea_receptor = new TextArea();
    this.tArea_emissor = new TextArea();

    this.bot_enviar = new Button();
    this.bot_enviarLinha = new Button();
    this.bot_substituir = new Button();
    this.bot_apagar = new Button();

    // Layout.

    this.hbox_raiz = new HBox();

    this.vbox_oeste = new VBox();
    this.vbox_centro = new VBox();
    this.vbox_leste = new VBox();
}

/** Configura nós FX.*/
protected void confFX()
{
    // Controles.

    this.lab_receptor.setText("RECEPTOR");
    this.lab_receptor.setFont(new Font(32));

    this.lab_emissor.setText("EMISSOR");
    this.lab_emissor.setFont(new Font(32));

    this.bot_enviar.setText("<- ENVIAR");
    this.bot_enviar.setPrefSize(150 , 60);

    this.bot_enviarLinha.setText("<- ENVIAR+");
    this.bot_enviarLinha.setPrefSize(150 , 60);

    this.bot_substituir.setText("<- SUBSTITUIR");
    this.bot_substituir.setPrefSize(150 , 60);

    this.bot_apagar.setText("<- APAGAR TUDO ->");
    this.bot_apagar.setPrefSize(150 , 60);

    // Layout.

    this.hbox_raiz.setSpacing(20);
    this.hbox_raiz.setPadding(new Insets(30 , 30 , 30 , 30));
    this.hbox_raiz.setAlignment(Pos.CENTER);

    this.vbox_oeste.setSpacing(10);
    this.vbox_oeste.setAlignment(Pos.CENTER);

    this.vbox_centro.setSpacing(10);
    this.vbox_centro.setAlignment(Pos.CENTER);
    this.vbox_centro.setPrefSize(400 , 200);

    this.vbox_leste.setSpacing(10);
    this.vbox_leste.setAlignment(Pos.CENTER);
}

/** Adiciona e organiza em layout os nós FX.*/
protected void adFX()
{
    this.vbox_leste.getChildren().addAll(this.lab_emissor , this.tArea_emissor);
    this.vbox_centro.getChildren().addAll(this.bot_enviar , this.bot_enviarLinha , this.bot_substituir , this.bot_apagar);
    this.vbox_oeste.getChildren().addAll(this.lab_receptor , this.tArea_receptor);

    this.hbox_raiz.getChildren().addAll(this.vbox_oeste , this.vbox_centro , this.vbox_leste);
}

/** Configura eventos de nós FX.*/
protected void evFX()
{
    this.bot_enviar.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override public void handle(ActionEvent e)
        {
            tArea_receptor.appendText(tArea_emissor.getText());
        }
    });

    this.bot_enviarLinha.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override public void handle(ActionEvent e)
        {
            tArea_receptor.appendText(String.format("%n%s" , tArea_emissor.getText()));
        }
    });

    this.bot_substituir.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override public void handle(ActionEvent e)
        {
            tArea_receptor.replaceText(0 , tArea_receptor.getLength() , tArea_emissor.getText());
        }
    });

    this.bot_apagar.setOnAction(new EventHandler<ActionEvent>()
    {
        @Override public void handle(ActionEvent e)
        {
            tArea_receptor.setText("");
            tArea_emissor.setText("");
        }
    });
}
}

OBS (PT-BR): Eu notei que você fala português, portanto deixei o código na linguagem para que você entenda-o melhor.

This class has nothing exceptional. It just shows you how you can manipulate the text of a TextArea. You can find other types of handlers of a TextArea right here, and also here.

Regarding your problem seen where you call the JavaFX methods from another thread, this can be happening just because you're not using the JavaFX Application Thread. Like the Swing library has the Event Dispatch Thread (EDT), JavaFX also has its own thread responsible for handling the JavaFX elements. Whenever you need to manipulate any JavaFX element, be to setup something or to obtain some data, you need to do this using the JavaFX Application Thread, and not another.

For you to call methods of the JavaFX Application Thread, use the Platform runLater method. For more information about the JavaFX threads system, visit the following links:

http://docs.oracle.com/javafx/2/architecture/jfxpub-architecture.htm

http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm

In the first link, read the part that talks about threads. If you have any more questions, come back here and ask.

Good luck.

0
1/19/2014 4:35:47 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon