Java SplashScreen in Java Web Start - java

I am trying to move my Java Swing project to Java Web Start
and I have problem with the splash screen. This application uses Maven.
When I load my application via the command-line or by an external exe,
it displays the splash screen correctly.
final SplashScreen splash = SplashScreen.getSplashScreen();
When I run application via Java Web Start, it always return null.
Yes, I know about splash screen section in JNLP file.
<icon kind="splash" href="splash.png"/>
But this will show splash screen before the application is loaded and not
when the application is running. In other words, it isn't a replacement for the --splash switch.
In the manifest file, I have:
SplashScreen-Image: (URL to resource, file in jar)
This works well only when I run jar file and not in Java Web Start.
Has anybody had this same problem and found any solution for this?
I need a splash screen because the application takes several seconds to start and nothing is displayed for user in this time.

To show a splash screen for JNLP clients, call the start() method passing the splash image path. To remove the splash screen, call the stop() method.
public class ShowSplash
{
private static JWindow splashFrame;
public void start(String splashImagePath) throws Exception
{
JLabel label;
ImageIcon image;
URL url;
splashFrame = new JWindow();
url = ShowSplash.class.getResource(splashImagePath);
image = new ImageIcon(url);
label = new JLabel(image);
splashFrame.add(label, BorderLayout.CENTER);
splashFrame.pack();
splashFrame.setLocationRelativeTo(null);
splashFrame.setVisible(true);
}
public void stop() throws Exception
{
splashFrame.dispose();
splashFrame = null;
}
}

The JWS based splash screen uses totally different functionality (and a different loading philosophy) to the AWT based SplashScreen. The JWS splash is always a loose file that is referenced in the JNLP file. We cannot get access to draw on the JNLP splash.

Thanks for answers, but I have problem with replace SplashRenderer with JWindow.
Here is code:
splashFrame = new JWindow();
splashFrame.setBackground(Color.white);
JPanel splashPanel = new JPanel();
splashPanel.setLayout(new BorderLayout());
JLabel image = new JLabel(img);
splashPanel.add(image);
splashFrame.setContentPane(splashPanel);
splashFrame.pack();
splashFrame.setLocationRelativeTo(null);
splashFrame.setAlwaysOnTop(true);
splashPanel.paintImmediately(0, 0, splashPanel.getSize().width, splashPanel.getSize().height);
splashFrame.setSize(splashPanel.getSize().width,splashPanel.getSize().height);
splashFrame.setLocation(100, 100);
splashFrame.setVisible(true);
The Window is always in back, no metter if I set setAlwyasOnTop or if I set this methods
in background thread. SplashRender in this case always stays on top, but JWindow not.
Currently effect is - When I start APP the Window is not visible during start up, but it's shows when MainFrame appear.

Related

Why is the application not showing upon adding icon to JMenuItem?

I've been running into a problem lately where I try to se a JMenuItems icon which when I define and point to the icon the application it self don't start/show.
I started looking for errors, but there where none; started looking for write occurencies, which typically pretty much does appear when I add the icon and after that as pointed above the application doesn't start/show.
When the icon is set but commented:
Code
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
// mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
Screen Shot Of Visible Application
After the icon is set and trying to execute application:
Code:
mntmMaximize = new JMenuItem();
mntmMaximize.setText("Maximize Window");
mntmMaximize.setActionCommand("maximize");
mntmMaximize.addActionListener(this);
mntmMaximize.setMnemonic(KeyEvent.VK_PLUS);
mntmMaximize.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, ActionEvent.CTRL_MASK));
mntmMaximize.setIcon(new ImageIcon(Vision.class.getResource("xray/fullscreen16x.png")));
mnWindow.add(mntmMaximize);<br>
Picture:
The window not created and application terminated
Note that when the window is not created in this picture the application is therefor terminated.
Please try to answer nice, and if you need the whole source file it is possible.
Edit:
Also if needed i can make a video where i show when i start the application when the icon is set but not commented.
getResource uses the relative path with respect to the package (folder), like in
Vision.class.getResource("fullscreen16x.png")
or absolute like in:
Vision.class.getResource("/xray/fullscreen16x.png")

Internal graphics not initialized yet: javafx

