Jump to content
xisto Community
Sign in to follow this  
iGuest

How to make a Java Applet work in Browser? Java Applets

Recommended Posts

How to make a Java Applet work in Browser?Java Applets

Hi,

I have got some problem deploying Java Applet. I have a simple Java applet called ImgLoaderApplet that calls ImgLoaderPanel (a JPanel object). This applet(including the ImgLoaderPanel class) compiles and works well in both Eclipse and command line. However, when I try to call it from html within an browser, it always give me a blank panel at where the applet panel should show up. The Java Control Panel returns some message saying that the exception is thrown when try the createGUI() method of the ImgLoaderApplet. My browser is IE8. And I also used some other browser, which gave me the same result. And my browser works fine with example applets from sun.Com. Can any body tell me what is wrong and how to diagnose/solve the problem. I have been hung there for 4 days. Help!...

List 1. ImgLoaderPanel.Java

import java.Io.*;Import java.Awt.*;Import java.Awt.Event.*;Import javax.Swing.*;Import javax.Swing.SwingUtilities;Import javax.Swing.Filechooser.*;Import java.Sql.*;Import java.Nio.ByteBuffer;

public class ImgLoaderPanel extends JPanel implements ActionListener {   static private final String newline = "and";   JButton openButton, saveButton;   JTextArea log;   JFileChooser fc;

 public ImgLoaderPanel() {   super(new BorderLayout());

  //Create the log first, because the action listeners   //need to refer to it.   log = new JTextArea(5,20);   log.SetMargin(new Insets(5,5,5,5));   log.SetEditable(false);   JScrollPane logScrollPane = new JScrollPane(log);

  //Create a file chooser   fc = new JFileChooser();   openButton = new JButton("Open a File...", createImageIcon("images/Open16.Gif"));   openButton.AddActionListener(this);

  //Create the save button.    saveButton = new JButton("Save a File...", createImageIcon("images/Save16.Gif"));   saveButton.AddActionListener(this);

  //For layout purposes, put the buttons in a separate panel   JPanel buttonPanel = new JPanel(); //use FlowLayout   buttonPanel.Add(openButton);   buttonPanel.Add(saveButton);   //Add the buttons and the log to this panel.   add(buttonPanel, BorderLayout.PAGE_START);   add(logScrollPane, BorderLayout.CENTER);   }

  public void actionPerformed(ActionEvent e) {   }

  /** Returns an ImageIcon, or null if the path was invalid. */   protected static ImageIcon createImageIcon(String path) {   java.Net.URL imgURL = ImgLoaderPanel.Class.GetResource(path);   if (imgURL != null) {   return new ImageIcon(imgURL);   } else {   System.Err.Println("Couldn't find file: " + path);   return null;   }   }

  /** * Create the GUI and show it.  For thread safety, * this method should be invoked from the * event dispatch thread. */   private static void createAndShowGUI() {   //Create and set up the window.   JFrame frame = new JFrame("ImgLoaderPanel");   frame.SetDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   //Add content to the window.   frame.Add(new ImgLoaderPanel());   //Display the window.   frame.Pack();   frame.SetVisible(true);   }

  // called when run as an application   public static void main(String[] args) {   //Schedule a job for the event-dispatching thread:   //creating and showing this application's GUI.   javax.Swing.SwingUtilities.InvokeLater(new Runnable() {   public void run() {   createAndShowGUI();   }   });   }}

List 2. ImgLoaderApplet.Java

import javax.Swing.JApplet;Import javax.Swing.SwingUtilities;

public class ImgLoaderApplet extends JApplet {   //Called when this applet is loaded into the browser.   public void init() {   //Execute a job on the event-dispatching thread; creating this applet's GUI.   try {   SwingUtilities.InvokeAndWait(new Runnable() {   public void run() {   createGUI();   }   });   } catch (Exception e) {   System.Err.Println("createGUI didn't complete successfully");   }   }     /** * Create the GUI. For thread safety, this method should * be invoked from the event-dispatching thread. */   private void createGUI() {   //Create and set up the content pane.   ImgLoaderPanel newContentPane = new ImgLoaderPanel();   newContentPane.SetOpaque(true);   setContentPane(newContentPane);     }   }

List 3. ImgLoaderAppletTagNoJNLP.Html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>   <title>Image Loader Applet Demo Page</title>   <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">  </head>  <body>   <h1>Image Loader Applet Demo</h1>   <h2>This applet has been deployed with the applet tag <em>without</em> using JNLP</h2>   <applet code = 'ImgLoaderApplet'   width = 400   height = 320 />  </body></html>

 

List 4. Java Control Panel Messages

basic: Starting applet teardownBasic: Finished applet teardownBasic: 已添加进度监听程序:sun.Plugin.Util.GrayBoxPainter$GrayBoxProgressListener@179953c Basic: 小程序已载入。Basic: Applet resized and added to parent containerBasic: PERF: AppletExecutionRunnable - applet.Init() BEGIN ; jvmLaunch dt 446189 us, pluginInit dt 1593107053 us, TotalTime: 1593553242 usCreateGUI didn't complete successfullyBasic: Applet initializedBasic: 已删除进度监听程序:sun.Plugin.Util.GrayBoxPainter$GrayBoxProgressListener@179953c Basic: Applet made visibleBasic: Starting appletBasic: completed perf rollupBasic: Applet startedBasic: Told clients applet is started

-question by J.D.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.