Search code examples
javaeclipseswingjframejapplet

Why my program works on JFrame but not on JApplet?


I've got a project in Java (developped with Eclipse), in which I use some Swing components. At the start, I worked with JFrame : I created a new class herited from JPanel (PanelTr), added in it my JLabels, JTextField and JButton with an ActionListener on my button, and added an object PanelTr to my JFrame-herited class. Here's a piece of my code :

package gui;

public class PanelTr extends JPanel implements ActionListener {

    // Here I instantiate my JTextField and JButton used by ActionListener
    private JTextField textCap = new JTextField(20);
    private JButton submitButton = new JButton("Submit"); 

    public PanelTr() {

        // Here I add my JLabels and my layout manager

        submitButton.addActionListener(this);


}

    public void actionPerformed(ActionEvent evt)
    {
        if (evt.getSource()==submitButton)
        {
            JOptionPane.showMessageDialog(null,"DEBUG"); // Just to see if it is displayed
            String err = Analyzer.process(textCap.getText().toString()); // An analyzing process I use in my program
            if (err=="")
            {
                JOptionPane.showMessageDialog(null,"OK");
            }
            else
            {
                JOptionPane.showMessageDialog(null,err,"Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    }   
}

And my JFrame :

package gui;

public class FrameTr extends JFrame
{
    public FrameTr()
    {
        PanelFils ctn = new PanelFils(); //Conteneur
        setContentPane(ctn);
        ctn.setBackground(new Color(0,255,255));
        setTitle("Project");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String args[])
    {
        new FrameTr();
    }
}

In my Analyzer class, I use several Excel files located in the root folder of my project.

So with this code, my program runs well, on Eclipse and when I export it into a runnable jar (I place my Excel files in the same folder of my .jar file).

But now I need to make an Applet which allows the program to run on a web browser. So I created a new JApplet-based class with just this code :

package gui;

public class TestApplet extends JApplet{

    public void init()
    {
        PanelTr ctn = new PanelTr();
        setContentPane(ctn);
    }
}

When I run this class on Eclipse, my debug JOptionPane is displayed, but in the analyzing process I get a FileNotFoundException on my first Excel File. I tried to move it to the src folder, to the gui folder, even to the analyse folder (package of my Analyzer class), but still this FileNotFoundException...

I also tried to run my program in a webpage, so I exported as a runnable jar (Note : I couldn't choose TestApplet in my launch configuration since it doesn't have a main(), so I choosed FrameTr). I used the tag :

<applet archive="app.jar" width="700" height="100" code="gui/TestApplet.class"/>

My pane is displayed, but when I click on my button, the debug JOptionPane doesn't pop out !

So first, how can I solve the FileNotFound problem ? And then, why my JOptionPane isn't displayed on my web browser ?


Solution

  • The Java Applet is executed client side - it runs on the computer of the person using the website. It does not run on the web server.

    If you have files in the server then the client will not be able to access them unless you serve them up from your web server and access them via HTTP(S).

    If the value in the files are constants then you should put the files inside your JAR and distribute them as part of the Applet.