Link to home
Start Free TrialLog in
Avatar of AttilaB
AttilaB

asked on

How do you practically save / load temporary user data with JApplet made of a JPanel app?

I would like to make some applets out of Java desktop applications, basically by loading JPanels
with the built-in functionality into JApplets. This is sort of a "test case" that I made here for you
to reduce your work looking over my problem.

I put loading the JPanel in the init() method of the JApplet, and I represented the user info that is to be saved when the user navigates away from the applet page with the array:

private String[] TestArray = { "one", "two", "three", "Original"};  

(This is in the JPanel of course, with the 'application' part.)

Where the TestArray[3] element of the array is being changed by the user as they type in something in the textfield and hit the Update button.

What I would like to see is that if I navigate away from the applet in the browser and then come back to it without closing the browser that the last values for TestArray are restored to the last user values as opposed to being reloaded with the initial values.

How do I set up the start() and stop() methods in the applet so that that happens?

Also, the button for loading the URL for Google in this example  used to work, but for some reason it will just generate an exception now. I don't know hat is happening with that.

Attached the source code in the code windows and I also uploaded the applet to the Web so that you can see it running as it is running at the URL:

http://boundaryscan.htmlplanet.com/Test_Applet.htm

 
import javax.swing.*;

import java.awt.Dimension;



public class Test_Applet extends JApplet {



	private Operating_Panel operating_Panel = new Operating_Panel();

	private String[] savedUserData = new String[4];

   

    public void init() {

        createGUI();

        }



    private void createGUI() {

    	 this.setSize(new Dimension(395, 213));

         this.getContentPane().add(operating_Panel);

    }

    

    //code for restoring/loading user's data when they return from a different page to applet

    public void start()

      {

    		 

      }

    

   

     //code for saving user's data when they go away from page with applet

    public void stop()

    	

    {

    	savedUserData = operating_Panel.getCurrentArrayOfNumbers();

    }

}

Open in new window


Thank you.
<html>

	<head>

	</head>

	<body bgcolor="000000">

		<center>

			<applet

				code	= "Test_Applet.class"

				width	= "395"

				height	= "213"

				>

			</applet>

		</center>

	</body>

</html>

Open in new window

import java.awt.Color;

import java.awt.Desktop;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.URI;

import java.net.URL;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;



public class Operating_Panel extends JPanel {

    private JLabel jLabel1 = new JLabel();

    private JTextField jTextField1 = new JTextField();

    private JButton jButton1 = new JButton();

    private JLabel jLabel2 = new JLabel();

    

    private String[] TestArray = { "one", "two", "three", "Original"};

    private JLabel jLabel3 = new JLabel();

    private JButton jButton2 = new JButton();



    public Operating_Panel() {

        try {

            jbInit();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }



    private void jbInit() throws Exception {

        this.setLayout( null );

        this.setSize(new Dimension(395, 213));

        jLabel1.setText("Last typed data:");

        jLabel1.setBounds(new Rectangle(90, 15, 210, 30));

        jLabel1.setForeground(Color.magenta);

        jLabel1.setFont(new Font("Tahoma", 1, 20));

        jTextField1.setBounds(new Rectangle(50, 95, 185, 35));

        jTextField1.setFont(new Font("Tahoma", 0, 13));

        jButton1.setText("Update");

        jButton1.setBounds(new Rectangle(255, 95, 100, 35));

        jButton1.setFont(new Font("Tahoma", 1, 13));

        jButton1.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    jButton1_actionPerformed(e);

                }

            });

        jLabel2.setBounds(new Rectangle(90, 60, 165, 25));

        jLabel2.setFont(new Font("Tahoma", 1, 20));

        jLabel2.setText("Original");



        jLabel3.setText("jLabel3");

        jButton2.setText("Open URL");

        jButton2.setBounds(new Rectangle(285, 0, 110, 20));

        jButton2.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    jButton2_actionPerformed(e);

                }

            });

        this.add(jButton2, null);

        this.add(jLabel2, null);

        this.add(jButton1, null);

        this.add(jTextField1, null);

        this.add(jLabel1, null);

    }



    private void jButton1_actionPerformed(ActionEvent e) {

        System.out.println("Update Button Clicked");

        jLabel2.setText(jTextField1.getText());

        

        TestArray[3] = jLabel2.getText();

    }



    private void jButton2_actionPerformed(ActionEvent e) {

        System.out.println("Open URL Clicked");

        // For some reason it does not bring up the webpage ???

        try {

                    String url = "http://www.google.com";

                    Desktop.getDesktop().browse(URI.create(url));

                  }

                  catch (IOException ex) {

                      System.out.println(ex.getMessage());

                  }

    }

    

    

    // Getter to access the array to be saved from the Applet class:

    public String[] getCurrentArrayOfNumbers(){

    	return this.TestArray;

    }

    

}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What's your motivation for using an applet instead of an application? An applet needs to be signed in order to read from/write to disk
Avatar of AttilaB
AttilaB

ASKER

Well the motivation is for the application to run as an applet so that the applet can serve as an online demo for the application, with limited capabilities.

So, actually when you browse away from the applet is all the data in the applet lost forever from memory?

Is there any way to store away data in memory, that could be retrieved after returning to the applet page?

If that's the case and "signing in the applet" would be the only option and store a temp file on the user's machine, I guess.

Code-wise, is signing in the applet an involved process? There must be a standard way to do it, I hope.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AttilaB

ASKER

So, you are saying that instead of being a simple applet, it would be easier to deploy something like this as a JWS (Java Web Start) application.

I did a search what JWS is, and found some tutorials like this one:

http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/

Seems to make sense also I tried how it runs, from another tutorial, which lets you try it online
how the security dialog comes up for the user:

http://www.autexier.de/jmau/dev/webstart/

Here is a scary thought though:

The tutorial talks about "placing the jnlp file in web server(tomcat) root folder".
So this implies that this is MY web server, I have FULL access to.

So I do need to have FULL access to the computer the web server runs on, as opposed just having access to a home folder on the server where up until now I have uploaded my .class files, images and
HTML code?





Avatar of AttilaB

ASKER

Well, actually Web Start appears to be the best solution if I wanted to save user info, as you suggested. Thanks.
:)

>>as opposed just having access to a home folder on the server where up until now I have uploaded my .class files, images and HTML code?

You can't deploy a real world web-enabled solution in that way anyway