1

Crossposted:

https://community.oracle.com/message/13853226#13853226

http://www.coderanch.com/t/666101/JavaFX/java/TextFlow-FXML#3105251

I am trying to use TextFlow coming from FXML in controller during app execution (not at start-up) but no text is shown.

I have tried:

textflow.getChildren.add(text);

and also:

textflow=new TextFlow(text);

where text is:

Text text=new Text("AAA");

I both cases TextFlow shows nothing.

Is there another container for use with rich text using FXML JavaFX app?

For sure if I try both cases in non-FXML JavaFX app it works both of them.

Update:

TextFlow in FXML looks like this

<TextFlow fx:id="txtFlow" layoutX="20.0" layoutY="230.0" prefHeight="70.0" prefWidth="430.0" style="-fx-border-color: ADD9E6;" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="20.0" AnchorPane.rightAnchor="140.0">

FXML

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.TextFlow?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="testfxmlpackage.FXMLDocumentController">
    <children>
        <TextFlow fx:id="txtF" layoutX="22.0" layoutY="234.0" prefHeight="74.0" prefWidth="433.0" style="-fx-border-color: ADD8E6;" AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="22.0" AnchorPane.rightAnchor="142.0" />               
    </children>
</AnchorPane>

Controller

package testfxmlpackage;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;

public class FXMLDocumentController implements Initializable {  

  @FXML TextFlow txtF;  

  @Override
  public void initialize(URL url, ResourceBundle rb) {
    txtF=new TextFlow(new Text("aaa"));
    txtF.getChildren().add(new Text("aaa"));
  }    
} 

Main Class

package testfxmlpackage;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class TestFXMLPackage extends Application {

  @Override
  public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }  
}
Yarl
  • 728
  • 1
  • 7
  • 26

1 Answers1

2

Make sure you refresh your project in the IDE after you edit the Text node in the FXML file, also make sure that fx:id matches your object name in the controller class.

Is there another container for use with rich text using FXML JavaFX app?

You don't need another container, just Text nodes within a TextFlow should work for you.

(it would be better if we can see your code, so that we can try to identify the exact problem)

Here is an example that works perfectly:

TextFlowExample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>

<VBox fx:id="container" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <TextFlow fx:id="myTextFlow" />
   </children>
   <padding>
      <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
   </padding>
</VBox>

MainApp.java

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class MainApp extends Application implements Initializable {

    @FXML TextFlow myTextFlow;
    @FXML VBox container;

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

    @Override
    public void start(Stage primaryStage) throws Exception {

        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("TextFlowExample.fxml"));
        loader.setController(this);
        Parent parent = loader.load();
        Scene scene = new Scene(parent);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        Text text = new Text("Now this is a text node");
        myTextFlow.getChildren().add(text);
    }
}
Khaled SAB
  • 216
  • 1
  • 5
  • I am not declaring text node in controller so in FXML is only TextFlow element declared. – Yarl May 26 '16 at 05:49
  • can we see your FXML file ? – Khaled SAB May 26 '16 at 07:10
  • I've edited my answer to include an example, just try to identify your problem, probably your FXML file is missing something. try to set an identifiable background color for your text flow to see if it is showing and if its maximum size isn't 0 – Khaled SAB May 26 '16 at 07:41
  • I have updated question by adding TextFlow from FXML. I have already bypassed this problem by replacing TextFlow with Label so now I am just wondering where is the problem. – Yarl May 26 '16 at 15:15
  • I've just examined you example. Problem is that I am not accessing TextFlow at start-up. I need to update it from controller according to app state. – Yarl May 27 '16 at 11:07
  • I have added example which is not working even if I am setting TextFlow content during initialization. – Yarl May 27 '16 at 11:45
  • 3
    In your controller class inside the initialize method you are loosing the reference to the old `txtF` object declared in your FXML by instantiating a new TextFlow object with the same name, and you are not adding this new object to your `AnchorPane`. So you'll need to remove this line `txtF=new TextFlow(new Text("aaa"));`. – Khaled SAB May 27 '16 at 17:11
  • @ Khaled SAB Fortunately I have made wrong example but in real case the problem was in fact the same but at instance variables declaration place. Thank you. – Yarl May 27 '16 at 18:39