I've started a class on Java, and we've gotten to JavaFX. I'm having trouble getting an image to display. After a little debugging, I was able to narrow it down to the following exception:
com.sun.javafx.iio.ImageStorageException: No loader for image data
I know the path name is correct, as that was the first error/exception I was able to fix. The current GUI shows up blank, as if I've put nothing in the scene. I can add other aspects (such as labels), but the image never loads.
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Image Belial = new Image("file:C:\\Users\\jakem\\Desktop\\D&D Stuff\\Belial.jpg");
System.out.println("Image loaded? " + !Belial.isError());
if (Belial.isError()) {
System.out.println(Belial.getException());
}
ImageView imageView = new ImageView(Belial);
HBox hbox = new HBox(imageView);
Scene scene = new Scene(hbox);
primaryStage.setScene(scene);
primaryStage.setTitle("Belial");
primaryStage.show();
}
These are the modules I've loaded, along with adding the JavaFX library to my project library.
--module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
I'm using Intellij.
EDIT: As it turns out, my image file was not actually in jpg format despite having a jpg extension. Switched to a properly formatted jpg and it worked fine.
If I add some media (pictures and/or sounds) my JavaFX artifact won't launch.
Im using macOS and IntelliJ. Launching the .jar from Terminal returns "Error: Could not find or load main class". However, this only occurs with pictures I stored in variables but haven't included yet (for ex. a PlayerIcon) so it isn't a manifest issue.
I installed Java 8 because I got so many issues with Java 10, 11 and 12. Inside of IntelliJ the project launches normally, just the exported .jar is affected of the issue. I am new to JavaFX and I did not include a FXML file. Maybe that's the issue?
Heres a code snippet of how I included one image:
Image Scoreboard = new Image(getClass().getResourceAsStream("Scoreboard.png"));
ScoreBoardContainer.setImage(Scoreboard);
Group root = new Group();
root.getChildren().add(ScoreBoardContainer);
Scene GameUI = new Scene(root, w, h);
Thanks for your help!
Longer code snippet:
package sample;
import ...
public class Main extends Application {
...variables...
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
final ImageView HintergrundMuster = new ImageView();
final ImageView ScoreBoardContainer = new ImageView();
Image HG = new Image(getClass().getResourceAsStream("space-background.png"));
Image PlanetBraun = new Image(getClass().getResourceAsStream("Planet.png"));
Image Scoreboard = new Image(getClass().getResourceAsStream("Scoreboard.png"));
HintergrundMuster.setImage(HG);
ScoreBoardContainer.setImage(Scoreboard);
AudioClip SprungSound = new AudioClip(new File("jump.mp3").toURI().toString());
SprungSound.play(0);
Canvas canvas = new Canvas(b,h);
Vordergrund = canvas.getGraphicsContext2D();
Group root = new Group();
root.getChildren().add(HintergrundMuster);
root.getChildren().add(canvas);
root.getChildren().add(ScoreBoardContainer);
Scene GameUI = new Scene(root, b, h);
primaryStage.setTitle(„Test“);
primaryStage.setScene(GameUI);
primaryStage.show();
...
I just found the solution to my problem. Thanks for your help anyway!
I overlooked that some of my files had the .PNG format while others had .png. For IntelliJ, this doesn't matter, but for the JAR compiler it does indeed matter apparently.
Also, I had a problem with my audio files so I copied them into my "sample" folder and changed the code a little bit. Following snipped worked for me:
AudioClip SprungSound = new AudioClip(this.getClass().getResource("jump.mp3").toString());
The issue I'm having is that the mxGraph object does not load correctly on startup. It requires me to click a cell inside of it for it to appear, and even then it does not reveal the entire graph, just the cell that was clicked. In order to reveal all of it I have to refresh the graph via another control or drag a cell around the entire graph area.
When I initially developed this I was working with Java 8 and this was not an issue. This has only occurred since updating to Java 11 (OpenJDK). Everything else was kept the same when upgrading to 11, only the updated dependencies changed.
I am wrapping the mxGraphComponent inside of a SwingNode in order to place it inside of a JavaFX node. I've had issues in the past with Swing nodes inside JavaFX but I am creating all of the Swing components using the SwingUtilities.invokeLater() method. I am using the 3.9.8.1 version of JGraphX from Maven, but I have also tried the updated 4.0.0 from GitHub with no success.
Here's my MCVE:
public final class Main {
public static void main(final String[] args) {
Application.launch(JGraphExample.class);
}
}
public final class JGraphExample extends Application {
private mxGraph graph;
#Override
public void start(final Stage primaryStage) {
final SwingNode value = new SwingNode();
final BorderPane root = new BorderPane();
root.setCenter(value);
root.setBottom(createRefreshButton());
SwingUtilities.invokeLater(() -> value.setContent(buildGraphComponent()));
primaryStage.setWidth(500);
primaryStage.setHeight(500);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
private mxGraphComponent buildGraphComponent() {
graph = buildGraph();
return new mxGraphComponent(graph);
}
private mxGraph buildGraph() {
final mxGraph graph = new mxGraph();
graph.insertVertex(graph.getDefaultParent(), "x", "Hello", 100, 100, 100, 100);
return graph;
}
private Button createRefreshButton() {
final Button refresh = new Button("Refresh");
refresh.setOnAction(actionEvent -> graph.refresh());
return refresh;
}
}
Until clicking the refresh button the graph will not render correctly. This was not the case with Java 8 as it worked as intended. It seems the update to 11 has teased this issue out.
Has anyone come across this before or have any ideas? Thanks in advance.
I was trying to display a picture in a box with JavaFX. I followed methods documented on Oracle, but it still did not work, though it was extremely similar to the example shown on Oracle. My code is here:
public class TesterJavaFX extends Application {
#Override
public void start(Stage primaryStage) {
Image img = new Image("character.png");
ImageView imgview = new ImageView();
imgview.setImage(img);
imgview.setFitWidth(100);
imgview.setPreserveRatio(true);
imgview.setSmooth(true);
imgview.setCache(true);
HBox box = new HBox();
box.getChildren().add(imgview);
StackPane root = new StackPane();
root.getChildren().add(box);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
I have a file called rocket.png in the "src" directory. It even shows up on the IDE. But Java causes a illegal argument exception, and i don't know why. Can anybody help me? Thanks.
Note: The imports are all present.
The string passed to the Image constructor is a URL. If the image file is in the root of your classpath, the following should work:
Image img = new Image(getClass().getResource("/character.png").toExternalForm());
I wanted to know how should I set icons on javaFX stage.
I have found this method, but it did not work properly.
stage.getIcons().add(new Image(iconImagePath));
stage is an instance of javafx.stage.Stage, and I have imported javafx.scene.image.Image.
This is the exception which we receive:
Invalid URL: Invalid URL or resource not found
Also, there is nothing wrong with the iconImagePath, its value is "G:/test.jpg"
and there is a jpg file in the G drive named test. In addition, when we use ImageIO to read the same URL we can do it easily.
stage.getIcons().add(new Image(getClass().getResourceAsStream("bal.png")));
This example works. I placed an icon in the same folder/package as the source .java file.
Directory structure
The constructors of javafx.scene.image.Image expect a URI, not a (full) path. This URI can either be relative (e.g. /images/flower.png) or absolute (e.g. file:flower.png).
Strings like G:/test.jpg are no valid URLs and hence illegal.
Try file:g:/test.jpg instead.
Usually, the icons should be bundled with your application, so simply put the image file into your classpath (e.g. if you're using eclipse, put it into your 'src' directory) and use it like that:
stage.getIcons().add(new Image("/logo.jpg"));
use
stage.getIcons().add(new Image(("file:logo.png")));
and put the image logo.png in root of your project ( at same directory where src )
Best Way:
stage.getIcons().add(new Image(getClass().getResource(IconImagePath).toExternalForm()));
don't forget that your icon must be in 32x32 or 16x16 resolution, if not, it doesn't work.
// Set the icon
stage.getIcons().add(new Image(getClass().getResourceAsStream("penguin.png")));
I faced the same problem. I used Netbeans. I'm not sure if the folder structure is different for other IDEs, but I had to put the picture in /build/classes/(package that contains the JavaFX class file). This means it doesn't go into the src folder.
Here is the working code, which is exactly what you neened:
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;
/**
*
* #author Manikant gautam
* This is a beginner's sample application
* using JAVAFX
*
*/
public class Helloworld 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) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
// Set Icon From Here.
primaryStage.getIcons().add(
new Image("/resource/graphics/app_logo.png"));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Set Icon by statement:
primaryStage.getIcons().add(new Image("/resource/graphics/app_logo.png"));
If you're using eclipse make sure you add the folder that has the image to the build path. this way you can refer to the image with its name with no problems.
This is what I've done and it work. The image is located in the root of my resource folder.
stage.getIcons().add(new Image("/ubuntu-mini.png"));
I am using JavaFX 8
I use netbeans 8.2, if I use :
stage.getIcons().addAll(new Image(getClass().getResourceAsStream("home-icon32.png")));
I have to put the image in src directory. Don't know why, but works only this way. I've tried putting it in build/classes, but negative.
The solution is:
File f = new File("image.png");
Image ix = new Image(f.toURI().toString());
stage.getIcons().add(ix);
public class Main extends Application
{
private static final Logger LOGGER = Logger.getLogger(Main.class);
#Override
public void start(Stage primaryStage)
{
try
{
// BorderPane root = new BorderPane();
BorderPane root = (BorderPane) FXMLLoader
.load(getClass().getResource("/org/geeksynergy/view/layout/FrontPageBorder.fxml"));
root.setAccessibleText("good");
Scene scene = new Scene(root, 400, 400);
scene.getStylesheets().add(getClass()
.getResource("/org/geeksynergy/view/cssstyle/application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("AiRJuke");
primaryStage.getIcons().add(new Image("/org/geeksymergy/resource/images/download.png"));
primaryStage.show();
AnchorPane personOverview = (AnchorPane) FXMLLoader
.load(getClass().getResource("/org/geeksynergy/view/layout/Ui.fxml"));
root.setCenter(personOverview);
// added this line to save the playlist , when we close
// application window
Platform.setImplicitExit(false);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>()
{
public void handle(WindowEvent event)
{
M3UPlayList.defaultSavePlaylist();
Platform.setImplicitExit(true);
primaryStage.hide();
}
});
} catch (Exception e)
{
LOGGER.error("Exception while loding application", e);
}
}
public static void main(String[] args)
{
launch(args);
}
}