I'm trying to write a javaFx application whit multiple images inside a window.
The short story is that I have an enum class named Candy and each candy has some properties and a path to the image file representing it.
In the constructor of my javafx.application class (Table) I want to fill an array list with those images, so I wrote this so far:
public class Table extends Application {
ArrayList<Image> images;
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("CandyFx");
primaryStage.show();
}
public Table() {
images = new ArrayList<Image>();
for (Candy candy : Candy.values()) {
File file = new File (candy.getImagePath());
Image image = new Image(file.toURI().toString());
images.add(image);
}
}
}
Now every time I want to create an instance of Table class, the application throws a java.lang.RuntimeException: Internal graphics not initialized yet.
How can I initial graphics which it seems I did not?
First of all if you are using linux ,GTK 2.18 is required to run JavaFX .try to install
libswt-gtk-3-java
This exception will thrown whenever your screen is null .Try to create your images inside start method. Just before the primaryStage.show();.
Take a look at this link too
http://cr.openjdk.java.net/~vadim/RT-33475/webrev.00/modules/graphics/src/main/java/com/sun/glass/ui/Screen.java.html
I have no idea how it exactly works, but when we first create a JFXPanel in our start we don't get the errors anymore.
JFXPanel jfxPanel = new JFXPanel();
Actually, JavaFX Stage class is the top level JavaFX container which should be constructed and modified on the JavaFX Application Thread.
Here you are using Stage class but have not embed JavaFx content into Swing application which may show you a 'java.lang.RuntimeException with message Internal graphics not initialized yet.
Here you may use-
JFXPanel jfxPanel = new JFXPanel();
Or you may use in this way too.
JFrame frame = new JFrame("Java Swing And JavaFX");
JFXPanel jfxPanel = new JFXPanel();
frame.add(jfxPanel);
JFXPanel is a component to embed JavaFX content into Swing applications. The content to be displayed is specified with the setScene(javafx.scene.Scene) method that accepts an instance of JavaFX Scene. After the scene is assigned, it gets repainted automatically. All the input and focus events are forwarded to the scene transparently to the developer.
References:
JFXPanel: https://docs.oracle.com/javase/8/javafx/api/javafx/embed/swing/JFXPanel.html
Stage: https://docs.oracle.com/javase/8/javafx/api/javafx/stage/Stage.html

How to change java icon in a JFrame

