0

I have made a Java Applet.

<applet code=gui.clientGUI.MyApplet.class 
        archive="QTminer.jar"
        width=400 height=200>
</applet>

my jar looks like this:

enter image description here

this is my simple code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class MyApplet extends JApplet {

    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    initUI();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initUI() {
        getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        JButton btnStartApplication = new JButton("Start Application");
        btnStartApplication.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SS oi = new SS();
                oi.setVisible(true);
            }
        });
        getContentPane().add(btnStartApplication);
    }
}

class SS extends JWindow {

    private JLabel lblNewLabel;
    private JLabel label;

    public SS() {
    setBounds(new Rectangle(0, 0, 883, 590));
    setLocationRelativeTo(null);
    getContentPane().setLayout(null);

    lblNewLabel = new JLabel("Welcome", SwingConstants.CENTER);
    lblNewLabel.setForeground(Color.RED);
    lblNewLabel.setFont(new Font("Segoe UI", Font.BOLD | Font.ITALIC, 24));
    lblNewLabel.setBounds(0, 313, 883, 41);
    getContentPane().add(lblNewLabel);

    label = new JLabel(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));

    label.setBounds(0, 0, 883, 592);
    getContentPane().add(label);

    setVisible(true);
}
}

My problem is that when running in eclipse everything works, but when running in browser i get a NullPointerException right on the load of the image icon:

label.setIcon(new ImageIcon(getClass().getResource("/gui/resources/Qtminer_background.jpg")));
Francesco Rizzi
  • 631
  • 6
  • 24
  • 1
    I guess you can't expect a JWindow to pop up (in a browser) that runs independently from the JApplet... That would be pretty user-unfriendly, too. Why don't you just make everything inside one JApplet? - Remove the previous panel and add the new one. Btw, maybe you will get better help than my comment if you post a [mcve]. You really just need to show us a compilable example that launches a new JWindow after a button click inside a JApplet. – Lukas Rotter Sep 25 '15 at 14:57
  • 2
    Not sure if this is the cause of your problem, but you should replace spin-loops like `while(object.showable==false);` with [wait](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--)/[notify](http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) or with [CountDownLatch](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CountDownLatch.html). – VGR Sep 25 '15 at 15:04
  • I'll try, but if you want you can try to compile this code, is fully compilable. – Francesco Rizzi Sep 25 '15 at 15:07
  • 2
    @LuxxMiner *"Remove the previous panel and add the new one."* (shudder) A `CardLayout` is almost always a better solution. – Andrew Thompson Sep 25 '15 at 15:19
  • 1
    @AndrewThompson Oh yeah, that's right. I always thought there's not really a big difference between doing it manually and with `CardLayout`. I can't believe I never just [googled](http://stackoverflow.com/a/10694622/4857909) it :P. Thx for the advice. – Lukas Rotter Sep 25 '15 at 15:29
  • ***"My splash screen loses his background image! In Eclipse my splash screen shows correctly"*** Then apparently the image is either not on the run-time class-path of the applet, or not at the place within the class-path that the code is requesting it from. Show the applet element used to launch the applet (particularly the `codebase` and `archive` fields) as well as the content listing for each Jar. – Andrew Thompson Sep 25 '15 at 15:50
  • And a tip: Add @LuxxMiner (or whoever, the `@` is important) to *notify* the person of a new comment. ;) – Andrew Thompson Sep 25 '15 at 15:51
  • @AndrewThompson, thanks for the tip. I didn't understand what should i show, pardon, can you help me? – Francesco Rizzi Sep 25 '15 at 15:53
  • 1
    *"I didn't understand what should i show"* Are you launching the applet using HTML? Copy/paste the part that shows the applet element used to launch it. That will tell us what Jars are on the class-path. Then we need to know the Jar content. If you don't understand something I write, please be specific about what you don't understand. I don't like having to write an essay in reply to a vague 'I don't understand'.. – Andrew Thompson Sep 25 '15 at 16:02
  • @AndrewThompson sorry for the vagueness. HTML CODE and a view of my jar have been added to my post, you can see it in the first lines – Francesco Rizzi Sep 25 '15 at 16:16
  • *"..and a view of my jar"* Rather than a screenshot of WinRAR (which BTW - does not even show the resource in question, just the parent directory), use the Jar tool on the command line to get a complete listing. Something like `jar -tvf QTminer.jar` – Andrew Thompson Sep 25 '15 at 16:29
  • @AndrewThompson done, i hope it's good now – Francesco Rizzi Sep 25 '15 at 16:40
  • The path in code matches the actual path as far as I can see.. But `ServerSplashScreen.class.getResource("/gui/resources/Qtminer_background.jpg")` .. what is `ServerSplashScreen`? Try instead `this.getClass().getResource("/gui/resources/Qtminer_background.jpg")` – Andrew Thompson Sep 25 '15 at 16:48
  • @AndrewThompson got some news, check the edit in my original post. Using the original code i posted, strange things happen. – Francesco Rizzi Sep 25 '15 at 17:53
  • @LuxxMiner i edited my post, please check it out and if you can help me. – Francesco Rizzi Sep 26 '15 at 14:27
  • @AndrewThompson i edited my post, please check it out and if you can help me. – Francesco Rizzi Sep 26 '15 at 14:28
  • 1) Remove the constructor. Do everything of relevance in the `init()` method and ensure it is done on the EDT.` 2) `MyApplet app=new MyApplet(); app.setVisible(true);` ?!? You already have an instance of the applet (which will automatically be set visible)! There is no sense to creating one here. – Andrew Thompson Sep 27 '15 at 00:45
  • I made en edit to include code that implements my suggestions on how to arrange the applet parts. A few comments: 1) It creates an image in code to allow other people to run it. 2) The original code wouldn't run for me until I create the image, then it worked for me as is. But the code in the constructor was 'dangerous' in that it was being called off the EDT. So that may have been the problem in the browser. 3) Try the code I posted in a) your IDE. b) in the browser completely unchanged. c) in the browser, adapted for your image. 4) .. but – Andrew Thompson Sep 27 '15 at 03:22
  • 4) .. but popping a free floating window out of an embedded applet suggests it would be better to create the GUI in a `JFrame` or a `JWindow` as needed, then launch that directly from a link using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). Launched using JWS it will be simpler & more reliable to deploy and make for a better user experience. – Andrew Thompson Sep 27 '15 at 03:27
  • @AndrewThompson code works with image created in code. but the problem comes back when i create label this way: label.setIcon(new ImageIcon(this.getClass().getResource(/path/img.jpg))); – Francesco Rizzi Sep 27 '15 at 14:00
  • Be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. But again, this will be better done without the applet. – Andrew Thompson Sep 27 '15 at 14:18
  • @AndrewThompson Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException at javax.swing.ImageIcon.(Unknown Source) it seems that it's not founding my jpg file – Francesco Rizzi Sep 27 '15 at 14:39

1 Answers1

0

My OS denied access to the local file jar.

Testing the applet on a real web-server made it work.

Solved!

Francesco Rizzi
  • 631
  • 6
  • 24