import java.io.IOException; import javafx.application.Application; import javafx.event.EventHandler; import javafx.fxml.FXMLLoader; import javafx.stage.Stage; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; public class Main extends Application {
@Override public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene.fxml")); Parent root = loader.load(); Controller controller = loader.getController(); Scene scene = new Scene(root);
scene.setOnKeyPressed(new EventHandler() { @Override public void handle(KeyEvent event) {
switch(event.getCode()) {
case W: controller.moveUp(); break; case S: controller.moveDown(); break; case A: controller.moveLeft(); break; case D: controller.moveRight(); break; default: break; } } }); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } } package application; public class Controller{
I was losing my mind trying to get a node to take the focus so it would pick up key events. This solution is much better and seems more useful. Thank you.
Hey, I did everything like you, but it kept telling me: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveDown()" because "this.val$controller" is null ... How can i fix that?
I had the same issue. Then I added/removed controller from Scene builder and it stopped bugging. A thing to note since its not explained in any of the videos is when you add a Scene.fxml file and call it with root object (which Bro's doing in all of his FX videos) it needs to have some content for us to be able to attach controller to it and generate fxml code inside. So make sure to add any pane (stage) to it at least and then add a controller (application.Controller under hierarchy section). Then save scene builder and reopen fxml to reflect changes. After that you can compile the code. This part is really annoying with Java. It might help someone else in 2023.
package application;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
public class Main extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Scene.fxml"));
Parent root = loader.load();
Controller controller = loader.getController();
Scene scene = new Scene(root);
scene.setOnKeyPressed(new EventHandler() {
@Override
public void handle(KeyEvent event) {
switch(event.getCode()) {
case W:
controller.moveUp();
break;
case S:
controller.moveDown();
break;
case A:
controller.moveLeft();
break;
case D:
controller.moveRight();
break;
default:
break;
}
}
});
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package application;
public class Controller{
public void moveUp() {
System.out.println("MOVIN' UP!");
}
public void moveDown() {
System.out.println("MOVIN' DOWN!");
}
public void moveLeft() {
System.out.println("MOVIN' LEFT!");
}
public void moveRight() {
System.out.println("MOVIN' RIGHT!");
}
}
May Allah bless you and your knowledge
Bro discord
Can you make one discord server
very nice idea
first yes
Remember me when you get famous xD
I was losing my mind trying to get a node to take the focus so it would pick up key events. This solution is much better and seems more useful. Thank you.
Hey, I did everything like you, but it kept telling me: Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "application.Controller.moveDown()" because "this.val$controller" is null ... How can i fix that?
same here
we didint linked Controller in scenebuilder
I had the same issue. Then I added/removed controller from Scene builder and it stopped bugging. A thing to note since its not explained in any of the videos is when you add a Scene.fxml file and call it with root object (which Bro's doing in all of his FX videos) it needs to have some content for us to be able to attach controller to it and generate fxml code inside. So make sure to add any pane (stage) to it at least and then add a controller (application.Controller under hierarchy section). Then save scene builder and reopen fxml to reflect changes. After that you can compile the code. This part is really annoying with Java. It might help someone else in 2023.
Yes, in all cases it must be linked ٍScene
@@benderbg
I check RUclips every day for you
how to make key events with number keys?
Stuck on it. Need help!!!
you saved my life bro i love you
U r my hero like literally
thank you very much, i finally found what i've been looking for, you're the best
What if i wanted to press 2 keys down? Lets say Ctrl + O?
scene.setOnKeyReleased(event -> {
if(event.getCode().equals(KeyCode.O)) {
if(event.isControlDown()) {
doAnything;
}
}
})
Aweasome!!
Thank you a lot!
this is great. been looking for this for days. Thanks for sharing
Thanks, awesome content.
Nice
Thank you Bro
Nice video man :)
Awesome efforts 👍🔥
Hey bro 🔥 you are amazing 🔥
thanks for watching Vedant!