I help to develop an application with JavaFX for linux and OSX. On Linux, we cannot have the application name on the gnome top bar. We have the entry point for JavaFX. The window have the good name but on gnome we have something like "com.myApp.javaFXMainClass".
I have the same problem with swing and I was able to correct it with these code :
// Set name in system menubar for Gnome (and Linux)
if (System.getProperty("os.name").toLowerCase().contains("linux")) {
try {
Toolkit xToolkit = Toolkit.getDefaultToolkit();
Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, "MyApp");
} catch (Exception e) {
// TODO
}
}
How to do that with JavaFX ?
A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage class is the top-level JavaFX container. The JavaFX Scene class is the container for all content. Example 1-1 creates the stage and scene and makes the scene visible in a given pixel size.
Scene Builder − JavaFX provides an application named Scene Builder. On integrating this application in IDE's such as Eclipse and NetBeans, the users can access a drag and drop design interface, which is used to develop FXML applications (just like Swing Drag & Drop and DreamWeaver Applications).
JavaFX uses a single-threaded rendering design, meaning only a single thread can render anything on the screen, and that is the JavaFX application thread. In fact, only the JavaFX application thread is allowed to make any changes to the JavaFX Scene Graph in general.
package test;
import javafx.application.Preloader;
import javafx.stage.Stage;
public class TestPre extends Preloader {
@Override
public void start(Stage stage) throws Exception {
com.sun.glass.ui.Application.GetApplication().setName("app test");
}
}
package test;
import java.io.ByteArrayInputStream;
import java.util.Base64;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Test __app = new Test();
Stage __stage = new Stage();
__app.start(__stage);
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
String __simage = "iVBORw0K.....";
ByteArrayInputStream __imgstream = new ByteArrayInputStream(Base64.getDecoder().decode(__simage));
javafx.scene.image.Image __image = new javafx.scene.image.Image(__imgstream);
primaryStage.getIcons().add(__image);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
com.sun.javafx.application.LauncherImpl.launchApplication(Test.class, TestPre.class, args);
}
}
This bug has already been reported here but I couldn't get it working so mt final resort was to create an awt
application as a bootstrap to JavaFx
and it's working like charm
Code snippet @gitlab
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Field;
public class CustomJavaFxAppName {
private void display() {
JFrame f = new JFrame("CustomJavaFxAppName");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFXPanel jfxPanel = new JFXPanel() {
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
};
initJFXPanel(jfxPanel);
f.add(jfxPanel);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private void initJFXPanel(JFXPanel jfxPanel) {
Platform.runLater(() -> {
javafx.scene.control.Label label = new javafx.scene.control.Label(
System.getProperty("os.name") + " v"
+ System.getProperty("os.version") + "; Java v"
+ System.getProperty("java.version"));
StackPane root = new StackPane(label);
Scene scene = new Scene(root);
jfxPanel.setScene(scene);
});
if (System.getProperty("os.name").toLowerCase().contains("linux")) {
try {
Toolkit xToolkit = Toolkit.getDefaultToolkit();
Field awtAppClassNameField = xToolkit.getClass().getDeclaredField("awtAppClassName");
awtAppClassNameField.setAccessible(true);
awtAppClassNameField.set(xToolkit, "MyApp");
} catch (Exception ignored) { }
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new CustomJavaFxAppName()::display);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With