Results 1 to 9 of 9
  1. #1
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default Writing a hybrid aplication

    I have seen a lot of applications that can run as standalone application and applet as well. But I can't find any tutorial to write this kind of application. Could somebody post here a code of simple "Hello world" JFrame application and also HTML code that works this way? thx

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Best to write your application to create a JPanel. Then you can create a simple application that creates a JFrame and places the JPanel into its contentPane, or if desired place the JPanel in contentPane of another app that creates a JApplet.

  3. #3
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Here's an example:

    Java Code:
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagLayout;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class AppletFrame extends JApplet {
       
       public void init() {
          try {
             javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                   getContentPane().add(new MainGui());
                }
             });
          } catch (Exception e) {
             System.err.println("createGUI didn't successfully complete");
          }
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                JFrame frame = new JFrame("AppletFrame");
                frame.getContentPane().add(new MainGui());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);         }
          });
       }
    }
    
    @SuppressWarnings("serial")
    class MainGui extends JPanel {
       private JLabel label = new JLabel("Main GUI");
    
       public MainGui() {
          setPreferredSize(new Dimension(400, 300));
          setLayout(new GridBagLayout());
          label.setFont(label.getFont().deriveFont(Font.BOLD, 32));
          add(label);
       }
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    That Applet is not truly an applet. It's only a Panel extension. To have code that is an applet or to take any applet and have it run as an application, you need to wrap the applet with browser replacement code.
    Here's something I cobbled together years ago. The applet part of it is in another class: Applet6. The code needs to be tailored for each applet it is to support.
    Java Code:
    // Wrap Applet to make an app
    
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.*;
    import java.io.*;
    
    
    //***********************************************************************
    // Define a wrapper class for the applet
    // This code needs to be tailored to work with your applet
    class AppletWrapper extends Frame implements AppletStub, AppletContext {
    
        Applet applet;
    
        // Constructor
        AppletWrapper(Applet a, int x, int y) {  
              applet = a;  
              setTitle(a.getClass().getName());
              setSize(x, y);
              Dimension ss = Toolkit.getDefaultToolkit().getScreenSize();
              setLocation((ss.width-x)/2, (ss.height-y)/2);
              add(applet, "Center");
    
              applet.setStub(this);   // This connects us to the applet <<<<<<<
        
              // Trap window closing
              addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent we) {
                    System.exit(0);         // EXIT Program 
                }
              });  // end WindowListener
    
              applet.init();
              setVisible(true);
              applet.start();
       } // end constructor
      
        // AppletStub methods
        public boolean   isActive() { return true; }
        public URL       getDocumentBase() { return null; }
        public URL       getCodeBase() {
            return null;
        }
    
        // Sample getParameter() - returns <PARAM values
        public String    getParameter(String name) {
            if (name.equalsIgnoreCase("DEBUG")) {
                    return "NO";
            }
            if(name.equals("PLOTDEBUG"))
                return "YES";
    
            if (name.equalsIgnoreCase("SERVER"))
                return "NO";
    
            return ""; 
        }
    
        public AppletContext getAppletContext() { return this; }
        public void      appletResize(int width, int height) {}
       
        // AppletContext methods
        public AudioClip getAudioClip(URL url) { return null; }
        public Image     getImage(URL url) { return null; }
        public Applet    getApplet(String name) { return null; }
        public Enumeration<Applet> getApplets() { return null; }
        public void      showDocument(URL url) {}
        public void      showDocument(URL url, String target) {}
        public void      showStatus(String status) {}
        public InputStream	getStream(String key){return null;} 
        public Iterator<String>	getStreamKeys() {return null;} 
        public void	setStream(String key, InputStream stream) {} 
    
    } // end class AppletWrapper
                   
    //--------------------------------------------------------------------------
    // Following creates an instance of the applet and wraps it with the above
    public class AppFromApplet6 {
    
       public static void main(String[] args) {
          Applet a6 = new Applet6();    // Here we create an instance of the Applet to wrap
          new AppletWrapper(a6, 600, 300);   // Wrap it and start it
    
    }

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by Norm View Post
    That Applet is not truly an applet. It's only a Panel extension.
    Norm, could you please elaborate on this a bit as I'm not sure what you mean by it. Thanks in advance!

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    Add a getParameter() (or any of the AppletContext methods) call to the applet and see what happens when its executed.

    The posted example only puts up a GUI and really doesn't demo a working applet.
    Last edited by Norm; 07-13-2010 at 08:04 PM.

  7. #7
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Here's an example:
    Your example is exactly what I want, but when I run it as applet, i get error (in any browser). As standalone application it works correct. I googled for that error but it doesn't help (answer was to install new JRE - still same error).
    Log from JAVA Console:
    Java Code:
    java.lang.NoClassDefFoundError: Viewer (wrong name: reportviewer/Viewer)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClassCond(Unknown Source)
    	at java.lang.ClassLoader.defineClass(Unknown Source)
    	at java.security.SecureClassLoader.defineClass(Unknown Source)
    	at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    	at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Exception: java.lang.NoClassDefFoundError: Viewer (wrong name: reportviewer/Viewer)
    (the only change I made in your code was renaming the main class)

    I tried to put class files directly into folder were html file (and not using codebase attribute) and put it into folder (and use codebase attribute) but still I get same error.

    My HTML file (applet part):
    Java Code:
    <applet codebase="reportviewer"
               code="Viewer" 
               width="800"
               height="600">
    </applet>
    Quote Originally Posted by Norm View Post
    That Applet is not truly an applet. It's only a Panel extension. To have code that is an applet or to take any applet and have it run as an application, you need to wrap the applet with browser replacement code.
    Nice code. It's the shortest code for this purpose I've ever seen. Anyway for my problem "Panel extension" would be adequate.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default

    If the class is in a package you need to account for it.
    Use code=reportviewer.Viewer
    and put the class files in the reportviewer folder.

  9. #9
    bassfero is offline Member
    Join Date
    Jul 2010
    Posts
    22
    Rep Power
    0

    Thumbs up

    Quote Originally Posted by Norm View Post
    If the class is in a package you need to account for it.
    Use code=reportviewer.Viewer
    and put the class files in the reportviewer folder.
    It's working now! Thx a lot.

Similar Threads

  1. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  2. Replies: 6
    Last Post: 12-23-2009, 09:10 AM
  3. Writing in XLS
    By selva.bics in forum Advanced Java
    Replies: 1
    Last Post: 11-09-2009, 09:09 AM
  4. Hibernate aplication Problem
    By Prashant.surwade in forum Advanced Java
    Replies: 6
    Last Post: 09-18-2009, 10:32 AM
  5. writing to gui
    By rob in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •