1

I don't know why the cell content can exceed the limits of the gridpanes rowconstraint, although a maximum is set. I tried to clip it somehow, but I don't get the actual height of the cell (when I would change it dynamically). As you can see on the image, the green rectangle is too big. How could I restrict its height dynamically?

enter image description here

Here's a small example:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.RowConstraints;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;


    public class Main extends Application {
        @Override
        public void start(Stage primaryStage) {
            try {
                GridPane gp = new GridPane();
                gp.setGridLinesVisible(true);

                gp.setVgap(10);
                gp.add(new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5))), 0, 0);
                gp.add(new Pane(new Rectangle(100,200,Color.rgb(0,255,0,0.5))), 0, 1);

                gp.getRowConstraints().add(new RowConstraints(50,100,110));
                gp.getRowConstraints().add(new RowConstraints(50,100,110));

                Scene scene = new Scene(gp,400,400);
                primaryStage.setScene(scene);
                primaryStage.show();
            } catch(Exception e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            launch(args);
        }
    }
blaster
  • 845
  • 1
  • 10
  • 25
  • I edited your code. You can use clipping to make the content fit the size of the cell. However the content IS cut away, not shrinked if you had that in mind. – Rupert Jan 21 '16 at 11:34
  • I don't see any edit? If you have used a fixed-size clipping rectangle, then it's not what I was looking for. I want the content to be clipped according to the given grid. – blaster Jan 21 '16 at 12:11
  • Yes, the reviews of my edit showed me 1. that editing is not the correct way and 2. that you're looking for a possibility to resze the cell if it exceeds 100 but the cell should never exceed 110. Right? – Rupert Jan 21 '16 at 12:19

2 Answers2

0

Meta: OK, next try, this time as an answer with your code modified.

To topic: After setting your RowConstraints you can iterate over the GridPane's children nodes and see if on of them exceeds the preferred height. If so, then

  1. clip the Node
  2. set Vgrow for that node in the GridPane

Does this fit your needs?

public class RowConstraintsExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane gp = new GridPane();
            gp.setGridLinesVisible(true);

            int minHeight = 50;
            int preferredHeight = 100;
            int maxHeight = 110;

            gp.setVgap(10);
            gp.add(new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5))), 0, 0);
            gp.add(new Pane(new Rectangle(100,200,Color.rgb(0,255,0,0.5))), 0, 1);

            gp.getRowConstraints().add(new RowConstraints(minHeight, preferredHeight, maxHeight));
            gp.getRowConstraints().add(new RowConstraints(minHeight, preferredHeight, maxHeight));

            for (Node node: gp.getChildren()) {
                Integer rowIndex = GridPane.getRowIndex(node);
                if (rowIndex != null) {
                    RowConstraints rowConstraints = gp.getRowConstraints().get(rowIndex);

                    if (node.getBoundsInLocal().getHeight() > rowConstraints.getPrefHeight()) {
                        node.setClip(new Rectangle(100, rowConstraints.getMaxHeight()));
                        GridPane.setVgrow(node, Priority.SOMETIMES);
                    }
                }
            }

            Scene scene = new Scene(gp,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Rupert
  • 91
  • 4
  • no, because once the clip is set, it is set. I'd like to have the clip following the size of the grid-cell. – blaster Jan 22 '16 at 12:55
  • assume to following situation: 3 rowconstraints, each (150,300,400). Now I set the first rowconstraint.minheight to 200. the other two gridcells will get smaller. I want the content in the gridcells to be clipped accordingly. – blaster Jan 22 '16 at 13:08
0

You could adjust your code as follows and use a clipping rectangle:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane gp = new GridPane();
            gp.setGridLinesVisible(true);

            gp.setVgap(10);
            gp.getRowConstraints().add(new RowConstraints(50,100,110));
            gp.getRowConstraints().add(new RowConstraints(50,100,110));

            Pane pane1 = new Pane(new Rectangle(100,100,Color.rgb(255, 0, 0, 0.5)));
            Pane pane2 = new Pane(new Rectangle(100,200,Color.rgb(0, 255, 0, 0.5)));

            Rectangle clipRectangle1 = new Rectangle();
            pane1.setClip(clipRectangle1);
            pane1.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
                clipRectangle1.setWidth(newValue.getWidth());
                clipRectangle1.setHeight(newValue.getHeight());
            });   

            Rectangle clipRectangle2 = new Rectangle();
            pane2.setClip(clipRectangle2);
            pane2.layoutBoundsProperty().addListener((observable, oldValue, newValue) -> {
                clipRectangle2.setWidth(newValue.getWidth());
                clipRectangle2.setHeight(newValue.getHeight());
            });

            gp.add(pane1, 0, 0);
            gp.add(pane2, 0, 1);

            Scene scene = new Scene(gp,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
user2035039
  • 961
  • 3
  • 16
  • 30