JavaFX Load exception in main - javafx

I am having some problems with my code I'm writing for an exam project. It's in JavaFX.
I an sending some parameters from my model to my main, and then again from the main to the controller. I am getting a LoadException even though the location set should be correct.
This is the error code:
javafx.fxml.LoadException:
bio/grpro_project/target/classes/project/view/reservation.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2565)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at project.ReservationMain.showReservationView(ReservationMain.java:22)
at project.ReservationMain.start(ReservationMain.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/272422922.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/2050891229.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1063369253.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1170609517.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1399457240.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2560)
... 18 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at project.controller.ReservationController.initialize(ReservationController.java:166)
... 28 more
This is the main class:
public class ReservationMain extends Application {
public void showReservationView(Stage stage, String showtitle, String showdate, String showtime) {
try {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("view/reservation.fxml"));
Parent reservationView = loader.load();
ReservationController controller = loader.<ReservationController>getController();
controller.setup(showtitle, showdate, showtime);
stage.setTitle("Reservation");
stage.setScene(new Scene(reservationView, 800, 600));
stage.show();
} catch (IOException e) {
e.printStackTrace(); //Show a dialog or something... ?
}
}
#Override
public void start(Stage primaryStage) throws Exception {
showReservationView(primaryStage, "", "", "");
}
public static void main(String[] args) {
launch(args);
}
}
And this is the method in the model used to send parameters to the main:
reservationButton.setOnAction((event) -> {
System.out.println(showtime + showdate + showtime);
ReservationMain newscene = new ReservationMain();
Stage stage = new Stage();
newscene.showReservationView(stage, showtitle, showdate, showtime);
});
Hope someone can help - I've been trying to fix this for many hours now.
The controller code (gets data from a model class, should not be a problem though):
public class ReservationController {
private Reservation reservation = new Reservation();
private DatabaseLoad databaseLoad = new DatabaseLoad();
private DatabaseInsert databaseInsert = new DatabaseInsert();
private int theater;
private ObservableList numberOfSeatsSelectedItems = FXCollections.observableArrayList();
private int numberOfSeatsToSelect = 1;
private int selectedRow;
private int selectedSeat;
private int showID;
#FXML
private TextField nameField;
#FXML
private TextField phoneField;
#FXML
private GridPane seatsGridContainer;
#FXML
private VBox movieScreenBox;
#FXML
private Text theaterNumber;
#FXML
private Text freeSeats;
#FXML
private ComboBox seatsSelectedBox;
#FXML
private Button reserveButton;
#FXML
private Text movieTitleText;
#FXML
private Text movieDateText;
#FXML
private Text movieTimeText;
public ReservationController() {
}
public void setup(String showtitle, String showdate, String showtime){
showID = reservation.loadTheatertoShow(showtitle, showdate, showtime);
reservation.loadTheaterFromDatabase(showID);
theater = reservation.getTheaterNumber();
numberOfSeatsSelectedItems.addAll(1,2,3,4,5,6,7,8);
}
#FXML
public void initialize() {
//int numberOfSeatsSelected;
seatsSelectedBox.setItems(numberOfSeatsSelectedItems);
seatsSelectedBox.setOnAction((event) -> {
numberOfSeatsToSelect = (int) seatsSelectedBox.getSelectionModel().getSelectedItem();
});
movieScreenBox.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
seatsGridContainer.setHgap(8);
seatsGridContainer.setVgap(8);
seatsGridContainer.setAlignment(Pos.CENTER);
ArrayList occupiedSeats = reservation.loadOccupiedSeats(showID);
Seat[][] seat = new Seat[reservation.getNumberOfRows()][reservation.getNumberOfSeatsInARow()];
for (int row = 0; row < seat.length; row++) {
for (int seatNumber = 0; seatNumber < seat[0].length; seatNumber++) {
seat[row][seatNumber] = new Seat("");
seat[row][seatNumber].setPrefSize(30, 30);
//
final int finalRow = row;
final int finalSeatNumber = seatNumber;
seat[row][seatNumber].setOnMouseClicked(arg0 -> {
selectedRow = finalRow;
selectedSeat = finalSeatNumber;
for (int i = 0; i < seat.length; i++) {
for (int j = 0; j < seat[0].length; j++) {
if (seat[i][j].isFree()) {
seat[i][j].setStyle("-fx-background-color: green;");
seat[i][j].setSelected(false);
}
}
}
// check: does the seat selection extend the boundaries of the theater or go into already booked seats?
if(seat[finalRow].length <= finalSeatNumber-1 + numberOfSeatsToSelect) {
int seatsLeft = seat[finalRow].length - finalSeatNumber;
seatsSelectedBox.setValue(seatsLeft);
for(int i = 0 ; i < seatsLeft ; i++) {
seat[finalRow][finalSeatNumber + i].setStyle("-fx-background-color: dodgerblue;");
}
}
else {
for(int i = 0 ; i < numberOfSeatsToSelect ; i++) {
if(!seat[finalRow][finalSeatNumber + i].isFree()) {
seatsSelectedBox.setValue(i);
break;
}
seat[finalRow][finalSeatNumber + i].setStyle("-fx-background-color: dodgerblue;");
seat[finalRow][finalSeatNumber + i].setSelected(true);
}
}
});
// checks and sets which seats are booked
if(occupiedSeats.contains("" + row + seatNumber)) {
seat[row][seatNumber].setIsFree(false);
}
//set free seats to change the cursor on mouseover
if (seat[row][seatNumber].isFree()) {
seat[row][seatNumber].setStyle("-fx-background-color: green;");
seat[row][seatNumber].setOnMouseEntered(me -> seatsGridContainer.setCursor(Cursor.HAND));
seat[row][seatNumber].setOnMouseExited(me -> seatsGridContainer.setCursor(Cursor.DEFAULT));
}
// set booked seats to display as red
else {
seat[row][seatNumber].setStyle("-fx-background-color: red;");
seat[row][seatNumber].setDisable(true);
}
seatsGridContainer.add(seat[row][seatNumber], seatNumber, row);
}
}
reserveButton.setOnAction((event) -> {
databaseInsert.newPersonInDatabase(nameField.getText(),phoneField.getText());
ArrayList nextPersonIDs = databaseLoad.getFromDatabase(
"SELECT * FROM person WHERE PhoneNumber=" + phoneField.getText(),"person")[2];
int nextPersonID = (int) nextPersonIDs.get(nextPersonIDs.size() - 1);
ArrayList arr = databaseLoad.getFromDatabase("SELECT * FROM reservations","reservations")[0];
int nextReservationID = (int) arr.get(arr.size()-1) + 1;
for(int i = 0; i < numberOfSeatsToSelect ; i++) {
databaseInsert.newReservationInDatabase(
nextReservationID, nextPersonID, showID, selectedRow, selectedSeat+i);
}
initialize();
});
ArrayList[] shows = databaseLoad.getFromDatabase("SELECT * FROM shows WHERE ShowID=" + showID, "shows");
movieTitleText.setText("Film: " + shows[0].get(0));
movieDateText.setText("Dato: " + shows[1].get(0).toString());
movieTimeText.setText("Tidspunkt: " + shows[2].get(0).toString());
theaterNumber.setText("Sal nr: " + theater);
freeSeats.setText("Ledige sæder: " + (reservation.getNumberOfSeats()-occupiedSeats.size()));
}
}
If I try adding a / to the folder of the fxml file (/view/reservation.fxml), this is the error message:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/245257410.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2428)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at project.ReservationMain.showReservationView(ReservationMain.java:22)
at project.ReservationMain.start(ReservationMain.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/2038127331.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1716030070.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1391275142.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/156840264.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/848186220.run(Unknown Source)
... 1 more
Process finished with exit code 1

Related

FXML Exception in JavaFx | TableView - Table is Null | Eclispe IDE

I have been working on it for the past 3-4 days. I have googled it and also saw similar questions' answers but it did not help me.
Exception at Changing Scence:javafx.fxml.LoadException:
address-to-//application/Dashboard.fxml
This is the Exception I am getting when I log into the dashboard from the login page. Usually, I am not getting this exception but when I am trying to display the data in tableView from the xampp server then this exception occurs otherwise it is working fine.
here is the code of Main.java
public class Main extends Application {
private static Stage stg;
#Override
public void start(Stage primaryStage) {
stg = primaryStage;
try {
primaryStage.setResizable(false);
Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
primaryStage.setTitle("My Application Title");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root , 1200,670));
primaryStage.show();
}catch(Exception e) {
System.out.println("Exception Occured: " +e);
}
}
//to change the scene
public void changeScene(String fxml) {
//passing XML file via a string parameter
try {
Parent pane = FXMLLoader.load(getClass().getResource(fxml));
stg.getScene().setRoot(pane);
}catch(Exception e) {
System.out.println("Exception at Changing Scence:" +e);
}
}
public static void main(String[] args) {
launch(args);
}
}
and this is the code on the login button
/*TO CHECK THE LOGIN
* CREDENTIALS STARTS*/
#FXML
public void loginUser(ActionEvent event) {
uname = username.getText().toString();
upass = password.getText().toString();
try {
DbConnectivity db = new DbConnectivity();
pst = db.connect.prepareStatement("SELECT * FROM adminlogin WHERE admin=? AND password=?");
pst.setString(1, uname);
pst.setString(2, upass);
rst = pst.executeQuery();
if(uname.isEmpty() && upass.isEmpty()) {
wronginput.setText("Please Enter Credentials!");
} else if(rst.next()) {
wronginput.setText("Log In Success!");
Main main = new Main();
main.changeScene("/application/Dashboard.fxml");
} else {
wronginput.setText("Wrong Username or Password");
username.setText("");
password.setText("");
username.requestFocus();
}
} catch (SQLException e) {
System.out.println("Prepared Exception: "+e);
}
}
/*TO CHECK LOGIN
* CREDENTIALS ENDS*/
And these are StackTrace
javafx.fxml.LoadException:
address-to-/application/Dashboard.fxml
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2714)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2692)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2555)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3368)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3324)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3292)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3264)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3240)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3233)
at application.Main.changeScene(Main.java:39)
at application.LoginControl.loginUser(LoginControl.java:59)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.base#19.0.2.1/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml#19.0.2.1/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1852)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base#19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics#19.0.2.1/javafx.scene.Node.fireEvent(Node.java:8923)
at javafx.controls#19.0.2.1/javafx.scene.control.Button.fire(Button.java:203)
at javafx.controls#19.0.2.1/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:207)
at javafx.controls#19.0.2.1/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base#19.0.2.1/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base#19.0.2.1/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base#19.0.2.1/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics#19.0.2.1/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
at javafx.graphics#19.0.2.1/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
at javafx.graphics#19.0.2.1/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at javafx.graphics#19.0.2.1/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.View.notifyMouse(View.java:937)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics#19.0.2.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.TableView.setItems(javafx.collections.ObservableList)" because "this.studentTable" is null
at application.DashboardController.RefreshStudentRecord(DashboardController.java:343)
at application.DashboardController.loadData(DashboardController.java:289)
at application.DashboardController.initialize(DashboardController.java:281)
at javafx.fxml#19.0.2.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2662)
... 66 more
For more clarifications
I have DashboardController.java that is implementing Initializable class.
Inside the controller class I have method to implement called initialize.
here is this method
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
// TODO Auto-generated method stub
loadData();
}
In this method I am using loadData() method that is
private void loadData() {
DbConnectivity db = new DbConnectivity();
conn = db.getConnection();
RefreshStudentRecord();
stsno.setCellValueFactory(new PropertyValueFactory<>("stsno"));
stsname.setCellValueFactory(new PropertyValueFactory<>("stsname"));
stsfname.setCellValueFactory(new PropertyValueFactory<>("stsfname"));
stsid.setCellValueFactory(new PropertyValueFactory<>("stsid"));
stscnic.setCellValueFactory(new PropertyValueFactory<>("stscnic"));
stsphone.setCellValueFactory(new PropertyValueFactory<>("stsphone"));
stsaddress.setCellValueFactory(new PropertyValueFactory<>("stsaddress"));
stsclass.setCellValueFactory(new PropertyValueFactory<>("stsclass"));
}
Inside the loadData() method I am calling another method that is
#FXML
public void RefreshStudentRecord() {
studentDataList.clear();
studentquery = "SELECT * FROM studentdata";
try {
studentPst = conn.prepareStatement(studentquery);
studentRst = studentPst.executeQuery();
while(studentRst.next()) {
studentDataList.add(new StudentsDataController(
studentRst.getInt("S_NO"),
studentRst.getString("studentname"),
studentRst.getString("fathername"),
studentRst.getString("studentid"),
studentRst.getString("studentcnic"),
studentRst.getString("phone"),
studentRst.getString("address"),
studentRst.getString("class")
)
);
studentTable.setItems(studentDataList);
}
} catch (SQLException e) {
System.out.println("Exception at Refresing student data: "+e);
e.printStackTrace();
}
}
I think the problem is somewhere thereby, seeing the StackTrace as it is pointing out that the table is null that's why all of the problems have occurred, but I have no clue why the table is null.
Kindly help me to solve this problem
Before setting the table ArrayList, do a check to check if the ArrayList has some data. Also, move your studentTable.setItems outside the loop such that it looks as follows:
while(studentRst.next()) {
studentDataList.add(new StudentsDataController(
studentRst.getInt("S_NO"),
studentRst.getString("studentname"),
studentRst.getString("fathername"),
studentRst.getString("studentid"),
studentRst.getString("studentcnic"),
studentRst.getString("phone"),
studentRst.getString("address"),
studentRst.getString("class")
)
);
}
if(studentDataList.size()>0){
studentTable.setItems(studentDataList);
}
Thanks to all for taking interest in my Question.
Here is how I solved it.
I created a separate class to handle the stuff for the tableView and made this class the controller of the fxml of tableView.
Then everything went right :)

