2

I am trying to create a custom control in JavaFx. The main aim is to create a node that acts as a button. It is simply a Label that would be styled with css to look like a button. I have created a class MyControl that extends the control class like so:

public class MyControl extends Control { @Override protected String getUserAgentStylesheet() { return MyControl.class.getResource("myControl.css").toExternalForm(); } public MyControl() { getStyleClass().add("new-control"); } public MyControl(String item) { getStyleClass().add("new-control"); } } } 

For the SkinBase class MyControlSkin I have:

public class MyControlSkin extends SkinBase<MyControlSkin> { public MyControlSkin(MyControlSkin control) { super(control); Label label = new Label("Some text here"); getChildren.add(label); } } 

In the myControl.css file i simply have:

.tree-node-view { -fx-skin: "treeviewsample.TreeViewSkin"; } .label { -fx-text-fill: -fx-text-background-color; } 

I have created the css for this but the problem is that I don't see the label displayed on the scene:

MyControl control = new MyControl(); 

but no Label displays on the screen. Please help me I am new to this so ask me for more information if in case my question does not make sense.

I am using javaFx 2.2

2
  • Can you show myControl.css? Commented Oct 18, 2014 at 0:12
  • I have added the css file myControl.css Commented Oct 18, 2014 at 9:11

1 Answer 1

3

I don't know if you made errors transferring the code from your IDE to the post, but as it stands the skin class will not compile. You need

public class MyControlSkin extends SkinBase<MyControl> { public MyControlSkin(MyControl control) { super(control); Label label = new Label("Some text here"); getChildren().add(label); } } 

The css must define an fx:skin property for a class that matches the control. So you need

.new-control { -fx-skin: "MyControlSkin"; } .label { -fx-text-fill: -fx-text-background-color; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much. I copied it wrong. The css -fx-skin property was linking to the wrong file. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.