1

I have over 100 Buttons in my JAVAfx application and I want to give a DEFAULT styling[given below] to all buttons in the programme. Please help ! :)

-fx-background-color:#3c7fb1;
-fx-text-fill: black;
-fx-font-size: 14px;
-fx-padding: 3 30 3 30;
2
  • possible duplicate of Styling button in javaFX using CSS Commented Dec 27, 2014 at 14:33
  • Nope @X-Fate this question is not a duplicate of the one specified by you. Both are completely different. Commented Dec 28, 2014 at 6:16

3 Answers 3

3
  1. Create a new CSS file.
  2. Attach the CSS file to your Scene.
  3. Put your button styles to .button {}.
Sign up to request clarification or add additional context in comments.

Comments

0

It's easy to set default Style for all JavaFX Button in an application. Just give a id to the style sheet which you want to set as default for all button.And then set this id for all button of your application.

Button button =new Button("Button");
Button button1 =new Button("Button");
button.setId("allbtn");
button1.setId("allbtn");
String  style= getClass().getResource("New.css").toExternalForm();
scene.getStylesheets().add(style);
  1. Create buttons
  2. apply id to them (for css as we apply in the html) using setId().
  3. Define CSS for this ID
  4. Finally add CSS file to the Scene Thats it.

And CSS file :

#allbtn{
-fx-color:black;
-fx-padding:4px;
-fx-background-color:#34c669;
-fx-background-radius: 10px;
}

Learn more about JavaFX Button CSS

1 Comment

I think by that you're loosing the ability to lookup different buttons in one layout. Also when you have one callback for several buttons you can't distinguish the caller by it's ID anymore. So personally I would not recommend this approach.
0

To apply a default style in an fxml file add a '.button' class to your css file and include it in the Anchor pane

Add this to your css file e.g. app.css

.button {
        -fx-color:black;
        -fx-padding:4px;
        -fx-background-color:blue;
        -fx-background-radius: 10px;
}

Update your AnchorPane tag to include the stylesheet:

<AnchorPane prefHeight="700.0" prefWidth="500.0" stylesheets="@../app.css" >

This will change all buttons to use your style for within the fxml file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.