0

I'm new to JavaFx and I'm trying to set up a style. I was flowing a tutorial but I hit a wall I can't get pass. No matter what I do in the CSS file it as no effect on the App.

public class LoginForm extends Application{

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

@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("login2.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("login2.css");

primaryStage.setScene(scene);
primaryStage.show();
}}

I'm using Eclipse, Java 7.1. The weird thing is it does see the CSS file, and I know this because if I change it to a file that isn't there it will not compile. I've try a few code for the CSS file but at the moment it looks like this

.root{
    -fx-font-size: 14pt;
    -fx-font-family: "Tahoma";
    -fx-base: #DFB951;
    -fx-background: #A78732;
    -fx-focus-color: #B6A678;
}
Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
  • 1
    it works for me(i used netbeans), nothing wrong with the code. except you do not need to add the css twice. adding it to the scene is enough. add `-fx-background-color: #A78732;` to `.root` to change the background color. only problem i can think of is if the packages of the java class and css are different or some eclipse configuration problem. – bhathiya-perera Jul 04 '14 at 06:33

2 Answers2

0

It is possible to add the Cascading Style Sheet to the Scene. So if it doesn't solve the problem you can also set the style in the code for a first try.

This is possible to do it like that:

Component.setStyle("CSS-Code insert");
Lukas Hieronimus Adler
  • 1,063
  • 1
  • 17
  • 44
0

I was following the same Oracle tutorial and had the same symptom whith Eclipse. Had you check that there is only one "login2.css" file in your project and that it is located in the bin folder.

Renaud
  • 1
  • 1