How to use Javafx and scenebuilder to draw on canvas (Group element)

i am new to scenebuilder and fx and need some help.
I have the class Puffer , TestMain and MainViewController.
I tried to paint on the canvas and it worked.I tried Puffer and it worked too.
Now I wanted to use scenebuilder and have my problems.I would like to draw the Group from Puffer on the canvas with a button and don't know what's the best solution.I already tried to modify Puffer without any result.
public class Puffer {
Group root = new Group();
public void draw() {
for (int x = 0; x < 800; x++) {
for (int y = 0; y < 600; y++) {
Complex z0 = new Complex(0.11, 0.123); //!!!!!!!!!!!!! under 1
Complex c = new Complex(x / 400.0 - 2, 1 - y / 400.0);
if (isBounded(iterate(z0, c))) {
drawPoint(x, y, root);
}
}
}
// primaryStage.setScene(new Scene(root, 800, 600));
// primaryStage.show();
}
public void drawPoint(int x, int y, Group root) {
int max = 255;
int min = 0;
int range = max - min + 1;
Line line = new Line(x, y, x, y);
// line.setStroke(Color.rgb((int)(Math.random() * range) + min,(int)(Math.random() * range) + min,(int)(Math.random() * range) + min));
root.getChildren().add(line);
}
public Complex iterate(Complex z0, Complex c) {
Complex zn = z0;
for (int i = 0; i < 20; i++) {
zn.square().add(c);
}
return zn;
}
public boolean isBounded(Complex zn) {
return zn.module() < 2;
}
public class MainViewController {
#FXML
Canvas canvas;
// Event Listener on Button.onAction
#FXML
public void btnOkClicked(ActionEvent event) {
System.out.println("Test");
}
#FXML
public void drawCanvas(){
// GraphicsContext gc = canvas.getGraphicsContext2D();
// gc.setLineWidth(3);
// gc.setStroke(Color.BLACK);
// System.out.println("drawCanvas");
//
// try {
// canvas.setOnMousePressed(event -> {
// System.out.println("Mouse click");
// gc.beginPath();
// gc.lineTo(event.getSceneX(), event.getSceneY());
// gc.stroke();
// });
//
// canvas.setOnMouseDragged(event -> {
// System.out.println("Mouse dragged");
// gc.lineTo(event.getSceneX(), event.getSceneY());
// gc.stroke();
// });
// }catch (Exception e){
// System.out.println(e);
// System.exit(0);
// }
}
public class TestMain extends Application {
#Override
public void start(Stage s1) throws Exception {
try {
Parent root = FXMLLoader.load(getClass().getResource("MainView.fxml"));
Scene scene = new Scene(root);
s1.setTitle("Test");
s1.setScene(scene);
s1.show();
}catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}

Exception in Application constructor, JavaFX [duplicate]

This question already has answers here:
getResourceAsStream returns null
(26 answers)
How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?
(1 answer)
Closed 1 year ago.
first of all, I'm a complete begginer in Java and JavaFX, using IntelliJ Idea for that.
I have tried to download and run a piece of game's code but ran into a problem that I can't pinpoint the cause of.
I have this in my VM options:
--module-path
${PATH_TO_FX};${OUTPUT}
--add-modules
javafx.controls,javafx.fxml
Error message itself:
Exception in Application constructor
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class gg.game.Game
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:891)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:803)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
... 1 more
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.graphics/javafx.scene.image.Image.validateInputStream(Image.java:1142)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:705)
at gg.game/gg.game.Game.<init>(Game.java:19)
... 14 more
Exception running application gg.game.Game
Game's code:
package gg.game;
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.HashMap;
public class Game extends Application {
public static ArrayList<Block> platforms = new ArrayList<>();
private HashMap<KeyCode,Boolean> keys = new HashMap<>();
Image backgroundImg = new Image(getClass().getResourceAsStream("background.png"));
public static final int BLOCK_SIZE = 45;
public static final int MARIO_SIZE = 40;
public static Pane appRoot = new Pane();
public static Pane gameRoot = new Pane();
public Character player;
int levelNumber = 0;
private int levelWidth;
private void initContent(){
ImageView backgroundIV = new ImageView(backgroundImg);
backgroundIV.setFitHeight(14*BLOCK_SIZE);
backgroundIV.setFitWidth(212*BLOCK_SIZE);
levelWidth = LevelData.levels[levelNumber][0].length()*BLOCK_SIZE;
for(int i = 0; i < LevelData.levels[levelNumber].length; i++){
String line = LevelData.levels[levelNumber][i];
for(int j = 0; j < line.length();j++){
switch (line.charAt(j)){
case '0':
break;
case '1':
Block platformFloor = new Block(Block.BlockType.PLATFORM, j * BLOCK_SIZE, i * BLOCK_SIZE);
break;
case '2':
Block brick = new Block(Block.BlockType.BRICK,j*BLOCK_SIZE,i*BLOCK_SIZE);
break;
case '3':
Block bonus = new Block(Block.BlockType.BONUS,j*BLOCK_SIZE,i*BLOCK_SIZE);
break;
case '4':
Block stone = new Block(Block.BlockType.STONE,j * BLOCK_SIZE, i * BLOCK_SIZE);
break;
case '5':
Block PipeTopBlock = new Block(Block.BlockType.PIPE_TOP,j * BLOCK_SIZE, i * BLOCK_SIZE);
break;
case '6':
Block PipeBottomBlock = new Block(Block.BlockType.PIPE_BOTTOM,j * BLOCK_SIZE, i * BLOCK_SIZE);
break;
case '*':
Block InvisibleBlock = new Block(Block.BlockType.INVISIBLE_BLOCK,j * BLOCK_SIZE, i * BLOCK_SIZE);
break;
}
}
}
player =new Character();
player.setTranslateX(0);
player.setTranslateY(400);
player.translateXProperty().addListener((obs,old,newValue)->{
int offset = newValue.intValue();
if(offset>640 && offset<levelWidth-640){
gameRoot.setLayoutX(-(offset-640));
backgroundIV.setLayoutX(-(offset-640));
}
});
gameRoot.getChildren().add(player);
appRoot.getChildren().addAll(backgroundIV,gameRoot);
}
private void update(){
if(isPressed(KeyCode.UP) && player.getTranslateY()>=5){
player.jumpPlayer();
}
if(isPressed(KeyCode.LEFT) && player.getTranslateX()>=5){
player.setScaleX(-1);
player.animation.play();
player.moveX(-5);
}
if(isPressed(KeyCode.RIGHT) && player.getTranslateX()+40 <=levelWidth-5){
player.setScaleX(1);
player.animation.play();
player.moveX(5);
}
if(player.playerVelocity.getY()<10){
player.playerVelocity = player.playerVelocity.add(0,1);
}
player.moveY((int)player.playerVelocity.getY());
}
private boolean isPressed(KeyCode key){
return keys.getOrDefault(key,false);
}
#Override
public void start(Stage primaryStage) throws Exception {
initContent();
Scene scene = new Scene(appRoot,1200,620);
scene.setOnKeyPressed(event-> keys.put(event.getCode(), true));
scene.setOnKeyReleased(event -> {
keys.put(event.getCode(), false);
player.animation.stop();
});
primaryStage.setTitle("Mini Mario");
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
#Override
public void handle(long now) {
update();
}
};
timer.start();
}
public static void main(String[] args) {
launch(args);
}
}
I would be grateful to any kind of response. Thank you.

