Ceeya
Ceeya

Reputation: 65

Put a Scrollbar into to Pane JavaFX

I'm just working on a little project and started to get in JavaFX. I have a problem and I don't know any solutions..

So I managed it to bring a Scrollbar(incl Listener) into the root. But the listener doesn't work.

I want to add many comboboxes one below the other and when I reached the scenesize(LayoutX) it should be possible to scrolldown.

How can I solve my problem?

package application;

import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.*;
import javafx.stage.Stage;
import application.XMLActions;;

public class Main extends Application {

    /**
     * Globale Variablen
     */
     int abstandszaehler = 0;
     private Pane root = new Pane();        
     private ScrollBar sB = new ScrollBar();
     private XMLActions xA = new XMLActions();




    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("XML-Zeilenersteller");
        primaryStage.setResizable(false);


        /**
         * Elemente für die Root
         */
        //Buttons
        Button newXMLLine = new Button();
        newXMLLine.setText("Einfügen neuer XML-Zeile");
        newXMLLine.setLayoutX(735);
        newXMLLine.setLayoutY(80);
        newXMLLine.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                setComboBox();
            }
        });

        Button newXMLDocument = new Button();
        newXMLDocument.setText("Erstelle XML-Dokument");
        newXMLDocument.setLayoutX(735);
        newXMLDocument.setLayoutY(550);
        newXMLDocument.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("Test2");
            }
        });

        //Überschrift
        Text header = new Text(105, 50, "XML Lines Creator");
        header.setFont(new Font(30)); header.setStyle("-fx-underline: true;");

        //Scrollbar
        sB.setLayoutX(715);
        sB.setLayoutY(80);
        sB.setMin(0);
        sB.setOrientation(Orientation.VERTICAL);
        sB.setPrefHeight(500);
        sB.setMax(360);
        sB.setUnitIncrement(30);
        sB.setBlockIncrement(35);
        sB.valueProperty().addListener((ObservableValue<? extends Number> ov, 
                Number old_val, Number new_val) -> {
                    System.out.println(-new_val.doubleValue());
            });          

        /**
         *  Hauptseite als Root
         * Rootbearbeitungen
         */
        root.setStyle("-fx-background-color: lightsteelblue");                                                      
        root.getChildren().addAll(sB,newXMLDocument,header,newXMLLine );


        //Scene setzen
        Scene mainScene = new Scene(root, 900, 600);
        primaryStage.setScene(mainScene);
        primaryStage.show();


    }


    public void setComboBox(){
        ComboBox cB = new ComboBox(xA.getList());   
        root.getChildren().add(cB);
        cB.setLayoutX(80);
        cB.setLayoutY(80 + abstandszaehler);
        abstandszaehler = abstandszaehler + 30;

    }        
}

EDIT 1: I got a little progress with that code in the listener:

root.setLayoutY(-new_val.doubleValue());

Upvotes: 0

Views: 4178

Answers (2)

Itai
Itai

Reputation: 6911

First, if you wish to layout controls vertically consider using VBox. This VBox should then be enclosed by a ScrollPane.

If you then set the VBox's prefHeight to Control.USE_COMPUTED_SIZE and maxHeight to Double.POSITIVE_INFINITY the VBox should resize to fit it's content without limit, and the enclosing ScrollPane should show and hide scrollbars as necessary.

Upvotes: 1

hotzst
hotzst

Reputation: 7496

Replace the Pane with a ScrollPane. On the ScrollPane you can define the policy for your scrollbar.

If you define it like this, it will behave like a Pane:

ScrollPane sp = new ScrollPane();
sp.setHbarPolicy(ScrollBarPolicy.NEVER);
sp.setVbarPolicy(ScrollBarPolicy.NEVER);

Take a look at this article by Oracle.

Upvotes: 1

Related Questions