JavaFX: Scroll a Pane with dynamic information(label, textfield, button)


How can I make that a Pane ( with elements inside) be Scrollable ?

In FXML file, i've put inside a ScrollPane, the Pane I want that it be Scrollable. It's Correct? or i'm wrong?


5/14/2014 5:09:05 PM

Yes, that is correct. Once you create the FXML file you simply need to display it as such:

public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("NAME_OF_FXML.fxml"));
    Scene scene = new Scene(root);
    stage.setTitle("ScrollPane Example");

The FXML file should look like the follows:

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

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

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="" xmlns="" fx:controller="REFERENCE_TO_CONTROLLER">
      <ScrollPane prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <Pane prefHeight="382.0" prefWidth="386.0"> <!-- Add FXML id to your pane here -->
                  <Label layoutX="14.0" layoutY="14.0" text="Element 1" />
                  <Label layoutX="14.0" layoutY="170.0" text="Element 2" />
                  <Label layoutX="14.0" layoutY="352.0" text="Element 3" />

Note that your pane must be larger than the scroll pane in order for the scrollbars to appear. The result should look like the following:

Scrollpane screenshot

If you want to dynamically add information to your pane, you can give the Pane an FXML id in your FXML file and then get a reference to it using your controller class. The controller class can be obtained from your FXML resource as follows:

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("NAME_OF_FXML.fxml"));
screenController = myLoader.getController();
5/17/2014 6:14:12 AM

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