Ok so I've been researching this one quiet a bit. I am fairly new to java but thought that this one would be easy. Ive tried just about every way that has been answered on this site and still no luck, and usually when I look here I am able to find a answer that fits what I am looking for. Does anyone know how to change the Java icon in the top corner of the JFrame. I'm pretty positive that its not my file path either because all my images are in the same folder and they all work, this is the only one that I can't seem to get to work.
This is the first part my code for the main menu of my program, everything works except when i try to add the icon image. The code I've entered below does not have anything in it for the JFrame IconImage, I removed it since it didn't work. So if there is someone who knows how to get it working with this code that would be highly appreciated, thank you very much in advanced!
public class MainFrame
{
private MyPanel main;
private MyPanel2 create;
private MyPanel3 update;
private MyPanel4 find;
JFrame frame = new JFrame("Main Menu:");
public void displayGUI()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new CardLayout());
main = new MyPanel(contentPane, this);
create = new MyPanel2(contentPane);
update = new MyPanel3(contentPane);
find = new MyPanel4(contentPane);
contentPane.add(main, "Main Menu");
contentPane.add(create, "Create Part");
contentPane.add(update, "Update Part");
contentPane.add(find, "Find Part");
frame.setLocation(200, 200);
frame.setSize(700, 580);
frame.setContentPane(contentPane);
frame.setVisible(true);
}
I have an answer for you. First, make sure that the images are in a folder, not a package. Next, insert this line of code:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("path/to/image.png"));
ImageIcon icon = new ImageIcon( );
setIconImage(icon.getImage());
This code gets the image from the class path, and returns it as a image icon, and then it sets it. This should add the image icon to the application. If it doesn't, then tell me.
EDIT: After you told me that that didn't work then I decided to take a second crack at it...
First, put your images into a completely separate folder. I usually call this /res. Next, put your image in there. Now, for loading I took a completely different route. I decided to use ImageIO instead of default loading. To load the image, you use this code:
try {
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}
ImageIO works a lot better for loading images. If this still doesn't work then please tell me.
If you want to export this as a JAR then put a folder the same name as you used in the program in the same directory as the JAR.
For example in a NetBeans project, create a resources folder in the src folder.
Put your images (jpg, ...) in there.
Whether you use ImageIO or Toolkit (including getResource),
you must include a leading / in your path to the image file:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/agfa_icon.jpg"));
setIconImage(image);
If this code is inside your JFrame class, the image is added to the frame as an icon in your title bar.
This works pretty fine for me.
Just add this after you've created your JFrame.
try {
Image image = new ImageIcon("/icons/image.jpg").getImage();
frame.setIconImage(image);
}catch(Exception e){
System.out.println("Application icon not found");
}
Paste your image icon (fav.png) in the same package first,
Write following code in constructor of JFrame:
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("fav.png")));
Note:- fav.png is the name of icon
this.setIconImage(new ImageIcon(getClass().getResource("/iconsfolder/iconsname.jpg")).getImage());
// sets the Global icon for the system
try this code put after this code:
public void displayGUI()
{

JFrame's icon not displaying

I've got a Java application with Swing's JFrame as a main GUI unit. I've set the icon to it via setIconImage(). When I run this program in NetBeans, everything works fine and the frame's icon displays. But when I compile it and try to run jar-file (with JRE7), the application has standard icon with Duke. How do I change that icon when running app outside NetBeans?
UPD:
OK, here's the code:
public static void main(String[] args) throws IOException{
URL imgUrl = Polygon.class.getResource("/imgs/icon.png");
Image img = ImageIO.read(imgUrl);
JFrame f = new JFrame();
f.setSize(new Dimension(500, 500));
f.setIconImage(img);
f.setVisible(true);
}
UPD2:
I've added this line to the end of the code:
JOptionPane.showMessageDialog(null, new ImageIcon(img));
Everything's fine with the image! It loads! BUT it's not displayed as the icon.
When you run the application from inside Netbeans, the files from your project folder are available, but if you run the compiled JAR yourself they may not.
Read this example (note the comments) to load your image properly.
Try getClass().getResource("imgs/icon.png"). It works for me. Note the difference between absolute and relative path. You may not need the leading /.

export a JOGL applet and embedd into a html page

It is some time that I'm testing opengl with java and JOGL. I have a good result and I wan to publish it on the web, but I have a problem. I'm in eclipse, and I'm testing an Applet with JOGL.
EDIT: Thanks to Ricket answer it fixed this problem
first of all i have this run time error (but the program works correctly):
java.lang.IllegalArgumentException:
adding a window to a container at
EDIT: but it still doesn't work:
then I found this incredibly clear page
and I did what is said. I open html with the browser, the libs are downloaded, but it stops at Starting applet AppletHelloWorld, as that is the name I gave to my applet.
Maybe I am missing something like a main function or exporting the jar properly?
This is my main code:
public class AppletHelloWorld extends Applet
{
public static void main(String[] args)
{
JFrame fr=new JFrame();
fr.setBounds(0,0,1015,600);
fr.add(new AppletHelloWorld());
fr.setVisible(true);
}
public void init()
{
setLayout(null);
MyJOGLProject canvas = new MyJOGLProject(); //MyJOGLProject extends JFrame
canvas.run(); // this do setVisible(true)
} //....
Just as the error says, you're trying to add a window to a container. A JFrame is a window. You can't add a JFrame to anything, including a Container. I think perhaps you either don't know what a JFrame is, or don't know what a Container is.
Ideally you would have MyJOGLProject extend GLEventListener instead. Then your init function would make a new GLCanvas, add an instance of MyJOGLProject to it (via addGLEventListener), and then add the GLCanvas to your applet.
Alternatively, if you're okay with the applet popping up a JFrame, then simplify your init method:
public void init() {
setLayout(null);
MyJOGLProject canvas = new MyJOGLProject();
canvas.setVisible(true);
}
That should do it.
Use JApplet. I think that's the reason why it fails.
(Use Webstart with JNLP in NetBeans)

Categories