Reputation: 165
I have a table view in javafx scene-builder
. I want to use Vgrow ALWAYS
property using css. My table-view is within the HBox
. The scene-builder allow me to use this property. as shown.
But how i do it using css. please help me.
I have tried it in css this way
-fx-Vgrow: ALWAYS;
/* also */
Vgrow: ALWAYS;
nothing works. any help?
Upvotes: 0
Views: 4133
Reputation: 9869
Just to make it CSS customizable, you are thinking for a massive work on your controls. All the required css meta implementation is defined on the super class level (so most of the css meta data is defined in Region class). As all the javafx controls are subclass of Region, they do satisfy all the css meta data properties set in Region.
If you want to have your own specify css meta data, then you have to define explicity on each and every control that you want to have. (which is again a big work).
I am NOT saying that this is something that cannot be done. Below is the demo code for a customized ScrollPane to include vgrow and hgrow as css meta properties. And it perfectly works!! Just imagine if you want the below feature for all controls, you have to customize every control that you want to configure through css ;) .
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CustomCSSPropertyDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
VBox root = new VBox();
root.setPadding(new Insets(15));
root.setSpacing(15);
Scene sc = new Scene(root, 600, 600);
sc.getStylesheets().add(getClass().getResource("custom.css").toString());
stage.setScene(sc);
stage.show();
Label label = new Label("Testing");
MyScrollPane scrollPane = new MyScrollPane();
scrollPane.getStyleClass().add("myclass");
root.getChildren().addAll(label,scrollPane);
}
public static void main(String[] args) {
Application.launch(args);
}
}
The css file code:
.myclass{
-fx-vgrow-policy:always;
}
And the customized ScrollPane control:
import com.sun.javafx.css.converters.EnumConverter;
import javafx.beans.property.ObjectProperty;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
import javafx.css.StyleableObjectProperty;
import javafx.css.StyleableProperty;
import javafx.scene.control.Control;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MyScrollPane extends ScrollPane {
private ObjectProperty<Priority> hgrowPolicy;
private ObjectProperty<Priority> vgrowPolicy;
public final void setHgrowPolicy(Priority var1) {
this.hgrowPolicyProperty().set(var1);
}
public final Priority getHgrowPolicy() {
return this.hgrowPolicy == null ? Priority.SOMETIMES : (Priority)this.hgrowPolicy.get();
}
public final ObjectProperty<Priority> hgrowPolicyProperty() {
if (this.hgrowPolicy == null) {
this.hgrowPolicy = new StyleableObjectProperty<Priority>(Priority.SOMETIMES) {
@Override
protected void invalidated() {
HBox.setHgrow(MyScrollPane.this, this.get() );
}
public CssMetaData<MyScrollPane, Priority> getCssMetaData() {
return MyScrollPane.StyleableProperties.HGROW_POLICY;
}
public Object getBean() {
return MyScrollPane.this;
}
public String getName() {
return "hgrowPolicy";
}
};
}
return this.hgrowPolicy;
}
public final void setVgrowPolicy(Priority var1) {
this.vgrowPolicyProperty().set(var1);
}
public final Priority getVgrowPolicy() {
return this.vgrowPolicy == null ? Priority.SOMETIMES : (Priority)this.vgrowPolicy.get();
}
public final ObjectProperty<Priority> vgrowPolicyProperty() {
if (this.vgrowPolicy == null) {
this.vgrowPolicy = new StyleableObjectProperty<Priority>(Priority.SOMETIMES) {
@Override
protected void invalidated() {
VBox.setVgrow(MyScrollPane.this, this.get() );
}
public CssMetaData<MyScrollPane, Priority> getCssMetaData() {
return StyleableProperties.VGROW_POLICY;
}
public Object getBean() {
return MyScrollPane.this;
}
public String getName() {
return "vgrowPolicy";
}
};
}
return this.vgrowPolicy;
}
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return MyScrollPane.StyleableProperties.STYLEABLES;
}
public List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
return getClassCssMetaData();
}
private static class StyleableProperties {
private static final CssMetaData<MyScrollPane, Priority> HGROW_POLICY;
private static final CssMetaData<MyScrollPane, Priority> VGROW_POLICY;
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;
private StyleableProperties() {
}
static {
HGROW_POLICY = new CssMetaData<MyScrollPane, Priority>("-fx-hgrow-policy", new EnumConverter(Priority.class), Priority.SOMETIMES) {
public boolean isSettable(MyScrollPane var1) {
return var1.hgrowPolicy == null || !var1.hgrowPolicy.isBound();
}
public StyleableProperty<Priority> getStyleableProperty(MyScrollPane var1) {
return (StyleableProperty)var1.hgrowPolicyProperty();
}
};
VGROW_POLICY = new CssMetaData<MyScrollPane, Priority>("-fx-vgrow-policy", new EnumConverter(Priority.class), Priority.SOMETIMES) {
public boolean isSettable(MyScrollPane var1) {
return var1.vgrowPolicy == null || !var1.vgrowPolicy.isBound();
}
public StyleableProperty<Priority> getStyleableProperty(MyScrollPane var1) {
return (StyleableProperty)var1.vgrowPolicyProperty();
}
};
ArrayList list = new ArrayList(Control.getClassCssMetaData());
list.add(HGROW_POLICY);
list.add(VGROW_POLICY);
STYLEABLES = Collections.unmodifiableList(list);
}
}
}
Now looking back, while targeting to make things simple, we are indeed making it more complex. Now it is upto you how to proceed with :)
Upvotes: 1