New_ToQT
New_ToQT

Reputation: 53

Javafx Grid pane Center elements

I m working on a javafx application . a part of it consists on rendering a List into the Gui using a scrollpane of a grid element . i m 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). i tried Container.setAlignment(Pos.CENTER); but no result . Any help is much appriciatedenter image description here

Upvotes: 1

Views: 2956

Answers (1)

Sai Dandem
Sai Dandem

Reputation: 9989

You cannot set the alignment of each cell on GridPane level. 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);

Upvotes: 3

Related Questions