我无法在JavaFx中实用地自定义我的GridPane

时间:2016-09-27 10:18:08

标签: user-interface javafx gridpane

我正在做一个连接到documentum数据存储的JavaFx项目。我需要从数据存储中检索一些数据,以我使用GridPanes的表格形式插入和组织它们。由于数据因对象而异,我必须使代码动态化并使用程序化GUI设计。好吧,数据获取和上传到GridPane没有问题。但是,我在设计GridPan的界面时遇到了一些问题。我在我的FXML文件中创建了一个名为“aclInfo”的GridPane,我正在尝试在其中创建我的新GridPane。以下是我写的代码:

public void test ( ){
    aclInfo.getChildren().clear();

try{


    for (int i = 0 ; i <= listOfAcls.size() - 1 ; i ++ ){

        GridPane mainGridPane = new GridPane();

        mainGridPane.setStyle("-fx-grid-lines-visible: true");
        //mainGridPane.setStyle("-fx-background-color :red");


        RowConstraints row0 = new RowConstraints();
        RowConstraints row1 = new RowConstraints();
        row0.setMinHeight(20);
        row1.setMinHeight(32);

        mainGridPane.getRowConstraints().addAll(row0 , row1);


        ColumnConstraints mianCol0 = new ColumnConstraints();



        mianCol0.setMinWidth(911);


        mainGridPane.getColumnConstraints().addAll(mianCol0 );


        GridPane title = new GridPane ();
        ColumnConstraints col0 = new ColumnConstraints();
        ColumnConstraints col1 = new ColumnConstraints();
        ColumnConstraints col2 = new ColumnConstraints();
        col0.setMinWidth(303.66);
        col1.setMinWidth(303.66);
        col2.setMinWidth(303.66);
        title.getColumnConstraints().addAll(col0 , col1 , col2);


        //title.setStyle("-fx-grid-lines-visible: true");

        GridPane desc = new GridPane();
        RowConstraints descrow0 = new RowConstraints();
        RowConstraints descrow1 = new RowConstraints();
        descrow0.setMinHeight(20);
        descrow0.setMinHeight(22);

        desc.getRowConstraints().addAll(descrow0 , descrow1);


        ColumnConstraints desccol0 = new ColumnConstraints();
        ColumnConstraints desccol1 = new ColumnConstraints();
        ColumnConstraints desccol2 = new ColumnConstraints();
        desccol0.setMinWidth(303.66);
        desccol1.setMinWidth(303.66);
        desccol2.setMinWidth(303.66);

        desc.getColumnConstraints().addAll( desccol0, desccol1,desccol2);
        //desc.setStyle("-fx-background-color :red");
        desc.setStyle("-fx-grid-lines-visible: true");


        Text aclName = new Text(listOfAcls.get(0));
        Text roid = new Text ("r_object_id");
        Text rap = new Text ("r_accessor_permit");
        Text ran = new Text ("r_accessor_name");
        Text roidValue = new Text (holding.get(i).r_object_id);
        Text rapValue = new Text (holding.get(i).r_accessor_permit);
        Text ranValue = new Text (holding.get(i).r_accessor_name);





        title.add(aclName, 1, 0);
        title.setHalignment(aclName, HPos.CENTER);
        desc.add(roid , 0 , 0 );
        desc.setHalignment(roid, HPos.CENTER);
        desc.add(ran , 1 , 0 );
        desc.add(rap , 2 , 0 );
        desc.add(roidValue, 0 , 1);
        desc.add(rapValue, 1 , 1);
        desc.add(ranValue, 2 , 1);

        mainGridPane.add(title, 0, 0);
        mainGridPane.add(desc, 0, 1);
        aclInfo.add(mainGridPane, 0 , i);


    }




}catch(Exception e){
    Alert alert = new Alert (Alert.AlertType.ERROR , e.getLocalizedMessage());
    alert.showAndWait();
}

}
}

我无法将我的文本集中在GridPane中。

知道我哪里弄错了:)

2 个答案:

答案 0 :(得分:1)

