user3287267
user3287267

Reputation: 1

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?

ScrollPane.setContent(Pane);

Upvotes: 0

Views: 7803

Answers (1)

Kent Shikama
Kent Shikama

Reputation: 4070

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");
    stage.setScene(scene);
    stage.show();
}

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="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="REFERENCE_TO_CONTROLLER">
   <children>
      <ScrollPane prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <content>
            <Pane prefHeight="382.0" prefWidth="386.0"> <!-- Add FXML id to your pane here -->
               <children>
                  <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" />
               </children>
            </Pane>
         </content>
      </ScrollPane>
   </children>
</AnchorPane>

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

Upvotes: 1

Related Questions