4

I am having trouble making it so that my nameInput label and passInput label are bolded. My program allows me to do a -fx-text-fill: #FFFFFF; and that works but when i try -fx-font-weight: bold; it wont work or bold my label when the app runs.

Here is my code containing my labels and buttons:

package appGUI;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;

public class Login extends Application {

    Stage window;
    Button loginButton;
    String user = "test";
    String pw = "test";

    public static void main(String[] args) {
        launch(args);       
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("Secret");

        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10,10,10,10));
        grid.setVgap(8);
        grid.setHgap(10);

        // name label
        Label nameLabel = new Label("Enter your name:");
        GridPane.setConstraints(nameLabel, 0, 0);

        // name input
        TextField nameInput = new TextField();
        nameInput.setPromptText("name");
        GridPane.setConstraints(nameInput, 1, 0);

        // password label
        Label passLabel = new Label("Enter the password:");
        GridPane.setConstraints(passLabel, 0, 1);

        // password input
        PasswordField passInput = new PasswordField();
        passInput.setPromptText("password");
        GridPane.setConstraints(passInput, 1, 1);

        // login button
        loginButton = new Button("Enter");
        GridPane.setConstraints(loginButton, 1, 2);
        loginButton.setOnAction(e -> {
            if(nameInput.getText().equals(user) && passInput.getText().equals(pw)) {
                System.out.println("THIS WORKS. YOU FINALLY SOLVED IT!!!!");
          } else {
                AlertBox.display("Error: Access Denied", "Only a special person can access this app");
            }

        });

        grid.getChildren().addAll(nameLabel, nameInput, passLabel, passInput, loginButton); 

        Scene scene = new Scene(grid, 350, 200);
        scene.getStylesheets().add("/appGUI/custom.css");

        window.setScene(scene);
        window.show();

    }
}

And here is the .css file titled "custom.css" that works properly and does everything I wrote except for bold text:

.root {
   -fx-background-color: linear-gradient(#707070, #BABABA); 
}

.label {
    -fx-text-fill: #FFFFFF;
    -fx-font-weight: bold;  
}

.button {
    -fx-background-color: linear-gradient(#00F5FF, #FFA07A);
    -fx-background-radius:10;
}

I have tried almost everything and I can't seem to find the answer to what seems like a really easy problem to fix. My labels just wont bold, but they'll fill with #FFFFFF, and i just want to know why one property works and the other one doesn't!

2
  • It simply works, nothing is wrong. Try with "bolder". Commented Feb 23, 2017 at 7:35
  • have the same problem using FXForms2 generated forms. Some properties work, some don't. In my case, Size and Italic works ok, while text-fill color doesnt... Commented Apr 6, 2017 at 16:48

1 Answer 1

5

For some strange reason the default font not necessarily supports all features.

Try:

.label {
    -fx-font-family: "Helvetica";
    -fx-font-weight: bold;
}

If you want to make this change application wide, add it to the scene:

.root {
    -fx-font-family: "Helvetica";
}

scene.getStylesheets().add("pathTo/root.css");
Sign up to request clarification or add additional context in comments.

3 Comments

I am facing the same problem (on MacOS) and "Helvetica" does not help either. With "Arial" it is now working. CSS in JavaFx seems even more black-magic than for the web.
Dear Jörg, what JavaFx version are you using? Helvetica did work well for me on Mac with Java 8.
I am using OpenJFX version 14.0.1.

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.