admin 管理员组文章数量: 1086019
Hi I am trying to add Tiles into tilepane which is added into scroll pane my aim is to have three columns of cards perfectly fit horizontally I don`t want to scroll, but I am not able to figure out how to give perfect width to those cards.
package com.sam.ui.tableManagment;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.TilePane;
public class TableTileCardsContainer extends ScrollPane {
ObservableList<String> tables;
TilePane tilePane;
TableTileCard tableTileCard;
public TableTileCardsContainer() {
tables = FXCollections.observableArrayList("1", "2", "3","1", "2", "3");
tilePane = new TilePane();
tilePane.setHgap(5);
tilePane.setVgap(5);
this.setFitToWidth(true);
tilePane.setOrientation(Orientation.HORIZONTAL);
for (String table : tables) {
tableTileCard = new TableTileCard(table);
tableTileCard.setPrefHeight(80);
tableTileCard.prefWidthProperty().bind(
tilePane.widthProperty().subtract(tilePane.getHgap()*2).divide(3)
);
tilePane.getChildren().add(tableTileCard);
}
this.setContent(tilePane);
this.getStylesheets().add("/css/ProductCardsContainer.css");
}
This is how i am trying to add
tableTileCard.prefWidthProperty().bind(
tilePane.widthProperty().subtract(tilePane.getHgap()*2).divide(3)
);
Hi I am trying to add Tiles into tilepane which is added into scroll pane my aim is to have three columns of cards perfectly fit horizontally I don`t want to scroll, but I am not able to figure out how to give perfect width to those cards.
package com.sam.ui.tableManagment;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.TilePane;
public class TableTileCardsContainer extends ScrollPane {
ObservableList<String> tables;
TilePane tilePane;
TableTileCard tableTileCard;
public TableTileCardsContainer() {
tables = FXCollections.observableArrayList("1", "2", "3","1", "2", "3");
tilePane = new TilePane();
tilePane.setHgap(5);
tilePane.setVgap(5);
this.setFitToWidth(true);
tilePane.setOrientation(Orientation.HORIZONTAL);
for (String table : tables) {
tableTileCard = new TableTileCard(table);
tableTileCard.setPrefHeight(80);
tableTileCard.prefWidthProperty().bind(
tilePane.widthProperty().subtract(tilePane.getHgap()*2).divide(3)
);
tilePane.getChildren().add(tableTileCard);
}
this.setContent(tilePane);
this.getStylesheets().add("/css/ProductCardsContainer.css");
}
This is how i am trying to add
tableTileCard.prefWidthProperty().bind(
tilePane.widthProperty().subtract(tilePane.getHgap()*2).divide(3)
);
Share
Improve this question
edited Mar 28 at 13:41
Sam
asked Mar 28 at 13:36
SamSam
337 bronze badges
4
|
1 Answer
Reset to default 2You can set the preferred number of columns for a tile pane, using setPrefColumns
. This will determine the preferred width of the tile pane (basically compute it based on the preferred number of columns, accounting for the size of the tiles and the horizontal gap between them).
If you want to prevent the tile pane growing beyond its preferred width (and thereby determine a maximum number of columns), you can use
tilePane.setMaxWidth(Region.USE_PREF_SIZE);
I think the following gives the effect you are looking for. I included a split pane to show some resizing behavior.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) {
TilePane tilePane = new TilePane();
tilePane.setPrefColumns(3);
tilePane.setHgap(5);
tilePane.setVgap(5);
tilePane.setMaxWidth(Region.USE_PREF_SIZE);
ScrollPane scrollPane = new ScrollPane(tilePane);
scrollPane.setFitToWidth(true);
for (int i = 0 ; i < 6; i++) {
tilePane.getChildren().add(createTile());
}
SplitPane splitPane = new SplitPane(scrollPane, new Pane());
Scene scene = new Scene(splitPane, 1000, 500);
stage.setScene(scene);
stage.show();
}
private Region createTile() {
Pane pane = new Pane();
pane.setStyle("""
-fx-background-color: lightblue, white;
-fx-background-insets: 0, 0 0 40 0;
-fx-min-height:200;
-fx-min-width:200;
""");
return pane;
}
public static void main(String[] args) {
launch();
}
}
Here are some screen shots with various sizes and vertical scrolling:
本文标签: javaHow to adjust tile pane and scroll pane width correctlyStack Overflow
版权声明:本文标题:java - How to adjust tile pane and scroll pane width correctly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744034026a2522025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
tilePane.setPrefColumns(3)
(and without the binding), though it depends on howTableTileCard
is defined. Can you create a minimal reproducible example? – James_D Commented Mar 28 at 15:18GridPane
might work better than aTilePane
. – James_D Commented Mar 28 at 15:32