Setting stage icon - java

My first intention was to set TextInputDialog icon. But I started from setting stage icon. I saw couple SO questions with marvelous answers containing usually 2 lines of code.
First I tried to put this icon to /resources/icons but exception "Invalid URL or resource not found" appeared. To be sure I don't make any mistake writing file path I moved this icon to /source/sample directory. I use code (I will post whole code):
public void start(Stage stage) throws Exception {
FXMLLoader loaderModyfikacjaKonfiguracji = new FXMLLoader(getClass().getResource("FXMLModyfikacjaKonfiguracji.fxml"));
Parent root = loaderModyfikacjaKonfiguracji.load();
stage.setTitle("Modyfikacja konfiguracji");
Image image = new Image("file:icon.png");
//stage.getIcons().removeAll();
stage.getIcons().add(image);
ControllerModyfikacjaKonfiguracji controllerModyfikacjaKonfiguracji = loaderModyfikacjaKonfiguracji.getController();
stage.setScene(new Scene(root, 510, 700));
stage.show();
}
Everywhere it looks so simple to set icon. I also tried .jpg. not using file: throws exception, using file: compiles but I see no effect of changed icon. What am I doing wrong or where is the problem?

I've successfully used this to set an icon before
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("AppIcon.png")));
In my case, the application fxml file and AppIcon.png are in the same directory.
If you dont want to go that route, i would suggest trying
Image image = new Image("file:./icon.png");
But that's a guess.

Related

JavaFX is not displaying JPG files

For some reason, my application is not showing JPG files within a JavaFX window. It does not matter if I'm trying to display the image inside an ImageViewer node or setting it as a background with the CSS -fx-background-image: url(...); property.
I have tried with different jpg files. It did not work.
I have tried with different image extensions (png, gif). It did work.
I'm using OpenJDK 16+36 (I tested with Hotspot and OpenJ9 JVM), JavaFX 16, my operating system is Linux 5.11.16 and my window manager is GNOME Shell 3.38.4.
PS: Needless to say, the operating system handles JPG images without any problems.
What could be causing this behavior?
Edit 1:
For me, nothing that involves jpeg is working on JavaFX. Wether I try to use jpeg as an background-image, like this very Oracle's sample, or within an ImageViewer, the result is the same (the window opens normally, as if I hadn't put any image). If I replace the file with a different extension, it works perfectly. The funny thing is that jpeg is working without issues in Swing.
Here's one sample of the possible situations in which the issue happens:
/**
* JavaFX Application
*/
#Override
public void start(Stage primaryStage) throws Exception {
// opens a window with the image without issue and prints true
var fileUrl = getClass().getResource("/freemind.png");
var image = new Image(fileUrl.toExternalForm());
var imageViewer = new ImageView(image);
var vbox = new VBox(imageViewer);
var scene = new Scene(vbox);
primaryStage.setScene(scene);
primaryStage.show();
System.out.println(new File(fileUrl.getFile()).exists());
}
But when I replace the fileUrl with another existing jpg file, in the same directory, it opens a blank window, and prints true.
Edit 2:
I found the -Dprism.verbose=true -Djavafx.verbose=true debug flags, which were unknown to me, and they print the problem:
Stack trace here
Some JPEG library, which I am not sure whether it comes embedded in the JavaFX SDK or is related to the operating system is in a different version than the expected. Now, which library is that, I don't know!
rpm -qa | grep -P jpe?g returns:
libjpeg-turbo-2.0.5-5.fc33.i686
libjpeg-turbo-2.0.5-5.fc33.x86_64
libjpeg-turbo-devel-2.0.5-5.fc33.x86_64
mjpegtools-libs-2.1.0-20.fc33.x86_64
openjpeg2-2.3.1-10.fc33.x86_64

JavaFX - Cannot Load Image Inside Project Directory

