简体   繁体   English

Javafx网格窗格中心元素

[英]Javafx Grid pane Center elements

I m working on a javafx application . 我正在使用javafx应用程序。 a part of it consists on rendering a List into the Gui using a scrollpane of a grid element . 它的一部分包括使用网格元素的滚动面板将列表渲染到Gui中。 im getting the values i need correctly but i can't figure out how to center each gridpane cell. 即时获取我需要的值,但我不知道如何居中每个网格窗格单元格。

// clear existing content if it exists  
    if(content.getChildren()!=null)
    {
        content.getChildren().clear();
    }
     // get Elements to display 
    OfferService os = new OfferService();
    List<Offer> myList = os.afficheroffre();
    UserService us = new UserService();

    GridPane Container = new GridPane();  // main container for all data specific to an offer
    Container.setAlignment(Pos.CENTER);

    // Scroll pane to display all the found contact requests
    ScrollPane scrollPane = new ScrollPane(Container);
    scrollPane.setPrefSize(900, 630);
    scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

    AnchorPane.setTopAnchor(scrollPane, 0.);
    Container.setPrefWidth(900);
    Container.setPrefHeight(630);

    content.setRightAnchor(scrollPane, 0.);
    content.setBottomAnchor(scrollPane, 0.);
    content.setLeftAnchor(scrollPane, 0.);

    Container.setPadding(new Insets(30,0,0,30));

    // iterate through all offers and create an offer element
    int i = 0;
    int j = 0;
    for (Offer o : myList)
    {
       final User u = us.getById(o.getIdOfferuser());
        //HBox : single with spacing
        HBox Hb = new HBox();
        //VBox : single
        VBox Vb = new VBox();

        ImageView img = new ImageView(new Image("/Uploads/" + o.getImageOffer()));

        Label name = new Label(o.getNameOffer());

        ImageView avatar = new ImageView(new Image("/Uploads/" + u.getAvatar()));

        Button profile = new Button(u.getFirstName());

        Label price = new Label(String.valueOf(o.getPriceOffer()));

        Rating rating = new Rating();
        HBox hbb = new HBox();
        Button reserve = new Button("Reserve");

        Button details = new Button("more details");
        hbb.getChildren().add(reserve);
        hbb.getChildren().add(details);

        Vb.getChildren().add(name);
        Vb.getChildren().add(avatar);
        Vb.getChildren().add(profile);
        Vb.getChildren().add(price);
        Vb.getChildren().add(rating);
        Vb.getChildren().add(hbb);

        Hb.getChildren().add(Vb);

        // Add all the service elements to the services container
        Container.add(img,i,j);
        Container.add(Hb, i, j);

        i++;
        if(i>2)
        {
            i = 0;
            j++;
        }
    }

Basically i have this dispalying and i want the elements to be centered inside the Hb variable(HBox). 基本上我有这种令人失望的东西,我希望元素在Hb变量(HBox)内部居中。 i tried Container.setAlignment(Pos.CENTER); 我尝试了Container.setAlignment(Pos.CENTER); but no result . 但没有结果。 Any help is much appriciated 任何帮助都非常有用 在此处输入图片说明

You cannot set the alignment of each cell on GridPane level. 您不能在GridPane级别上设置每个单元格的对齐方式。 You need to set it on column level and row level. 您需要在列级别和行级别进行设置。

So for every column you add, you need to add a column constraint as below to make it horizontally align center: 因此,对于您添加的每一列,都需要添加如下所示的列约束以使其水平居中对齐:

ColumnConstraints col = new ColumnConstraints();
col.setHalignment(HPos.CENTER);
gridPane.getColumnConstraints().add(col);

And for every row you add, you need to add a row constraint as below to make it vertically align center: 对于添加的每一行,您需要添加如下所示的行约束以使其垂直居中对齐:

RowConstraints row = new RowConstraints ();
row.setValignment(VPos.CENTER);
gridPane.getRowConstraints().add(row);

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

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