Forum Stats

  • 3,875,109 Users
  • 2,266,806 Discussions
  • 7,912,085 Comments

Discussions

How to use TextFlow from FXML?

Uzivatel919
Uzivatel919 Member Posts: 19
edited May 27, 2016 2:41PM in JavaFX 2.0 and Later

Crossposted:

http://stackoverflow.com/questions/37436595/how-to-use-textflow-from-fxml

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

I am trying to use TextFlow coming from FXML 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.

Best Answer

  • jsmith
    jsmith Member Posts: 2,856
    edited May 27, 2016 1:57PM Answer ✓

    User Khaled SAB solved your problem on StackOverflow:  http://stackoverflow.com/a/37437608/1155209

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

Answers

  • jsmith
    jsmith Member Posts: 2,856
    edited May 25, 2016 12:59PM

    If you would like assistance with this, you should provide an mcve.

  • Uzivatel919
    Uzivatel919 Member Posts: 19
    edited May 27, 2016 7:07AM

    Maybe but I could not agree.

  • Uzivatel919
    Uzivatel919 Member Posts: 19
    edited May 27, 2016 7:46AM

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

  • jsmith
    jsmith Member Posts: 2,856
    edited May 27, 2016 1:57PM Answer ✓

    User Khaled SAB solved your problem on StackOverflow:  http://stackoverflow.com/a/37437608/1155209

    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"));
    
  • Uzivatel919
    Uzivatel919 Member Posts: 19
    edited May 27, 2016 2:41PM
This discussion has been closed.