I am using netbeans. I have a project directory like this:
HTMLEdit/
src/
htmledit/
- pic.png
- MyClass.java
I tried to get the image, but it return null. I had trying both of these but still cannot get it to work:
System.out.println(getClass().getResourceAsStream("/pic.png"));
and
System.out.println(getClass().getResourceAsStream("pic.png"));
What's causing this weird behavior?
EDIT :
It looks like it's because I choosed JAVAFX Project when created the project. I recreate the project by choosing Java Project and it works fine. May be this is Netbeans bug.
getClass().getResourceAsStream() is used for files embedded inside your java jar file. You should use FileInputStream if you need to read a file from your file system as a stream of bytes. Here's the documentation: https://docs.oracle.com/javase/8/docs/api/java/io/FileInputStream.html
When you do getClass().getResourceAsStream("/pic.png")then the url that will be looked to access the file will be an absolute url. The absolute URL is indicated by the slash which is at the front of the resource location.
If you do getClass().getResourceAsStream("pic.png"), then a resource relative to the package where the class resides will be used.
Because you said that both of the getResourceAsStream() statements did not work in Netbeans, I checked the below JavaFX code in Netbeans and it worked perfectly.
public class MyClass extends Application{
#Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Image images = new Image(getClass().getResourceAsStream("pic.png"));
ImageView image = new javafx.scene.image.ImageView(images);
root.getChildren().add(image);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is the structure and the output of the program.
In case if you want to know the Netbeans version, I am using then it is Netbeans 8.0.2.
Also, read the following post.
Different ways of loading a file as an InputStream

Why javafx setResizeable(false) not work?

I have a javafx code like this
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Welcom to MSS Login");
stage.setResizable(false);
stage.show();
}
Here I already setResizable(false); but I still can resize the window when run app. How to set a window not resizeable?
Thanks
I came across this problem today, trying to create some windows for my database management system and I clearly wanted fixed window size, non resizable, but unfortunately this is not fully supported by Ubuntu 16.04 LTS, which seems to have a different approach to how it performs with window size handlers.
However, I found the fix after none of stageStyle("TRANSPARENT"), setResizable(false) did not work.
The second one would normally work on Windows.
The fix for Ubuntu:
primaryStage.setMaxHeight(yourH);
primaryStage.setMinHeight(yourH);
primaryStage.setMaxWidth(yourW);
primaryStage.setMinWidth(yourW);

Switching Scenes with several FXML files

I am new to Java FX and trying to build up an "easy" application which consists of a header button bar and a "content area" below.
I've managed a big part like that:
MainWindow.fxml with a Borderpane: MenuBar in the Top Area
And a Pane with fx:id: content in the center area.
Several X.fxml files for the content ()
One Controller which creates the obj content:
#FXML
private Pane content;
and switches the content:
content.getChildren().clear();
content.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));`
Main file which initializes the parent and scene:
Parent root = FXMLLoader.load(getClass().getResource("MainWindow.fxml"));
Stage Window = primaryStage;
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Window.setTitle("ICIS - In Car Interactive System");
Window.setScene(scene);
Window.show();`
So far everything works fine!
But now I want to apply a SplitPane and do the "content change" in each side of the Split Pane (TwoWindows.fxml): So I've extended the
Controller with obj. for every Pane of the Split Pane, assigned the fx:id to that pane and want to control them analog to the example before.
#FXML
private Pane SecondWindow1;
SecondWindow1.getChildren().add(FXMLLoader.load(getClass().getResource("Navi.fxml")));
Well, during compilation everything is fine, but while running I get the Null error exception, so that I assume SecondWindow1 (left half of SlitPane) is not known to the controller.
I also assume, it is because I initialize at the beginning only MainWindow.fxml (which includes the content area) but not the TwoWindows.fxml (SplitPane) which inlcude the SecondWindow1 Object.
Well I've tried since hours now to solve it, but apparently I am overlooking sth. Somebody knows how to fix that problem? Do I need one Controller for every FXML File?

JavaFX Cannot Find Image

just started a new project and am already running in to some issues. I am trying to display an image with javafx but the compiler is complaining that the provided location is invalid. Here is all of my code so far (apart from some imports):
public class main extends Application{
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception{
Stage stage = primaryStage;
String version = "alpha";
stage.setTitle("Space Invaders v. " + version);
VBox vbox = new VBox();
Image pic = new Image("Art\\whiteMonster.jpg");
ImageView image = new ImageView(pic);
vbox.getChildren().add(image);
Rectangle rect = new Rectangle(20,0,200,200);
rect.setFill(Color.BLACK);
vbox.getChildren().add(rect);
//Creating the scene.
Scene scene = new Scene(vbox);
stage.setScene(scene);
stage.show();
}
}
If I change the file path to the images url where I found it in google image search it displays the black rectangle added beneath it (just to test if everything works) but not the image. In the current configuration however it simply throws an error saying the URL is either invalid or that the resource was not found. What makes me think that this should work is this example here: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html
Thanks for any help.
PS. I've been looking at other similar questions here but haven't found anything that helped me and if you feel like you need any other information give me a shout.
-----EDIT-----
Project Structure, Code and File Manager, all images are placed directly in the "Art" folder.
I hope that is all you need.
Try putting the image inside the folder .Drag the image to the "src" and use Image pic = new Image("whiteMonster.jpg");. It should be displayed under the .java files but a little bit in the left.
The solution for me was to Close Project and open the Javafx Project
again
as shown here
This refreshes the images URL for Javafx.
NOTE: I am using Intellij IDEA Java version 16 with Javafx.
Error was: Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found

Categories