【问题标题】:Javafx grid pane..cannot add rows dynamically..without getting shrinkJavafx网格窗格..无法动态添加行..没有缩小
【发布时间】:2018-11-29 00:36:18
【问题描述】:

我想将 ui 节点动态添加到 gridapanes 行而不收缩..而不是收缩 gridpane 应该启用滚动(网格窗格在滚动窗格中)..但它们都没有发生......

我的所有尝试都是创建一个事件日历,能够将整个月的事件视为顶行中的天数(所以至少 30 个列)

--->控制器类 包装样品;

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

真的需要帮助

<?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>

【问题讨论】:

  • 您可能需要使用其他NodeListView,我猜。
  • 我应该使用列表视图做什么? Xan你解释一下..
  • 我只是在猜测。研究不同的Nodes 并确定哪个更适合您的情况。
  • 从你的图片来看,我猜是ListViewTableView
  • @Sedrick .我应该替换哪个节点...VBox insdie 网格单元???你能更具体..这样我就可以解决...谢谢你的帮助..

标签: java javafx dynamic fxml gridpanel


【解决方案1】:

解决了这个问题..而不是在场景构建器中创建网格平移..只需使用代码创建网格并通过代码设置约束...尝试在代码的执行顺序中尽早创建约束..

使用场景构建器创建的网格窗格对我来说不起作用

【讨论】:

    【解决方案2】:

    您的GridPane 行都有minHeight10.0。这意味着在使用 ScrollPane 之前,它们总是会缩小到该大小。

    将每行的minHeight 更改为-Infinity,这基本上使它与您的prefHeight 匹配:

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

    或者,为了在控制器中以编程方式设置这些约束,请在添加 VBox 后添加新的 RowConstraints

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

    【讨论】:

    • 没有帮助......更改 FXML 属性后程序甚至无法运行......提高 minHeight 并不能解决问题
    • 运行您的确切代码,在进行我建议的更改后,可以在此处正确运行。所有行都保持完全相同的高度,并且 Scene Builder 可以很好地打开文件。你确定你没有不小心改变了其他东西吗?
    • 没有做任何改变..除了你建议的那个..我会再试一次..谢谢你
    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多