1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package scrollbar;
import java.util.Optional;
import java.util.stream.Stream;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public final class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
final ScrollBar scroll1 = new ScrollBar();
final ScrollBar scroll2 = new ScrollBar();
scroll2.getStyleClass().add("my-scroll-bar");
final ScrollBar scroll3 = new ScrollBar();
scroll3.setOrientation(Orientation.VERTICAL);
final ScrollBar scroll4 = new ScrollBar();
scroll4.setOrientation(Orientation.VERTICAL);
scroll4.getStyleClass().add("my-scroll-bar");
final TilePane root = new TilePane(scroll1, scroll2, scroll3, scroll4);
root.setPrefRows(2);
root.setPrefColumns(2);
root.setHgap(3);
root.setVgap(3);
root.prefTileWidthProperty().bind(Bindings.subtract(Bindings.divide(root.widthProperty(), root.prefRowsProperty()), root.hgapProperty()));
root.prefTileHeightProperty().bind(Bindings.subtract(Bindings.divide(root.heightProperty(), root.prefColumnsProperty()), root.vgapProperty()));
Stream.of(scroll1, scroll2, scroll3, scroll4)
.map(scrollBar -> {
final StackPane result = new StackPane(scrollBar);
result.setPrefSize(Region.USE_COMPUTED_SIZE, Region.USE_COMPUTED_SIZE);
result.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
return result;
})
.forEach(root.getChildren()::add);
final Scene scene = new Scene(root);
Optional.ofNullable(getClass().getResource("test.css"))
.ifPresent(url -> scene.getStylesheets().add(url.toExternalForm()));
primaryStage.setTitle("Test");
primaryStage.setScene(scene);
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
} |