Recently delving into JavaFx, I decided to try my hand at setting up a custom style. Following a tutorial, I encountered a stumbling block that I just can't seem to overcome. No matter what changes I make in the CSS file, it simply doesn't reflect on the application.
public class UserLogin extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage primaryStage){
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25,25,25,25));
Scene scene = new Scene(grid, 300, 275);
scene.getStylesheets().add(getClass().getClassLoader().getResource("login-style.css").toExternalForm());
Label userName = new Label("User Name:");
grid.add(userName, 0, 1);
TextField userTextField = new TextField();
grid.add(userTextField, 1, 1);
Label pw = new Label("Password:");
grid.add(pw, 0, 2);
PasswordField pwBox = new PasswordField();
grid.add(pwBox, 1, 2);
Button btn = new Button("Sign in");
HBox hbBtn = new HBox(10);
hbBtn.setAlignment(Pos.BOTTOM_RIGHT);
hbBtn.getChildren().add(btn);
grid.add(hbBtn, 1, 4);
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
btn.setOnAction(new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent e){
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Sign in button pressed");
}
});
grid.getStylesheets().add("login-style.css");
primaryStage.setScene(scene);
primaryStage.show();
}}
Working within Eclipse with Java 7.1, the curious thing is that it does recognize the CSS file. This is evident because if I mistakenly reference a non-existent file, it fails to compile. I've experimented with several code snippets for the CSS file, and for now, it resembles something like this:
.root{
-fx-font-size: 14pt;
-fx-font-family: "Tahoma";
-fx-base: #DFB951;
-fx-background: #A78732;
-fx-focus-color: #B6A678;
}