JavaFx Call another panel and transmits values

My program is structured package and control on the interface as follows
**+ Browser : BrowserController.class**
-#FXML
private SplitPane spHTMLParser;
#FXML
private TextField txtURL;
#FXML
private Button btnSearch;
#FXML
private WebView browser;
**+ Browser/Console : ConsoleController.class**
#FXML
private Label lblURL;
In the browser page when the user clicks the btnSearch,I will show webview site with user input url. When the process is complete I want to show the Console.fxml in Browser.fxml with SplitPane and When the console is displayed, I'd label can get value url . Please help me!
#FXML
void btnSearch(ActionEvent event) {
String url = txtURL.getText().trim();
if (!url.isEmpty()) {
WebEngine webEngine = browser.getEngine();
progressBar.progressProperty().bind(webEngine.getLoadWorker().progressProperty());
webEngine.load(url);
webEngine.getLoadWorker().stateProperty().addListener((ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) -> {
switch (newValue) {
case SUCCEEDED:
if (count++ != 0) {
btnNext.setDisable(false);
}
btnBack.setDisable(false);
progressBar.setVisible(false);
{
try {
FXMLLoader fxmlLoader = new FXMLLoader();
Pane pnConsole = fxmlLoader.load(getClass().getResource("console/Console.fxml").openStream());
spHTMLParser.getItems().add(pnConsole);
spHTMLParser.setDividerPositions(0.5);
} catch (IOException ex) {
Logger.getLogger(BrowserController.class.getName()).log(Level.SEVERE, null, ex);
}
}
break;
}
});
}
}
Load events url successful work exactly but I got an error when trying to call the other pane via fxml
Its error messages with commands
Pane pnConsole = fxmlLoader.load(getClass().getResource("console/Console.fxml").openStream());
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at publishers.controllers.resources.browser.BrowserController.lambda$webView$4(BrowserController.java:144)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:112)
at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
at javafx.scene.web.WebEngine$LoadWorker.updateState(WebEngine.java:1260)
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1371)
at javafx.scene.web.WebEngine$LoadWorker.access$1200(WebEngine.java:1253)
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1240)
at com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2400)
at com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2244)
at com.sun.webkit.network.URLLoader.twkDidFinishLoading(Native Method)
at com.sun.webkit.network.URLLoader.notifyDidFinishLoading(URLLoader.java:838)
at com.sun.webkit.network.URLLoader.lambda$didFinishLoading$96(URLLoader.java:829)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)

