Region
には適用できますが、には適用できないCSS プロパティを使用していますRectangle
( 「CSS リファレンス」を参照)。でサポートされているすべてのプロパティは、Node
から利用できますNode.getCssMetaData
。評価中
new Rectangle().getCssMetaData().stream().map(CssMetaData::getProperty).sorted().forEach(System.out::println);
次のリストが得られます (Java 8 update 92):
-fx-arc-height
-fx-arc-width
-fx-blend-mode
-fx-cursor
-fx-effect
-fx-fill
-fx-focus-traversable
-fx-opacity
-fx-rotate
-fx-scale-x
-fx-scale-y
-fx-scale-z
-fx-smooth
-fx-stroke
-fx-stroke-dash-array
-fx-stroke-dash-offset
-fx-stroke-line-cap
-fx-stroke-line-join
-fx-stroke-miter-limit
-fx-stroke-type
-fx-stroke-width
-fx-translate-x
-fx-translate-y
-fx-translate-z
visibility
height
これらのいずれも、またはwidth
のスタイルを設定できませんRectangle
。fill
また、 a のは、 ではなく CSS プロパティRectangle
を使用して割り当てられることに注意してください。-fx-fill
-fx-background-color
回避策
使用するRegion
Rectangle
を aRegion
に置き換えて、最小サイズと最大サイズを幅/高さに設定するだけです。
.timesheetSlot{
-fx-background-color: #ff6600;
-fx-width: 20;
-fx-min-width: -fx-width;
-fx-max-width: -fx-width;
-fx-height: 25;
-fx-min-height: -fx-height;
-fx-max-height: -fx-height;
-fx-cursor: hand;
}
拡張中Rectangle
または、CSS を使用したサイズの割り当てをサポートするクラスでクラスを拡張しRectangle
ます。これには、2 つの新しいスタイル可能なプロパティを追加する必要があるため、いくつかのコードが必要です。
public class StyleableRectangle extends Rectangle {
private final StyleableDoubleProperty styleableWidth = new SimpleStyleableDoubleProperty(WIDTH_META_DATA, this, "styleableWidth");
private final StyleableDoubleProperty styleableHeight = new SimpleStyleableDoubleProperty(HEIGHT_META_DATA, this, "styleableHeight");
public StyleableRectangle() {
bind();
}
public StyleableRectangle(double width, double height) {
super(width, height);
initStyleableSize();
bind();
}
public StyleableRectangle(double width, double height, Paint fill) {
super(width, height, fill);
initStyleableSize();
bind();
}
public StyleableRectangle(double x, double y, double width, double height) {
super(x, y, width, height);
initStyleableSize();
bind();
}
private void initStyleableSize() {
styleableWidth.set(getWidth());
styleableHeight.set(getHeight());
}
private final static List<CssMetaData<? extends Styleable, ?>> CLASS_CSS_META_DATA;
private final static CssMetaData<StyleableRectangle, Number> WIDTH_META_DATA = new CssMetaData<StyleableRectangle, Number>("-fx-width", StyleConverter.getSizeConverter()) {
@Override
public boolean isSettable(StyleableRectangle styleable) {
return !styleable.styleableWidth.isBound();
}
@Override
public StyleableProperty<Number> getStyleableProperty(StyleableRectangle styleable) {
return styleable.styleableWidth;
}
};
private final static CssMetaData<StyleableRectangle, Number> HEIGHT_META_DATA = new CssMetaData<StyleableRectangle, Number>("-fx-height", StyleConverter.getSizeConverter()) {
@Override
public boolean isSettable(StyleableRectangle styleable) {
return !styleable.styleableHeight.isBound();
}
@Override
public StyleableProperty<Number> getStyleableProperty(StyleableRectangle styleable) {
return styleable.styleableHeight;
}
};
static {
// combine already available properties in Rectangle with new properties
List<CssMetaData<? extends Styleable, ?>> parent = Rectangle.getClassCssMetaData();
List<CssMetaData<? extends Styleable, ?>> additional = Arrays.asList(HEIGHT_META_DATA, WIDTH_META_DATA);
List<CssMetaData<? extends Styleable, ?>> own = new ArrayList(parent.size()+ additional.size());
own.addAll(parent);
own.addAll(additional);
CLASS_CSS_META_DATA = Collections.unmodifiableList(own);
}
// make metadata available for extending the class
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return CLASS_CSS_META_DATA;
}
@Override
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData() {
return CLASS_CSS_META_DATA;
}
private void bind() {
this.widthProperty().bind(this.styleableWidth);
this.heightProperty().bind(this.styleableHeight);
}
// -------------------------------------------------------------------------
// ----------------------- PROPERTY METHODS --------------------------------
// -------------------------------------------------------------------------
public final double getStyleableHeight() {
return this.styleableHeight.get();
}
public final void setStyleableHeight(double value) {
this.styleableHeight.set(value);
}
public final DoubleProperty styleableHeightProperty() {
return this.styleableHeight;
}
public final double getStyleableWidth() {
return this.styleableWidth.get();
}
public final void setStyleableWidth(double value) {
this.styleableWidth.set(value);
}
public final DoubleProperty styleableWidthProperty() {
return this.styleableWidth;
}
}
サンプル CSS:
.timesheetSlot {
-fx-fill: brown;
-fx-width: 20;
-fx-height: 25;
-fx-cursor: hand;
}