Forum Stats

  • 3,855,310 Users
  • 2,264,499 Discussions
  • 7,905,967 Comments

Discussions

stage.setScene(newScene);

915418
915418 Member Posts: 3
edited Dec 21, 2012 7:15AM in JavaFX 2.0 and Later
Im uuing pretty mych default example file. Im trying to make default javafx program to load another scene from default button. Im using Netbeans 7.2.1.

lines which is errorious is:
Parent sceneMapRoot = FXMLLoader.load(getClass().getResource("piirros.fxml"));

would you need more information? I can also upload a package if that is needed.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javafxapplicationfxml;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;

import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author ebure
 */
public class SampleController implements Initializable {
    
    @FXML
    private Stage  stage;
    
    @FXML
    private Label label;
    
    @FXML
    private void handleButtonAction(ActionEvent event) {


       Parent sceneMapRoot = FXMLLoader.load(getClass().getResource("piirros.fxml"));
        Scene sceneMap = new Scene(sceneMapRoot,500,500);
         
        stage.setTitle("Welcome to game!");
        stage.setScene(sceneMap);
        stage.show();
        System.out.println("You clicked me!");
        
       
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}
Edited by: Ebure on 20.12.2012 6:22

Edited by: Ebure on 20.12.2012 7:04

Edited by: Ebure on 20.12.2012 7:06

Answers

  • fabsav
    fabsav Member Posts: 79
    edited Dec 21, 2012 7:15AM
    # 1 #
    ???
    @FXML
    private Stage stage;
    ???

    you need to use @FXML only to call nodes (with an id) inside the fxml.
    stage contains your fxml, so delete these lines.

    # 2 #
    your fxml is supposed to be added in a scene. it knows nothing about it before his initialization.
    it means that your code is "garbage"... (sorry)
    make it in this way:
    create a class like this that will create the scene and will fill it by the fxml content:
    public class ClassName extends Application {
                
        public static void main(String[] args) {
            Application.launch(ClassName.class, args);
        }
        
        @Override
        public void start(final Stage stage) throws Exception {
            //use here the path for the fxml you want to load as root
            Parent root = FXMLLoader.load(getClass().getResource("fxmlPath"));
    
            final Scene scene = new Scene(root);  
    
            stage.setScene(scene);
            stage.setTitle("Title");
    
            stage.show();
        }
    }
    then in fxml controller, just write nodes parameters, listener, handler etc etc related to the fxml itself.

    ask for more or look at the javafx tutorial

    Edited by: fabsav on 21-dic-2012 13.14

    Edited by: fabsav on 21-dic-2012 13.15
This discussion has been closed.