您还可以为所有列添加对齐:

ColumnConstraints desccol0 = new ColumnConstraints();
desccol0.setHalignment(HPos.CENTER);
ColumnConstraints desccol1 = new ColumnConstraints();
desccol0.setHalignment(HPos.CENTER);
ColumnConstraints desccol2 = new ColumnConstraints();
desccol0.setHalignment(HPos.CENTER);

或行

 RowConstraints descrow1 = new RowConstraints();
 descrow1.setValignment(VPos.CENTER);

你的代码少了

答案 1 :(得分:0)

嗯,这里说明最终工作代码的样子:

   public void test ( ){
    aclInfo.getChildren().clear();

try{


    for (int i = 0 ; i <= listOfAcls.size() - 1 ; i ++ ){

        GridPane mainGridPane = new GridPane();

        mainGridPane.setStyle("-fx-grid-lines-visible: true");
        //mainGridPane.setStyle("-fx-background-color :red");


        RowConstraints row0 = new RowConstraints();
        RowConstraints row1 = new RowConstraints();
        row0.setMinHeight(20);
        row1.setMinHeight(32);

        mainGridPane.getRowConstraints().addAll(row0 , row1);


        ColumnConstraints mianCol0 = new ColumnConstraints();



        mianCol0.setMinWidth(911);


        mainGridPane.getColumnConstraints().addAll(mianCol0 );


        GridPane title = new GridPane ();
        ColumnConstraints col0 = new ColumnConstraints();
        ColumnConstraints col1 = new ColumnConstraints();
        ColumnConstraints col2 = new ColumnConstraints();
        col0.setMinWidth(303.66);
        col1.setMinWidth(303.66);
        col2.setMinWidth(303.66);
        title.getColumnConstraints().addAll(col0 , col1 , col2);



        GridPane desc = new GridPane();
        RowConstraints descrow0 = new RowConstraints();
        RowConstraints descrow1 = new RowConstraints();
        descrow0.setMinHeight(20);
        descrow0.setMinHeight(22);

        desc.getRowConstraints().addAll(descrow0 , descrow1);


        ColumnConstraints desccol0 = new ColumnConstraints();
        ColumnConstraints desccol1 = new ColumnConstraints();
        ColumnConstraints desccol2 = new ColumnConstraints();
        desccol0.setMinWidth(303.66);
        desccol1.setMinWidth(303.66);
        desccol2.setMinWidth(303.66);

        desc.getColumnConstraints().addAll( desccol0, desccol1,desccol2);

        desc.setStyle("-fx-grid-lines-visible: true");


        Text aclName = new Text(listOfAcls.get(0));
        Text roid = new Text ("r_object_id");
        Text rap = new Text ("r_accessor_permit");
        Text ran = new Text ("r_accessor_name");
        Text roidValue = new Text (holding.get(i).r_object_id);
        Text rapValue = new Text (holding.get(i).r_accessor_permit);
        Text ranValue = new Text (holding.get(i).r_accessor_name);





        title.add(aclName, 1, 0);

        desc.add(roid , 0 , 0 );
        title.setHalignment(aclName, HPos.CENTER);
        desc.setHalignment(roid, HPos.CENTER);
        desc.setHalignment(ran, HPos.CENTER );
        desc.setHalignment(rap, HPos.CENTER );

        desc.setHalignment(roidValue, HPos.CENTER);
        desc.setHalignment(ranValue, HPos.CENTER );
        desc.setHalignment(rapValue, HPos.CENTER );


        desc.add(ran , 1 , 0 );
        desc.add(rap , 2 , 0 );
        desc.add(roidValue, 0 , 1);
        desc.add(rapValue, 1 , 1);
        desc.add(ranValue, 2 , 1);

        mainGridPane.add(title, 0, 0);
        mainGridPane.add(desc, 0, 1);
        aclInfo.add(mainGridPane, 0 , i);


    }




}catch(Exception e){
    Alert alert = new Alert (Alert.AlertType.ERROR , e.getLocalizedMessage());
    alert.showAndWait();
}

}
}