TL;DR : Solution : Increase the JavaFX version to 15.0.1.
I am re-writing the question I asked yesterday because it was poorly formulated and poorly explained.
What I do : I use JavaFX to create a Media and a MediaView to render a .mp4 video in a scene.
What happens : The screen stays blank.
What should happen : The video should be rendered properly and visible by the user.
What I've tried :
Changing the file encoding (from H.264 to QuickTime (outputs as .mov)).
Result : QuickTime encoding isn't recognized by JavaFX.
Changing the FPS value from 30 to 60 and from 60 to 30.
Result : no difference.
Tweaking the file size by shortening the video.
Result : no difference.
Changing the video resolution scale from 16:9 to 16:10.
Result : no difference.
Changing the video resolution value from 2560x1440 to 1920x1080.
Result : the video is shown on the screen, but I need a 2560x1440 video to fill the screen. I will take care of different resolutions later on by myself.
Using different videos from my computer.
Result : resolutions less or equal to 1920x1080 are working fine. I tried video a different video with a 2560x1440 resolution and it does not work.
Using a 2560x1440 video referenced by an internet URL.
Result : same behavior as described above.
My code :
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.stage.Stage;
import java.io.File;
public class Application extends javafx.application.Application {
Stage window;
#Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setWidth(2560);
window.setHeight(1440);
window.setX(0);
window.setY(0);
window.setResizable(false);
window.setFullScreen(true);
window.setFullScreenExitHint("");
Pane pane = new Pane();
// Example to show that adding a simple figure to the pane works fine.
javafx.scene.shape.Rectangle r = new javafx.scene.shape.Rectangle(0, 0, 150, 150);
Media media = new Media(new File(/*Insert file name you own for testing purposes*/).toURI().toString());
// The path I would use : "src\\main\\resources\\img\\Animated Titlescreen Background.mp4".
// This is obtained using other classes and methods that read the computer directories,
// so it works fine across different computers.
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setAutoPlay(true);
MediaView mediaView = new MediaView(mediaPlayer);
pane.getChildren().addAll(mediaView, r);
Scene scene = new Scene(pane, 2560, 1440);
window.setScene(scene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
After reading more on this link, it says Windows 8 increase H.264 decoder resolution to 4096x2304, which is available on JavaFX version 15.0.1. I was using version 12.0.1 because of a critical issue on Linux with version 15.0.1.
Solution : Increase the JavaFX version to 15.0.1.
I'm trying to create a simple data entrty application with javafx and ran into a problem when adding a scene control. The display loses it's fill colour even BEFORE i've added the control to the scene! Simply instantiating the control breaks it.
I was running on Oracle java 8 on windows, but I;ve tried OpenJDK 8 on Windows and OpenJDk/OpenJFX 13 on linux and all behave identically. I stripped out the code to the bear minmium to recreate the problem.
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class BasicTest extends Application {
#Override
public void start(Stage primaryStage) {
Rectangle r = new Rectangle();
r.setWidth(200);
r.setHeight(50);
r.setFill(Color.BLUE);
r.setStroke(Color.WHITE);
r.setStrokeWidth(2);
Text t = new Text();
t.setText("Confirm");
t.setFill(Color.WHITE);
t.setFont(Font.font("null", 40));
StackPane sp = new StackPane(r);
sp.getChildren().add(t);
sp.setMaxWidth(200);
t.setTranslateY(-2);
Label b = new Label("Click me");//Comment this line out after first run
Scene scene = new Scene(sp, 300, 200);
scene.setFill(Color.BLUE);
primaryStage.setScene(scene);
primaryStage.show();
}
}
With the label commented out the scene background is blue so I get a white "Confirm" with white outline. Just adding the label constructor will make the scene background go grey.
Thanks for all the comments. Many apologies if my wording was confusing. I did add some images to show the difference but they seem to have disappeared. Anyway my thanks to #Matt for their very succinct description (which I wish I'd have thought of!). I like the mouse click idea too.
I was probably being very naughty asking this "question" on SO when I firmly believe this is a bug in javafx. Instantiating an object that goes no where near (yet) the scene graph should have no effect on it in my opinion - CSS or no CSS. I will raise a bug against javafx.
However I knew the power of SO would help me and it has!
Thanks to the clue by #jewelsea I simply replaced my scene.setFill() (which was only a test anyway) with CSS and the problem is circumvented. I can even add the control to the scene now and it works as expected. From the JavaFX CSS Reference:
"The Scene object has no settable CSS properties, nor does it have any pseudo-classes. However, the root node of the scene is assigned the style class "root" (in addition to style classes already assigned to the node). "
So I set up in my css:
.root {
-fx-background-color: blue;
}
Another way to circumvent this is to simply set the background of the StackPane to BLUE:
sp.setBackground(new Background(new BackgroundFill(Color.BLUE, null, null)));
This question already has answers here:
I took this code straight out of 'Java all in one for Dummies' ....why doesn't it work?
(2 answers)
Closed 8 years ago.
So, I do not know why my image is not showing because I put the image in the right folder and I believe that the code below is right. The result of this code is just a blank white frame background. I thought the image was corrupted but I tried a different image and its the same result. Anyway I will be highly grateful if anyone could solve this.
package fia_project;
import java.awt.Color;
java.awt.*;
import javax.swing.*;
public class Fia_test extends JFrame {
public Frame kenny;
public Fia_test(){
super("");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(true);
setSize(350, 100);
Container pane = getContentPane();
getContentPane().setBackground(Color.WHITE);
ImageIcon icon = new ImageIcon("speaker.png");
JLabel label2 = new JLabel(icon);
add(label2);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Fia_test t = new Fia_test();
}
}
Use ImageIO.read instead of ImageIcon as it will generate an error if the image can not be loaded, much more useful for diagnostics. See Reading/Loading an Image
ImageIcon(String) expects that the specified String represents a file on the file system. From the looks of you code, it is possible that you've placed the image within the source directory of your code.
Assuming that the image has been bundled into the resulting Jar file, you will no longer be able to access the image as if it was a file on the file system, but will need to use Class#getResource to load it...
For example...
BufferedImage img = ImageIO.read(getClass().getResource("speaker.png"));
ImageIcon icon = new ImageIcon(img);
Assuming that speaker.png is in the same directory as the class file.
I am creating an windows desktop swt application.
I need to change the frame icon, for that I used
frame.setIconImage((new ImageIcon("C:\\Documents and Settings\\arjuns\\Desktop\\logo1 copy.png")).getImage());
The icon is displaying when I manually run the code from eclipse, but when I create an installer using Install4j the icon is not appearing.
Can anyone please help me.
URL url = ClassLoader.getSystemResource("ressources/logo.png");
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.createImage(url);
setIconImage(img);
This is similar to the previous answer, but I need to add a bit of information.
You can still use a direct path to your image (C:/User/logo.png) BUT imagine you give your program to someone else, he wont have the image in that specific path.
So I recemmend you insert it in your project like so:
(I usualy do a sperate package for any ressources).
so it will become ressources/logo.png and it will work for anybody opening your project.
import java.awt.*;
import javax.swing.*;
class set extends JFrame
{
set()
{
setSize(100,100);
setVisible(true);
setIconImage(new ImageIcon("navbit-home.png").getImage());
}
public static void main (String[] args) {
new set();
}
}
please set appropriate path.like C:/Documents and Settings/arjuns/Desktop/logo1copy.png
The image should be available in the JAR file you create. Then use getResource() to get the image from the jar file.
For example,
URL resource = this.getClass().getResource("resources/logo.png");
frame.setIconImage(new ImageIcon(resource).getImage());
Here the logo.png is located under 'resources' folder of the class file where this code is executed.
Is it possible to change the application icon using JavaFX, or does it have to be done using Swing?
Assuming your stage is "stage" and the file is on the filesystem:
stage.getIcons().add(new Image("file:icon.png"));
As per the comment below, if it's wrapped in a containing jar you'll need to use the following approach instead:
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("icon.png")));
Full program for starters :) This program sets icon for StackOverflowIcon.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class StackoverflowIcon extends Application {
#Override
public void start(Stage stage) {
StackPane root = new StackPane();
// set icon
stage.getIcons().add(new Image("/path/to/stackoverflow.jpg"));
stage.setTitle("Wow!! Stackoverflow Icon");
stage.setScene(new Scene(root, 300, 250));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Output Screnshot
Updated for JavaFX 8
No need to change the code. It still works fine. Tested and verified in Java 1.8(1.8.0_45). Path can be set to local or remote both are supported.
stage.getIcons().add(new Image("/path/to/javaicon.png"));
OR
stage.getIcons().add(new Image("https://example.com/javaicon.png"));
Hope it helps. Thanks!!
I tried this and it totally works. The code is:
stage.getIcons().add(
new Image(
<yourclassname>.class.getResourceAsStream( "icon.png" )));
icon.png is under the same folder as the source files.
If you have have a images folder and the icon is saved in that use this
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/images/comparison.png")));
and if you are directly using it from your package which is not a good practice use this
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("comparison.png")));
and if you have a folder structure and you have your icon inside that use
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("../images/comparison.png")));
you can add it in fxml. Stage level
<icons>
<Image url="#../../../my_icon.png"/>
</icons>
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png")));
If your icon.png is in resources dir and remember to put a '/' before otherwise it will not work
What do you think about creating new package i.e image.icons in your src directory and moving there you .png images? Than you just need to write:
Image image = new Image("/image/icons/nameOfImage.png");
primaryStage.getIcons().add(image);
This solution works for me perfectly, but still I'm not sure if it's correct (beginner here).
stage.getIcons().add(new Image(ClassLoader.getSystemResourceAsStream("images/icon.png")));
images folder need to be in Resource folder.
stage.getIcons().add(new Image(<yourclassname>.class.getResourceAsStream("/icon.png" )));
You can add more than one icon with different sizes using this method.The images should be different sizes of the same image and the best size will be chosen.
eg. 16x16, 32,32
You can easily put icon to your application using this code line
stage.getIcons().add(new Image("image path") );
stage.getIcons().add(new Image("/images/logo_only.png"));
It is good habit to make images folder in your src folder and get images from it.
I used this in my application
Image icon = new Image(getClass().getResourceAsStream("icon.png"));
window.getIcons().add(icon);
Here window is the stage.
If you run the jar file, the code specified by Michael Berry will change the icon in the title bar and in the taskbar. Shortcut icon cannot be changed.
If you run a native program compiled with com.zenjava, You must add a link to the program icon:
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
...
<bundleArguments>
<icon>${project.basedir}/src/main/resources/images/filename.ico</icon>
</bundleArguments>
</configuration>
</plugin>
This will add an icon to the shortcut and taskbar.
Toggle icons in runtime:
In addition to the responses here, I found that once you have assigned an Icon to your application by the first time you cannot toggle it by just adding a new icon to your stage (this would be helpful if you need to toggle the icon of your app from on/off enabled/disabled).
To set a new icon during run time use the getIcons().remove(0) before trying to add a new icon, where 0 is the index of the icon you want to override like is shown here:
//Setting icon by first time (You can do this on your start method).
stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));
//Overriding app icon with a new status (This can be in another method)
stage.getIcons().remove(0);
stage.getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));
To access the stage from other methods or classes you can create a new static field for stage in you main class so can access it from out of the start() method by encapsulating in on a static method that you can access from anywhere in your app.
public class MainApp extends Application {
private static Stage stage;
public static Stage getStage() { return stage; }
#Override public void start(Stage primaryStage) {
stage = primaryStage
stage.getIcons().add(new Image(getClass().getResourceAsStream("enabled.png")));
}
}
public class AnotherClass {
public void setStageTitle(String newTitle) {
MainApp.getStage().setTitle(newTitle);
MainApp.getStage().getIcons().remove(0);
MainApp.getStage().getIcons().add(new Image(getClass().getResourceAsStream("disabled.png")));
}
}
If you got Invalid URL or resource not found put your icon.png in the "bin" folder in your workspace.
Another easy way to insert your own icon on the title bar in JavaFX is to add the image to your primary stage using the following method:
Image ico = new Image("resources/images/iconLogo.png");
stage.getIcons().add(ico);
Make sure your import javafx.scene.image.Image (if using an ide like netbeans this should be automatically done for you).
I tried this and it works:
stage.getIcons().add(new Image(getClass().getResourceAsStream("../images/icon.png")));