首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX -在网格窗格中显示特定行和列的节点

JavaFX -在网格窗格中显示特定行和列的节点
EN

Stack Overflow用户
提问于 2022-10-04 01:25:43
回答 1查看 57关注 0票数 0

我用JavaFX创建了一个UI,但是布局有一些问题。基本上,我用一堆节点创建了一个网格窗格。下面是基本网格,其中的线条可见:

我是这样添加所有节点的:

代码语言:javascript
运行
复制
 lEncoderName.setMinHeight(0);
    fxEncoderName.setMinHeight(0);
    addRow(0, lEncoderName, fxEncoderName);
    getRowConstraints().add(new RowConstraints(ROW_HEIGHT));
    lNodeType.setMinHeight(0);
    fxNodeType.setMinHeight(0);
    addRow(1, lNodeType, fxNodeType);
    getRowConstraints().add(new RowConstraints(ROW_HEIGHT));

    // MIDI
    lMidiType.setMinHeight(0);
    fxMidiType.setMinHeight(0);
    addRow(getRowCount(), lMidiType, fxMidiType);
    getRowConstraints().add(new RowConstraints(ROW_HEIGHT));
    lMidiName.setMinHeight(0);
    fxMidiName.setMinHeight(0);
    addRow(getRowCount(), lMidiName, fxMidiName);
    getRowConstraints().add(new RowConstraints(ROW_HEIGHT));
    lDumpPos.setMinHeight(0);
    fxDumpPos.setMinHeight(0);
    addRow(getRowCount(), lDumpPos, fxDumpPos);
    getRowConstraints().add(new RowConstraints(ROW_HEIGHT));

等等..。我的问题是我想要在特定的行和colum索引上显示节点,但我不知道如何显示。例如,在这里,我在"Value Type“标签前添加了一组节点,这里有空格。根据选择的不同,节点显示。但是,如果它们没有显示,我希望将我的节点移动到网格中的不同索引。因此,“值节点”应该就在"Editbuffer位置“下面:

基本上,我在搜索一种方法,在新的行和列上显示节点,而不是按照我添加的顺序显示节点,而不破坏网格。

EN

回答 1

Stack Overflow用户

发布于 2022-10-04 03:24:09

首先,我假设您有需要隐藏的节点的引用。

解决此问题而不从GridPane中删除节点的一种方法是:

  • 关闭行
  • 中节点的可见性和托管属性,并重新配置网格窗格中所有节点的rowIndex。

下面是我提到的逻辑的快速演示。

代码语言:javascript
运行
复制
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class DisplayGridPaneNodesDemo extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        List<Node[]> rows = new ArrayList<>();

        ToggleGroup tg = new ToggleGroup();
        RadioButton allBtn = new RadioButton("All");
        allBtn.setToggleGroup(tg);
        allBtn.setSelected(true);
        allBtn.setOnAction(e->displayRows(rows,i->true));

        RadioButton evenBtn = new RadioButton("Even");
        evenBtn.setToggleGroup(tg);
        evenBtn.setOnAction(e->displayRows(rows,i->i%2==0));

        RadioButton oddBtn = new RadioButton("Odd");
        oddBtn.setToggleGroup(tg);
        oddBtn.setOnAction(e->displayRows(rows,i->i%2!=0));

        HBox row = new HBox(allBtn, evenBtn,oddBtn);
        row.setSpacing(10);

        GridPane gridPane = new GridPane();
        gridPane.setGridLinesVisible(true);
        gridPane.setVgap(5);
        for(int i=0;i<10;i++){
            Label c1 = new Label("Row "+i+" Col 0");
            c1.setPadding(new Insets(10));
            Label c2 = new Label("Row "+i+" Col 1");
            c2.setPadding(new Insets(10));
            gridPane.addRow(i,c1,c2);
            Node[] nodes = {c1,c2};
            rows.add(nodes);
        }

        VBox root = new VBox(row, gridPane);
        root.setSpacing(10);
        root.setPadding(new Insets(10));
        Scene scene = new Scene(root, 320,550);
        stage.setScene(scene);
        stage.setTitle("Display Nodes in GridPane");
        stage.show();
    }

    private void displayRows(List<Node[]> rows, Predicate<Integer> isVisible) {
        // Start rowIndex from 0
        IntegerProperty rowIndex = new SimpleIntegerProperty();

        // Loop through all the rows
        for(int i = 0; i<rows.size(); i++){
            // Check if the row needs to be visible or not
            boolean visible = isVisible.test(i);
            Node[] nodes = rows.get(i);

            // Loop through all the nodes in the row and set the visibility and managed
            Stream.of(nodes).forEach(n->{
                n.setVisible(visible);
                n.setManaged(visible);
                // If the row needs to be visible , set the rowIndex to the nodes in the row.
                // YOU DON'T NEED TO WORRY ABOUT THE 'rowIndex' OF INVISIBLE NODES ;-)
                if(visible) {
                    GridPane.setRowIndex(n, rowIndex.get());
                }
            });

            // Increment the rowIndex if we show the current row.
            if(visible){
                rowIndex.set(rowIndex.get()+1);
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73942288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档