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);
}
}
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);
}
}