简体   繁体   中英

Javafx load a new fxml file into a scrollpane

Is this possible to load a new fxml file into a scrollpane

for more detail view this image

Click here to view image

Please help me..

The following code shows how to use FXMLLoader to turn a String into some FXML objects. Then the usual GetChildren().Add(XX) can be used to assign to wherever you need.

Robert

package ic.ac.uk.relationshipvisualiser.app;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

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

public class tmpTest extends Application {

    public static void main(String[] args) {
        System.out.println("Start tmpTest");
        launch(args);
        System.out.println("Start tmpTest");
    }

    final Group m_root = new Group();

    @Override
    public void start(Stage primaryStage) throws Exception {
        String sample_fxml = 
                "<?import javafx.scene.control.Label?>" +
                "<?import javafx.scene.Group?>" +
                "<Group xmlns:fx=\"http://javafx.com/fxml\">" +
                "       <Label fx:id=\"Name\" style=\"-fx-font-weight: bold;\" alignment=\"CENTER\">It worked</Label>" +        
                "</Group>";

        InputStream stream = new ByteArrayInputStream(sample_fxml.getBytes("UTF-8"));
        FXMLLoader l = new FXMLLoader();

        Group mG = (Group) l.load(stream);

        m_root.getChildren().add(mG);

        primaryStage.setScene(new Scene(m_root));

        primaryStage.show();

    }

}

Further to my previous answer to show reading from a file: First create a file c:\\test.fxml containing:

<?import javafx.scene.control.Label?>
<?import javafx.scene.Group?>
<Group xmlns:fx="http://javafx.com/fxml">
<Label fx:id="Name" style="-fx-font-weight: bold;" alignment="CENTER">It worked</Label>     
</Group>

Next use the following code:

package ic.ac.uk.relationshipvisualiser.app;

import java.io.FileInputStream;
import java.io.InputStream;

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

public class tmpTest extends Application {

    public static void main(String[] args) {
        System.out.println("Start tmpTest");
        launch(args);
        System.out.println("Start tmpTest");
    }
    final Group m_root = new Group();

    @Override
    public void start(Stage primaryStage) throws Exception {
        InputStream stream = new FileInputStream("c:\\test.fxml");
        FXMLLoader l = new FXMLLoader();
        Group mG = (Group) l.load(stream);

        m_root.getChildren().add(mG);
        primaryStage.setScene(new Scene(m_root));
        primaryStage.show();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM