Skip to content
Advertisement

How to use the custom CSS in JavaFX without getting warning?

I am working on making a custom style for the label but when I’m calling it I’m getting the following warning Feb 06, 2021 7:54:12 PM com.sun.javafx.css.StyleManager loadStylesheetUnPrivileged WARNING: Resource “@Field-background” not found. which doesn’t make my code to get the custom CSS effect.

MY Code:

public class Tutorial14 extends Application {

Connection conn;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;


@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFX 8 Tutorial 14 - SQLite");
    CheckConnection();



    // Create transparent stage
    primaryStage.initStyle(StageStyle.TRANSPARENT);

    Group root = new Group();
    Scene scene = new Scene(root, 220, 210, Color.rgb(0, 0, 0, 0));
    //Cascading Style Sheet (CSS)
    scene.getStylesheets().add(getClass().getResource("../Style.css").toExternalForm());
    Color foreground = Color.rgb(255, 255, 255, 0.9);

    //Rounded Rectangular where we will add our components
    //Rectangular Background
    Rectangle background = new Rectangle(220, 210);
    background.setX(0);
    background.setY(0);
    background.setArcHeight(15);
    background.setArcWidth(15);
    background.setFill(Color.rgb(0, 0, 0, 0.55));
    background.setStroke(foreground);
    background.setStrokeWidth(1.5);

    VBox vBox = new VBox(5);
    vBox.setPadding(new Insets(10, 0, 0, 10));
    Label label = new Label("Login Status");
    //label.setTextFill(Color.WHITESMOKE);
    label.setFont(new Font("SanSerif", 20));

    TextField userName = new TextField();
    userName.setFont(Font.font("SanSerif", 20));
    userName.setPromptText("User Name");

    userName.getStylesheets().add("@Field-background");

    userName.setMaxWidth(200);


    PasswordField password = new PasswordField();
    password.setFont(Font.font("SanSerif", 20));
    password.setPromptText("Password");
    password.setMaxWidth(200);

    Button button = new Button("Login");
    button.setFont(Font.font("SanSerif, 15"));
    button.setMaxWidth(200);


    password.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });
    userName.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });
    button.setOnAction(e -> {
        try {
            String query = "SELECT * FROM UserTable WHERE UserName = ? and Password = ?";
            preparedStatement = conn.prepareStatement(query);
            preparedStatement.setString(1, userName.getText());
            preparedStatement.setString(2, password.getText());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                label.setText("Login Successful");
            } else {
                label.setText("Login Failed");
            }
            preparedStatement.close();
            resultSet.close();
        } catch (Exception e1) {
            label.setText("SQL Error");
            e1.printStackTrace();
            System.out.println(e1);
        }
    });


    vBox.getChildren().addAll(label, userName, password, button);
    root.getChildren().addAll(background, vBox);
    primaryStage.setScene(scene);
    primaryStage.show();

}

public void CheckConnection() {
    conn = SqlConnection.DbConnector();
    if (conn == null) {
        System.out.println("Connection Not Successful");
        System.exit(1);
    } else {
        System.out.println("Connection Successful");
    }
}

}

MY CSS

.label {
-fx-text-fill: whitesmoke;
}

.button {
-fx-padding: 10 20 10 20;
-fx-end-margin: 200;
}

.field-background {
-fx-text-fill: black;
-fx-prompt-text-fill: gray;
-fx-highlight-fill: gray;
-fx-highlight-text-fill: black;
-fx-background-color: rgba(255, 255, 255, 0.8);
}

Any Advise.

Advertisement

Answer

Your CSS selector .field-background is a class selector.
Thus you should be using getStyleClass on the userName instance.

userName.getStyleClass().add("field-background");
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement