Blank window using Javafx - javafx

I made a window by using the screen builder program, but after I run it, only a white blank window appears. I think the problem that all variables , which are lblOUTPUT, aren't assigned. I'm a beginner at Scene builder and have been really struggling with this issue because it's my first work.
FXmain
package javaapplication6;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author ammar
*/
public class FXMain extends Application {
#Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/javaapplication6/FXML.fxml"));
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.setTitle("Kilometer to miles converter");
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLcontroller:
enter code here
package javaapplication6;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author ammar
*/
public class FXMLController implements Initializable {
#FXML
private Label lblOUTPUT;
#FXML
private Button btnCONVERT;
#FXML
private TextField txtKm;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

Ok, I solved after two hours of struggling, I just click the run button in the NetBeans (looks like a play button) and after that, right-click on the main class, then run the file, and it worked!.

Related

'#FXML' not applicable to type

package sample;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
import
public class tGenController implements Initializable {
Controller of a fxml
#FXML
//Error appears here says "#FXML" not applicable to type
private class generatePress() {
}
#Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
}
Error msg pop up on "#FXML" says "#FXML" not applicable type.
I through I did import all the required package but that still doesn't work.
There is a bit of mistake in your code. I think generatePress() is a function and you have defined it as a class.
Try this in your controller class -
#FXML private void generatePress(){
}
I cannot put a comment as I haven't enough reputation : why does your controller implements Initializable? Isn't it more convenient for you to use this code instead?
public class tGenController extends [Root] {
// [Root] is the main tag on your FXML file
#FXML
private void generatePress() {
//XXX
}
#FXML
public void initialize() {
//XXX
}
}

JavaFX NullPointer Exception Location is required

I have an FXML file that is in a separate package. this answer is close but it is located in the same package.
the what confuses me is
Where is this path /src/resources/.fxml that some of the answers reference
How do I "copy" my fxml code into it.
This is the line of code that is getting me Nullpointerexception: Location is Required
root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));
I have tried it with
root = FXMLLoader.load(getClass().getResource("/IMSView/AppView.fxml"));
Still get the same failure.
I am very new to java and I know I am doing something wrong. I could get this project to run when everything was in one file path. Now that I have added packages I cannot get the fxml to load.
Thanks for your time.
EDIT
I am using netbeans
The Main method ins in IMS.java The FXML that I want to load is in a package called IMSview and is name AppView.fxml
Project files
My Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ims;
import IMSModel.Product;
import IMSModel.Part;
import IMSView.AddProductController;
import IMSView.AppViewController;
import IMSView.AddPartController;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
*
* #author DBarnett
*/
public class IMS extends Application {
private Stage primaryStage;
private Stage window;
private BorderPane rootLayout;
private ObservableList<Part> PartData = FXCollections.observableArrayList();
private ObservableList<Product> ProductData = FXCollections.observableArrayList();
public IMS() {
// Add some sample data
// PartData.add(new InHouse(1,"Silly Cow",1,23));
// ProductData.add(new OutSourced(2"dog",2,3));
}
/**
* Returns the data as an observable list of Parts.
*
* #return
*/
public ObservableList<Part> getPartData() {
return PartData;
}
public ObservableList<Product> getProductData() {
return ProductData;
}
#Override
public void start(Stage primaryStage) throws IOException {
window = primaryStage;
Parent root;
root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("IMS");
primaryStage.setScene(scene);
primaryStage.show();
// this.primaryStage = primaryStage;
// this.primaryStage.setTitle("Inventory Management System");
window.setOnCloseRequest(e -> {
e.consume();
closeProgram();
});

Loading new scene in JavaFX

I am attempting to switch scenes from a login screen to the main screen of my program but whenever I try to switch scenes after clicking login, I get the following error.
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException"
I've messed around with my code and try to change some things to get different results but no dice. This is my first time doing a GUI so any help would be appreciated.
package pwmanager;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
*
* #author 176878
*/
public class FXMLDocumentController implements Initializable {
#FXML
private Button loginButton;
#FXML
Stage prevStage;
Stage currentStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
#FXML
public void getPrevStage(Stage stage){
this.currentStage = prevStage;
}
#FXML
public void loginButtonAction(ActionEvent event) throws IOException {
System.out.println("You clicked me, logging in!");
setPrevStage(prevStage);
Stage stage = new Stage();
try{
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainScreen.fxml"));
GridPane mainScreen = (GridPane) loader.load();
Scene scene = new Scene(mainScreen);
stage.setScene(scene);
stage.setTitle("Password Manager");
stage.show();
prevStage.hide();
}
catch(IOException e){
System.out.println("Did not load right");
}
}
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}

NullPointerException in changing javaFX scene

I have two scenes in my javaFx project .. the first one Language.fxml has a button which on click changes the scene to allDevices.fxml .. but it throws NullPointerException saying "Location is required" although both of the fxml files are in the same path !!
that's my LanguageController.java
package astrolabe;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
*
* #author Ahmed Fawzy
*/
public class LanguageController implements Initializable {
#FXML
private Button arabic ;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
arabic.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
try{
Node node=(Node) event.getSource();
Stage stage=(Stage) node.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("allDevices.fxml"));/* Exception */
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
The problem solved by adding the package name before the fxml file name !

Putting an object on a stage when mouse clicked

package javafxapplication36;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Lighting;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
private void init(Stage primaryStage) {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
I am trying to put an circle on stage when mouse is pressed, I am unable to make it happen. I tried setting the root at the beginning, but it doesn't happen.
Help appreciated !
I made just a few edits to your code and it seems to work perfectly !
Note : You need not set the root again inside the scene as you have already did it in the beginning !
Same goes for setting the scene to the Stage !
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.show();
}
}

Resources