Moving marker in a Grid Pane

I have created a Grid Pane with a number of rows. A marker should only be moveable on 1 row. When moving the mouse it should move sideways only, left or right. When you click on the marker it should be locked, no more movement. It should be highlighted with a different color.
How can I achive this?
If you know any tutorials or examples please add, thanks.
Message Output:
Executing
C:\Users\s22380\Desktop\temp\JavaFXApplication9\dist\run269988000\JavaFXApplication9.jar
using platform C:\Program Files\Java\jdk1.8.0_92\jre/bin/java
Exception in Application constructor
java.lang.reflect.InvocationTargetException at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at
com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) at
sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Unable to construct Application
instance: class javafxapplication9.JavaFXApplication9 at
com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745) Caused by:
java.lang.reflect.InvocationTargetException at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at
com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819)
at
com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at
com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method) at
com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at
com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at
com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more Caused by: java.lang.NullPointerException at
sample.Controller.(Controller.java:33) at
javafxapplication9.JavaFXApplication9.(JavaFXApplication9.java:19)
... 13 more Exception running application
javafxapplication9.JavaFXApplication9 Java Result: 1 Deleting
directory
C:\Users\s22380\Desktop\temp\JavaFXApplication9\dist\run269988000
jfxsa-run: BUILD SUCCESSFUL (total time: 1 second)
How about this: Main.java
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application
{
Controller controller = new Controller(10,10);
#Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
loader.setController(this.controller);
Parent root = (Parent)loader.load();
this.controller.InitUi();
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This is basically the sample code when you say new JavaFX project in IntelliJ. I changed it to explicitly set the controller. But that is just personal preference.
The xaml for that
<?import javafx.scene.layout.*?>
<GridPane fx:id="mainGrid" alignment="center" gridLinesVisible="true" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
</children>
</GridPane>
Again. Basically the default stuff.
I also added a style sheet to define the different lock for the not movable state.
redStyle.css
.button {
-fx-text-fill: #006464;
-fx-background-color: #DFB951;
-fx-border-radius: 20;
-fx-background-radius: 20;
-fx-padding: 5;
}
Now for the controller.
It will do a few things:
Listen to the mouse events of the object I want to push around. If the delata is big enough I will move it => if the mouse is moved fast enough
When the button is pressed I will switch out the style sheet to get the different look. Right now this style works on all buttons in the scene. It could be changed to work only on a specific one.
package sample;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.shape.Rectangle;
public class Controller
{
#FXML
private GridPane mainGrid;
#FXML
private Button movable;
private final int sizeX;
private final int sizeY;
private final double minMoveDistanc = 3;
private boolean canMove = true;
private Double lastX = null;
private Double lastY = null;
private String redButtonStyle = Controller.class.getResource("redStyle.css").toExternalForm();
public Controller(int sizeX, int sizeY)
{
this.sizeX = sizeX;
this.sizeY = sizeY;
}
public void InitUi()
{
if (this.mainGrid != null)
{
final int numCols = sizeX;
final int numRows = sizeY;
for (int i = 0; i < numCols; i++)
{
ColumnConstraints colConst = new ColumnConstraints();
this.mainGrid.getColumnConstraints().add(colConst);
}
for (int i = 0; i < numRows; i++)
{
RowConstraints rowConst = new RowConstraints();
this.mainGrid.getRowConstraints().add(rowConst);
}
// add rectangle to keep grid in size
for (int i = 0; i < numCols; i++)
{
for (int j = 0; j < numRows; j++)
{
Rectangle rect = new Rectangle();
rect.setWidth(50);
rect.setHeight(50);
this.mainGrid.add(rect,i,j);
}
}
// ad movable object (Button)
this.movable = new Button("Hallo");
this.movable.setPrefWidth(50);
this.movable.setPrefHeight(50);
this.movable.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent actionEvent)
{
canMove = ! canMove;
movable.setText(canMove? "move" : "stop");
if (canMove)
{
movable.getScene().getStylesheets().remove(redButtonStyle);
}
else
{
movable.getScene().getStylesheets().add(redButtonStyle);
}
}
});
this.mainGrid.add(this.movable,5,5);
}
if (this.movable != null)
{
this.movable.setOnMouseEntered(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent mouseEvent)
{
lastX = null;
lastY = null;
}
});
this.movable.setOnMouseExited(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent mouseEvent)
{
lastX = null;
lastY = null;
}
});
this.movable.setOnMouseMoved(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent mouseEvent)
{
if (!canMove)
{ return; }
double x = mouseEvent.getSceneX();
double y = mouseEvent.getSceneY();
if (lastX == null)
{
lastX = x;
lastY = y;
return;
}
// calculate delta
double deltaX = x - lastX;
double deltaY = y - lastY;
// remember new position
lastX = x;
lastY = y;
boolean moved = false;
// x movement
if (Math.abs(deltaX) > minMoveDistanc)
{
moved = true;
int currentColumn = GridPane.getColumnIndex(movable);
if (deltaX < 0)
{
GridPane.setColumnIndex(movable, Math.max(currentColumn -1 ,0));
}
else
{
GridPane.setColumnIndex(movable, Math.min(currentColumn + 1 ,sizeX-1));
}
}
// y movement
if (Math.abs(deltaY) > minMoveDistanc)
{
moved = true;
int currentRow = GridPane.getRowIndex(movable);
if (deltaY < 0)
{
GridPane.setRowIndex(movable, Math.max(currentRow - 1 ,0));
}
else
{
GridPane.setRowIndex(movable, Math.min(currentRow + 1 ,sizeY-1));
}
}
if (moved)
{
lastX = null;
lastY = null;
}
}
});
}
}
}

Resources