I just started (yesterday) developing Android apps through JavaFX, I'm on Eclipse using Gluon and it's being very weird.
I built the apk to the sample program provided by Gluon which is just a Label inside of a StackPane saying "Hello JavaFX World!" and thats it, it loads completely perfect on Android and on Desktop.
Now, I tried adding one button and it works perfectly fine on desktop but when I compile it to an apk and try to run it, it's just a black screen and I've tried waiting 10 minutes for it to load but nothing.
This is the code
package com.zach.csp;
import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class APCS extends Application {
#Override
public void start(Stage stage) {
StackPane root = new StackPane(new Label("Hello JavaFX World!"));
Rectangle2D visualBounds = Screen.getPrimary().getVisualBounds();
Scene scene = new Scene(root, visualBounds.getWidth(), visualBounds.getHeight());
//Button b = new Button("sadsa");
//root.getChildren().add(root);
stage.getIcons().add(new Image(APCS.class.getResourceAsStream("/icon.png")));
stage.setScene(scene);
stage.show();
}
}
It's got to be the button too, because after I commented it out in the code above the program worked perfectly on my phone. Are buttons not supported on android or something? Any help is appreciated.
Related
I have been encountering a problem with Eclipse IDE for quite a while.Whenever I try to create a new class inside a project and run it, another class of the same or different project runs instead.When I try running the Circle class shown below another class called Main runs. I tried fixing the issue by clicking Run As---->Run Configurations but could not find an item of this class under Java Application.I also tried deleting the main method of the other classes that run instead of the Circle1 class, but it did not work
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Circle1 extends Application {
#Override
public void start(Stage PrimaryStage) {
StackPane pane = new StackPane();
Circle circle = new Circle(40);
circle.setStroke(new Color(0.7,0.5,0.6,0.76));
circle.setFill(Color.BLACK);
pane.getChildren().add(circle);
HBox hBox = new HBox();
hBox.setSpacing(10);
hBox.setAlignment(Pos.CENTER);
Button btEnlarge = new Button("Enlarge");
Button btShrink = new Button("Shrink");
hBox.getChildren().addAll(btEnlarge,btShrink);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(hBox);
BorderPane.setAlignment(hBox, Pos.CENTER);
Scene scene = new Scene (borderPane,200,150);
PrimaryStage.setTitle("ControlCircle");
PrimaryStage.setScene(scene);
PrimaryStage.show();
}
public void main(String[]args) {
launch(args);
}
}
I tried fixing the issue by clicking Run As---->Run Configurations but could not find an item of this class under Java Application
So , you probably have to create a java application for your project by clicking java application -> New_configuration -> Give a name to the configuration -> browse the project from your workspace and search for your main class . Eclipse should find it for you automatically .
Then , to run this specific configuration press the drop down option from the Run button in Eclipse and select the name you just gave . Also the last configuration you run in eclipse is saved ,so to run a different project you need to select it.
.I also tried deleting the main method of the other classes that run instead of the Circle1 class, but it did not work
In my experience each java or javafx applications has one main method ,which launches your scene in your example . Every other class you create in your project shouldn't have a main method .
In order to create a class for your project :
right click on the src folder of your Java project. In the menu that pops up, open the submenu New ,Eclipse will then show you a dialog for new class creation ,give your class a name and it should look like this:
package com.yourpackage
public class Circle{
}
I've just tried to run a rudimentary example using OpenJFX 16 that is intended to open a webpage of a WebGL demo showing some blobs:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
#Override
public void start(final Stage primaryStage) {
primaryStage.setTitle("JavaFX WebView Example");
WebView webView = new WebView();
webView.getEngine().load("https://webglsamples.org/blob/blob.html");
VBox vBox = new VBox(webView);
Scene scene = new Scene(vBox, 960, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}
However, I get the following error message:
This page requires a browser that supports WebGL. Click here to
upgrade your browser.
I use Adoptium OpenJDK 16 under Mageia Linux. Is it the expected behaviour? I'm disappointed because a member of Gluon's support team told me:
there are some WebGL demos that run just fine in the WebView component
What can I do to solve this problem? Is it expected to work only with OpenJFX 17?
Edit.: Someone else tried with OpenJFX 18, it still doesn't work:
https://jvm-gaming.org/t/openjfx-javafx-webview-not-working-with-webgl/70495/2
JavaFXs WebView does not support WebGL and I am not aware of any plans to change that. Who told you that there are working demos for that and where can they be found? There have only been some experiments to resolve this issue but no official solution yet. E.g., https://github.com/miho/NativeFX
Here's how my scene builder looks like:
and here's the GUI:
The standalone scene builder:
I just run the following source code from Java SDK demos:
package sample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Button btn = new Button();
btn.setText("Say 'Hello World'!");
StackPane root_ctn = new StackPane();
root_ctn.getChildren().add(btn);
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
System.out.println("Hello World!");
}
});
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root_ctn, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The only place the text looks good is in the console.
View idea.log
I did not yet find out the solution, but I found an interesting pattern:
On your Gluon Scene Builder screenshot, there is written Pgy Rtqlgev, where it should be New Project, and Qrgp Rtqlgev where it should be Open Project. Note that every letter is substituted by the after next letter in the alphabet!
The same applies to Say 'Hello World'!, which is "translated" to Lc{ 'Jgrrq Yqtrf'!. Note that the letter y is replaced by a {, which comes two positions after y in the ASCII table. Interestingly, the characters ' and ! stay the same..
The space each letter takes is still the space of the correct letter, as you can see in the following graphic with the correct text on the green background:
Update:
Is it possible that the font "Segoe UI" is missing or flawed on your system? Can you use that font for example in Word?
Update: I found two other questions of users facing the same problem. In both cases the problem seems to be related to the Segoe UI font:
Scene Builder Editor displaying weird characters
JavaFX Scene builder shows wired characters
I have also encountered this problem and after reading many forums I think I have a possible explanation and solution. The problem seems to be related to Mac users and Segoe UI;
I am guessing that because the font is used in Microsoft products, Macs are unable to render the font, even downloaded versions have not worked.
The simplest fix, which has worked for me so far, is to include
style="-fx-font-family: serif"
in the root node or add it in the controller or add
.root{
-fx-font-family: serif
}
to your CSS. This works for any font in your system.
Installing Segoe UI was a huge red herring for me. Instead, I changed the version of javafx defined in build.gradle to 17.0.1 and upgraded JavaFX to 16
I try to use JavaFX the first time. I created a project an a *.fxml file in IntelliJ. Then I opend the built in fxml gui editor in IntelliJ. I got a warning, that I need to add a SDK for JavaFX. I went to project structure settings, Modules, Dependencies and added "IntelliJ-dir\jre64\lib\ext\jfxrt.jar". The warning disappeared. The module scope is set to compile. If I try to execute the following code by clicking the play button in IntelliJ next to the class declaration I get "Error: JavaFX runtime components are missing, and are required to run this application"
I don't know what to do now. I think I added the library as inteded but I can't run my Hello World window.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class splash extends Application {
private Button button;
#Override
public void start(Stage stage) throws Exception {
stage.setTitle("Hello World!");
button = new Button("Hi");
StackPane layout = new StackPane();
layout.getChildren().add(button);
Scene scene = new Scene(layout, 300, 250);
stage.setScene(scene);
}
}
You probably forgot to change the JDK of your builder in IntelliJ!
Open the settings and follow the path as bellow:
FOR MAVEN
FOR GRADLE
I recently asked a similar question about this for OSX and found a solution using the com.sun.glass package. However, this solution does not seem to work on X11-based desktop environments.
The issue:
I am trying to write a borderless window overlay that can be placed above ALL other desktop components, including the dock and menubar of any given Linux desktop environment which uses x11. Currently, when I use AlwaysOnTop(true) with my JavaFX window, the window is placed above all other windows but is NOT placed above the window managers UI (taskbar) due to it having a higher window level. The screenshot below shows what happens when using AlwaysOnTop: the entirety of the vertical white window should be placed above the taskbar but is instead forced below it. See screenshot:
There is a solution for this issue with Qt through using the x11bypasswindowmanager window flag, so I figured it must be possible through Java!
The only current solution I have is to use wmctrl directly through the commandline using a subprocess. This is not really suitable, as a lot of linux systems to not come with wmctrl installed.
Below is the code snippet I am using to generate the window in the above screenshot:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 200, 3000));
primaryStage.setAlwaysOnTop(true);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setX(800);
primaryStage.setY(0);
primaryStage.show();
}
}