1

Today i am Going to create Hello World Application with JavaFX Fxml project.Here is the code. Sample.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="temp.Temp">
<children>
    <Button id="button" layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
    <Label id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="16" prefWidth="69" fx:id="label" />
</children>
</AnchorPane>

Sample.java

package temp;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
public class Sample implements Initializable {

@FXML
private Label label;

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
    label.setText("Hello World!");
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    
}

Temp.java

package temp;
import javafx.application.Appl
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Temp extends Application {
public static void main(String[] args) {
    Application.launch(Temp.class, args);
}
@Override
public void start(Stage stage) throws Exception {
    try{
    Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
    stage.setScene(new Scene(root));
    stage.show();
    }
    catch(Exception e){
        System.out.print(e);
    }      
}
}

But It is Giving Error /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1757: The following error occurred while executing this line: /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1392: jarsigner returned: 1 BUILD FAILED (total time: рем seconds)

But When i Use Exception Handling it Gives Location Not Found ? Please help

2 Answers2

3

You are trying to load Controller from fx:controller="temp.Temp". This is not Your controller class, you should use fx:controller="temp.Sample" instead.

airborn
  • 2,547
  • 1
  • 15
  • 12
1

You also need a leading forward slash to load package resources. Assuming that you have Sample.fxml in the temp package, it would look like this:

Parent root = FXMLLoader.load(getClass().getResource("/temp/Sample.fxml"));
Community
  • 1
  • 1
karl
  • 1,059
  • 11
  • 12