javafx 2 - Clip an HBox inside a GridPane -


i have javafx 2 gui includes hbox filling 1 of cells of gridpane. of components dynamically sized. displays fine, no problems.

first question: i'd clip contents of hbox @ edge of gridpane cell they're in, none of overflow displayed, can't work out how. i've tried setting clip hbox rectangle of same size doesn't work. guess because it's using dimensions used layout, rather dimensions of what's displayed. should instead clipping on gridpane itself, or @ least using properties?

second question (for bonus point): how clipping interact translation , scaling of node?

thanks help.

i tried couple of short program attempts clip nodes within gridpane. none of clip attempt implementations liked well, though of them worked in fashion or other depending on requirements.

the clip attempt closely seemed fit description last wraps clipped node in region it's own size specifiers, layout of clipped region matches size of clip node applied node.

class clippednode extends region {   private final node content;   private final double width;   private final double height;    clippednode(node content, double width, double height) {     this.content = content;     this.width   = width;     this.height  = height;      content.setclip(new rectangle(width, height));      getchildren().setall(content);   }    @override protected double computeminwidth(double d)   { return width; }   @override protected double computeminheight(double d)  { return height; }   @override protected double computeprefwidth(double d)  { return width; }   @override protected double computeprefheight(double d) { return height; }   @override protected double computemaxwidth(double d)   { return width; }   @override protected double computemaxheight(double d)  { return height; } } 

clippednode sample

a full executable sample demonstrating various clip approaches provided below:

import java.util.iterator; import javafx.application.application; import javafx.beans.value.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.shape.rectangle; import javafx.scene.text.text; import javafx.stage.stage;  public class gridclipping extends application {   string buttontext[] = "the quick brown fox jumped on lazy dog".split(" ");   string[] colors = { "aqua", "coral", "cornsilk", "cornflowerblue" };    @override public void start(stage stage) throws exception {     final gridpane grid = new gridpane();     grid.addrow(0, createhbox("aqua"),     createhbox("coral"));     grid.addrow(1, createhbox("cornsilk"), createhbox("cornflowerblue"));     grid.setstyle("-fx-border-color: red;");      final gridpane gridwithclippedboxes = new gridpane();     gridwithclippedboxes.addrow(0, createclipwrappedhbox("aqua"),     createclipwrappedhbox("coral"));     gridwithclippedboxes.addrow(1, createclipwrappedhbox("cornsilk"), createclipwrappedhbox("cornflowerblue"));     gridwithclippedboxes.setstyle("-fx-border-color: red;");      final radiobutton noclip            = new radiobutton("no clip");     final radiobutton restrictgridsize  = new radiobutton("restrict max grid size (doesn't work)");     final radiobutton clipgrid          = new radiobutton("clip grid");     final radiobutton cliphboxes        = new radiobutton("clip hboxes");     final radiobutton clipwrappedhboxes = new radiobutton("clip wrapped hboxes");      final togglegroup clipradios       = new togglegroup();     clipradios.gettoggles().setall(      noclip,       restrictgridsize,       clipgrid,       cliphboxes,      clipwrappedhboxes     );      final rectangle gridclip = new rectangle(0, 0, 100, 25);      clipgrid.selectedproperty().addlistener(new changelistener<boolean>() {       @override public void changed(observablevalue<? extends boolean> ov, boolean wasclipped, boolean clipped) {         if (clipped != null) {           if (clipped) {             grid.setclip(gridclip);           } else {             grid.setclip(null);           }         }        }     });      restrictgridsize.selectedproperty().addlistener(new changelistener<boolean>() {       @override public void changed(observablevalue<? extends boolean> ov, boolean wasclipped, boolean clipped) {         if (clipped != null) {           if (clipped) {             // not work in our case.             // minimum size of grid components > max size of grid.             // grid expands in size fit minimum size of it's components anyway.             grid.setmaxsize(100, 25);            } else {             grid.setmaxsize(double.max_value, double.max_value);           }         }        }     });      cliphboxes.selectedproperty().addlistener(new changelistener<boolean>() {       @override public void changed(observablevalue<? extends boolean> ov, boolean wasclipped, boolean clipped) {         if (clipped != null) {           if (clipped) {             (node gridcell: grid.getchildren()) {               rectangle cellclip = new rectangle(100, 12);               gridcell.setclip(cellclip);             }           } else {             (node gridcell: grid.getchildren()) {               gridcell.setclip(null);             }           }         }        }     });      final vbox layout = new vbox(10);     clipwrappedhboxes.selectedproperty().addlistener(new changelistener<boolean>() {       @override public void changed(observablevalue<? extends boolean> ov, boolean wasclipped, boolean clipped) {         if (clipped != null) {           if (clipped) {             layout.getchildren().set(0, gridwithclippedboxes);           } else {             layout.getchildren().set(0, grid);           }         }        }     });      noclip.fire();      layout.setstyle("-fx-background-color: cornsilk; -fx-padding: 10;");     layout.getchildren().setall(       grid,       noclip,       restrictgridsize,       clipgrid,       cliphboxes,       clipwrappedhboxes     );      stage.setscene(new scene(layout));     stage.show();   }    private region createhbox(string webcolor) {     hbox box = new hbox(5);     box.setstyle("-fx-background-color: " + webcolor + ";");     (string text: buttontext) {       box.getchildren().add(new text(text));     }     box.setopacity(0.5);      return box;   }        private region createclipwrappedhbox(string webcolor) {     return new clippednode(createhbox(webcolor), 100, 12);   }    class clippednode extends region {     private final node content;     private final double width;     private final double height;      clippednode(node content, double width, double height) {       this.content = content;       this.width   = width;       this.height  = height;        content.setclip(new rectangle(width, height));        getchildren().setall(content);     }      @override protected double computeminwidth(double d)   { return width; }     @override protected double computeminheight(double d)  { return height; }     @override protected double computeprefwidth(double d)  { return width; }     @override protected double computeprefheight(double d) { return height; }     @override protected double computemaxwidth(double d)   { return width; }     @override protected double computemaxheight(double d)  { return height; }   }    public static void main(string[] args) { application.launch(args); } } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -