(SOLVED) Java: keyPressed events not firing when game is opened through application class - java

UPDATE: I solved the issue. Solution is in the answers below.
I'm making a game in java, and I want to extract it into a runnable Jar. To make this possible I made this class:
public class AfterglowApp extends Application {
public void start(Stage primaryStage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(AfterglowApp.class.getResource("afterglow.fxml"));
Pane root = fxmlLoader.load();
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(AfterglowApp.class, args);
}
}
In my FXML controller I have a keyListener that triggers on keyPressed for the anchor pane in the FXML file. This works fine when I run it as an FXML application through Eclipse, but when I run it with the application class, no keypresses register (it still registers mouse clicks as normal though).
I have a feeling that the anchor pane of the FXML file loses focus, and that that's what's causing it to not register key presses, but I don't know how to fix that (if that even is the problem).
I realize there's probably something dumb I'm missing, but any help would be much appreciated.

I was correct, it was something stupidly easy. All I had to do was to call
.requestFocusInWindow();
on my anchor pane after the app started. Sorry for wasting the time of everybody who read this. I'll leave the question up in the unlikely event that somebody else gets themselves in a similar situation.

Related

BorderPane does not set the appropriate order of focus for TextFields

I have created the following scene with the help of Scene Builder:
I set up a BorderPane
In the insert TOP area I have placed a MenuBar
In the insert CENTER I have placed some TextFields
In the insert BOTTOM area I have placed a Button
Upon opening my application, the focus is given to the Button that was placed in the insert BOTTOM section.
This is an image depicting this specific scenario
What can I do to set the focus to the first TextField in the insert CENTER section?
I have tried the following in order to solve this issue:
I made sure that the Hierarchy order was correct (i.e. the center
node comes before the bottom node).
I tried using .requestFocus() within my initialize() declaration but that did not work since initialize() occurs before loading the controller as described in JavaFx:What if I want to do something after initialize(),before the scene show,how can I achieve this?.
Another option I looked into was #Uluk Biy's answer in this question: JavaFX: Focusing textfield programmatically. However, in my application I have two controllers (main_controller.java and sample_app_controller). The main_controller.java is responsible for launching the application, meaning that it has the public static void main(String[] args) {launch(args));} and public void start(Stage primaryStage) functions through which my application runs. sample_app_controller is responsible of handling anything that happens in the second screen (the one I provided in this post). I understand that I can place the code provided by #Uluk Biy in my public void start(Stage primaryStage function. However, at that point I am in a different screen which is controlled by a different java file, so I can't really apply that solution.

How can I make a full screen java application?

I have an image viewer appplication. It works perfectly, but I'd like to make a full screen mode for it. It's an FXML project in Netbeans so, the main java is a separated file, therefore I cannot use this:
stage.setFullScreen(true);
because I can't reach the stage from my main .java file.
So I have a file, its name is imageViewer.java, it has this:
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("image_view.fxml"));
Scene scene = new Scene(root);
String css = this.getClass().getResource("style.css").toExternalForm();
scene.getStylesheets().add(css);
stage.setMinHeight(640);
stage.setMinWidth(960);
stage.setScene(scene);
stage.show();
}
and I have another file, it has fxml implements, and the ActionEvent void methods, you know, click the button, next picture etc...
The mentioned method (start method above) it's a method, therefore I can't return the stage. I have to reach the stage from image_viewController.java (the main java), what includes the other methods, functions and FXML implements etc...
I'd like to do this:
public void fullScreen(ActionEvent e) {
stage.setFullScreen(true);
}
But I can't reach the stage from another .java file. This is the first problem. And the second problem.
How can I make position absolute my panes, Vboxes etc like in html??? If my ImageView (what contains my image) get the full width and height, pulls down my HBox (what includes the buttons). If HBox were in absolute position, it wouldn't happened this, would it?
And the final problem, how can I make that, my HBox doesn't appear, just triggered by a hover effect. Is it possible with a separated css file? As I know, it is possible to make with FXML files.
Thanks for the answers!

JavaFX: Working with elements within another class

So the generally Idea I have is having a basic JavaFX app. So I make a new project and it has the default stuff such as
#Override
public void start(Stage stage) throws Exception
{
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
and it has the basic button that when you click it prints hello world. So so far all i am trying to do is show you it is a basic default project.
now lets assume I have another class. Lets say that class runs on another thread and that when I type in a name into the console (in my case NetBeans) then it will update the button to say something else.
This is very general, I been googling and trying things with no perfect result. In hindsight, I am trying to make it so, I can have other classes edit the FXML document or elements on the page, then it will show on my program. I am not sure if I have to reload the FXML doc or reload the stage or what. I just wnat a guide or help on how to reference and change stuff around on a JavaFX FXML program. I been working on a chat program and been using normal Javafx with NO FXML and run into problems. So I am thinking about doing it on FXML. but I have times where I a class handle the user names and a class handle the messages for everyone to read and I need these classes to be able to edit the FXML document and reload it to show up. SO, if i user connects to the server, for example, the ListView object will now show that person name. like I said before, I have been able to do this working with Javafx without an FXML file, but my code is like spaghetti. Thanks in advance

JavaFX 2.2 Stage always on top

I have a normal JFrame (one part of the app) and a second JavaFX window (I can't use a JFrame for the JavaFX stage). The problem is that the JavaFX window should always be on top of all other windows.
I have no idea how I should solve this problem! Any ideas?
I know this is a old thread, but things keep on changing. Coming to JDK 8u20 is a new method primaryStage.setAlwaysOnTop(true);
This would be the easiest way to make a stage always on top. For early access to 8u20 visit the website.
public class KeyholeDemo extends Application {
#Override public void start(Stage primaryStage) {
primaryStage.initStyle(StageStyle.TRANSPARENT);
primaryStage.setAlwaysOnTop(true);
// code omitted...
}
public static void main(String[] args) {
launch(args);
}
}
Sample code taken from this nice writeup
I have a similar problem right now.
I use this line of code to get an always on top effect:
stage.initModality(Modality.APPLICATION_MODAL);
It works fine for me.
Here is the doc.
AFAIK, there is no API to have JavaFX stage always on top. But you can put JavaFX scene inside JFrame by using JFXPanel.
Like this (I'm using Alert)
Stage stage = (Stage) alert.getDialogPane().getScene().getWindow();
stage.setAlwaysOnTop(true);
If you want it on top always blocking other windows, just set:
alert.initModality(Modality.APPLICATION_MODAL);

Java: Embed heavyweight components in lightweight (JavaFX SwingNode)

I have been developing a JavaFX application which I need to embed a Applet into. This question has already been asked; however the answer given on that question is not sufficient. I do not have access to the applet, I am simply downloading it off a server (game server in this case) and trying to run it on the desktop as my own custom client. So I cannot change anything on that side. I can easily do this using swing alone, but not when embedding into a SwingNode on a JavaFX application. I have spent days reading up about it and I understand the problems with not being able to put heavyweight components into a SwingNode and rely on having them work. However with anything when there's a will there is always a way.
The code I am current using:
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("GameClient");
//Game launcher
Launcher launcher = new Launcher();
SwingNode swingNode = new SwingNode();
SwingUtilities.invokeLater(() -> swingNode.setContent(launcher.getGameClientWrappedInJPanel()));
//HBox because I also want to add another component off to the side of the game
Scene scene = new Scene(new HBox());
((HBox)scene.getRoot()).getChildren().add(swingNode);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
Also, all advice is welcomed but keep in mind that this is all a testing set up; so things like the launcher downloading and initializing the client in the constructor is just a temporary work around.
So long story short, I was wondering if anyone would be able to give me any advice on how to handle this. (Basically a hack for properly rendering heavyweight in lighweight components) I have considered writing the overall gui in Swing and just using JFXPanels for the FX components, but if there is any hack I could use for my original goal; it would be a lot more convenient.
Thank you
-Thomas

Resources