This discussion is archived
3 Replies Latest reply: Feb 3, 2013 7:23 PM by Narayan RSS

JavaFX isnt loading CSS in Netbeans 7.3 RC1

AndreLopes Newbie
Currently Being Moderated
Guys, please help!
IM trying to load a background in javaFX and isnt working...

Heres my CSS Code :
/* 
    Document   : cadastrarCSS
    Created on : 02/02/2013, 15:55:21
    Author     : André
    Description:
        Purpose of the stylesheet follows.
*/

root{  
    display: block;  
    
}  

.scene{   
    -fx-background-color: blue;
    -fx-background-image: url("myImage.jpg"); 
    background-image: background;
}  
No Errors Happens
And heres my javaFX View Code:
package View;

import Control.ControlCadastro;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

/**
 *
 * @author André
 */
public class CadastrarChatUsers extends Application {
    
    ControlCadastro controlCadastro = new ControlCadastro(this);
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root,450,300);
        scene.setFill(Color.WHEAT);
        
        scene.getStylesheets().add("cadastro.css"); 
        
        primaryStage.setTitle("Cadastro de Usuários");
        
        //Cria o Layout
        GridPane gp = new GridPane();
        gp.setHgap(3);
        gp.setVgap(3);
        
        GridPane gpCadastrar = new GridPane();
        gpCadastrar.setHgap(4);
        gpCadastrar.setVgap(4);
        
        //Cria os Componentes:
        Text titleText = new Text("Cadastrar Usuários Do Chat");
        titleText.setFont(new Font("Verdana",18));
        titleText.setFill(Color.rgb(0,0,0));
        
        //Campo E-mail
        final TextField emailTextField = new TextField();
        Label labelEmail = new Label("E-Mail");
        Tooltip toolTipEmail = new Tooltip("Insira um e-mail Válido");
        emailTextField.setTooltip(toolTipEmail);
        
        //Campo Password
        final TextField passwordTextField = new PasswordField();
        Label labelPassword = new Label("Password");
        Tooltip toolTipPassword = new Tooltip("Insira uma senha Válida");
        passwordTextField.setTooltip(toolTipPassword);
        
        //Campo Nivel Usuario
        final ComboBox<String> nivelUsuarioComboBox = new ComboBox<>();
        nivelUsuarioComboBox.getItems().addAll("Admin","Moderador","Publico","Banido");
        nivelUsuarioComboBox.setValue("Publico");
        
        Label labelNivelUsuario = new Label("Nivel De Uso");
        Tooltip toolTipNivelUsuario = new Tooltip("Selecione O Nivel de Poder");
        nivelUsuarioComboBox.setTooltip(toolTipNivelUsuario);
        
        //Dead File 
        final CheckBox checkBoxDeadFile = new CheckBox("Arquivo Morto");
        Tooltip toolTipDeadFile = new Tooltip("Marque para indicar arquivo Morto");
        checkBoxDeadFile.setTooltip(toolTipDeadFile);
        
        //Banned
        final CheckBox checkBoxBanned = new CheckBox("Banido");
        Tooltip toolTipBanned = new Tooltip("Marque para Indicar Usuário Banido");
        checkBoxBanned.setTooltip(toolTipBanned);
        
        //
        final TextField registroTextField = new TextField();
        Tooltip toolTipRegistro = new Tooltip("Preencha o RE do usuário!");
        registroTextField.setTooltip(toolTipRegistro);
        Label labelRegistro = new Label("Registro");
        
        //Botao de Cadastro de usuário
        Button cadastrarButton = new Button("Cadastrar");
        cadastrarButton.setOnAction(new EventHandler<ActionEvent>() {
           @Override
            public void handle(ActionEvent t) 
            {
               boolean erroAoCadastrarUsuario = controlCadastro.cadastrarUsuario(emailTextField.getText(),passwordTextField.getText(),nivelUsuarioComboBox.getValue(),checkBoxDeadFile.isSelected(),checkBoxBanned.isSelected(),registroTextField.getText());
               if(erroAoCadastrarUsuario == false)
               {
                   emailTextField.setText("");
                   passwordTextField.setText("");
                   registroTextField.setText("");
                   checkBoxDeadFile.setSelected(false);
                   checkBoxBanned.setSelected(false);
               }
            }
        });
        
                
                
        Label labelCadastrar = new Label("Cadastrar");
        
        
        gp.add(titleText,4,2);
        
        gp.add(emailTextField,3,5);
        gp.add(labelEmail,4,5);
        
        gp.add(passwordTextField,3,8);
        gp.add(labelPassword,4,8);
        
        gp.add(nivelUsuarioComboBox,3,11);
        gp.add(labelNivelUsuario,4,11);
        
        gp.add(checkBoxDeadFile,3,14);
        
        gp.add(checkBoxBanned,3,17);
        
        gp.add(registroTextField,3,20);
        gp.add(labelRegistro,4,20);
        
        gp.add(gpCadastrar,3,26);
       
        gpCadastrar.add(cadastrarButton,0,0);
        gpCadastrar.add(labelCadastrar,1,0);
        //
        root.getChildren().add(gp);
        
        primaryStage.setScene(scene);
        primaryStage.show();
       
    }

   
    
    /**
     * The main() method is ignored in correctly deployed JavaFX application.
     * main() serves only as fallback in case the application can not be
     * launched through deployment artifacts, e.g., in IDEs with limited FX
     * support. NetBeans ignores main().
     *
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}
Edited by: Andre Lopes on 03/02/2013 10:05
  • 1. Re: JavaFX isnt loading CSS in Netbeans 7.3 RC1
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. Replace
    scene.getStylesheets().add("cadastro.css"); 
    with
     
    scene.getStylesheets().add(getClass().getResource("cadastro.css").toExternalForm());
    Also I am not sure that your CSS file is correct.
    background-image: background;
  • 2. Re: JavaFX isnt loading CSS in Netbeans 7.3 RC1
    AndreLopes Newbie
    Currently Being Moderated
    I had to change the CSS to same package to make it work with that code.. Still nothing happens.
    And i removed that last CSS line that you mentioned...

    What should i do????

    Please help
  • 3. Re: JavaFX isnt loading CSS in Netbeans 7.3 RC1
    Narayan Pro
    Currently Being Moderated
    Hi Andres ,

    First thing is first :) It seems your CSS codes are not worthy. You are using "*.scene*" class which is not allowed in the javafx CSS.


    Things to remember :
    - JavaFX doesn't take any NORMAL WEB CSS Property. (No worry because "-fx" prefix is usable in most)
    - JavaFX needs -fx prefix in most of the property
    - The Scene object has no settable CSS properties, nor does it have any pseudo-classes.
    - "root" pseudo class is for "Root Node" of the Scene.


    Now for making your CSS worthy you can add some code for getting change in your codes. Add new styleclass for "GridPane"
    Change in SRC Code:
    gp.getStyleClass().add("my-grid");
    Change in CSS
     .my-grid{  
        -fx-background-color: red;
        /* -fx-background-color is property of the Region and GridPane is subclass of Region */ 
    }  
    ---------------------
    Andre Lopes wrote:
    I had to change the CSS to same package to make it work with that code.. Still nothing happens.
    For this scenario lets suppose you have code structure like below
    + Your Project[FOLDER] 
         - css      [FOLDER]
              - cadastro.css
         - view     [FOLDER]
              - CadastrarChatUsers.java      [FILE]
         - control
              - ControlCadastro.java          [FILE]
    No you don't need to change the package. It works without any problem. If you use the "/" at the first of your "Resouce String" then it will point to the root folder of your project.


    Code approach for target css file inside /css package from /view package
    scene.getStylesheets().add(getClass().getResource("/css/cadastro.css").toExternalForm()); 
    ** Make Sure you do "Clean And Build" before running the application After you change your CSS codes this clear and load the actual CSS.

    Thanks
    Narayan

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points