简体   繁体   English

Javafx 网格窗格..无法动态添加行..没有收缩

[英]Javafx grid pane..cannot add rows dynamically..without getting shrink

I want to add ui nodes dynamically to a gridapanes row without shrinking..and instead of shrinking gridpane should enable scrolling (grid pane is in a scroll pane)..but neither of them is happening...我想将 ui 节点动态添加到 gridapanes 行而不收缩......而不是收缩 gridpane 应该启用滚动(网格窗格在滚动窗格中)......但它们都没有发生......

All I am attempting is to create a event calender with ability to view events of whole month as days in the top row (so at least 30 columns).我所尝试的只是创建一个事件日历,能够将整个月的事件查看为顶行中的天数(因此至少有 30 列)。

Controller class Controller class

package sample;

import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;


import java.net.URL;
import java.util.ResourceBundle;

public class Controller implements Initializable {
    public GridPane gridPane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        for (int i = 0; i <= 20; i++) {
            VBox box = new VBox();
            Label label = new Label(Integer.toString(i));
            label.setMinHeight(50);
            label.prefHeight(50);
            label.setMaxHeight(50);
            gridPane.setGridLinesVisible(true);
            label.setStyle("-fx-background-color:yellow;");
            box.getChildren().add(label);
            box.setAlignment(Pos.CENTER);
            gridPane.add(box, 0, i);
        }

        Label labe2 = new Label("HelloWorld");
        labe2.setMinHeight(80);
        gridPane.add(labe2, 0, 15);
    }
}

Really need help真的需要帮助

在此处输入图像描述

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

<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
            prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="sample.Controller">
    <children>
        <AnchorPane layoutX="39.0" layoutY="15.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0"
                    AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <children>
                <ScrollPane fitToHeight="true" fitToWidth="true" pannable="true" prefHeight="239.0" prefWidth="331.0"
                            AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
                            AnchorPane.topAnchor="0.0">
                    <content>
                        <GridPane fx:id="gridPane" prefHeight="239.0" prefWidth="331.0">
                            <columnConstraints>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                                <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/>
                            </columnConstraints>
                            <rowConstraints>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                                <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/>
                            </rowConstraints>
                        </GridPane>
                    </content>
                </ScrollPane>
            </children>
        </AnchorPane>
    </children>
</AnchorPane>

solved this issue..instead of creating the grid pan in scene builder..just create the grid using code and set constrains through code...try to create constrains early as possible in your execution order of the code..解决了这个问题......而不是在场景生成器中创建网格平移......只需使用代码创建网格并通过代码设置约束......尝试在代码的执行顺序中尽早创建约束......

didn't work out for me using scene builder created grid pane使用场景生成器创建的网格窗格对我没有效果

Your GridPane rows all have a minHeight of 10.0 .您的GridPane行的minHeight均为10.0 This means they will always shrink to that size before utilizing the ScrollPane .这意味着它们在使用ScrollPane之前总是会缩小到那个尺寸。

Change the minHeight for each row to -Infinity , which basically makes it match your prefHeight :将每行的minHeight更改为-Infinity ,这基本上使其与您的prefHeight匹配:

<RowConstraints minHeight="-Infinity" prefHeight="30.0" vgrow="SOMETIMES" />

Alternatively, in order to set these contraints programatically in your controller, add a new RowConstraints after adding the VBox :或者,为了在您的 controller 中以编程方式设置这些约束,请在添加VBox之后添加一个新的RowConstraints

gridPane.getRowConstraints().add(new RowConstraints(30));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM