Results 1 to 9 of 9
Thread: Writing a hybrid aplication
- 07-13-2010, 04:52 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
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
-
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.
- 07-13-2010, 05:16 PM #3
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
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); } }
- 07-13-2010, 07:28 PM #4
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 }
- 07-13-2010, 07:54 PM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 07-13-2010, 07:56 PM #6
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.
- 07-14-2010, 11:31 AM #7
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
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:
(the only change I made in your code was renaming the main class)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)
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):
Nice code. It's the shortest code for this purpose I've ever seen. Anyway for my problem "Panel extension" would be adequate.Java Code:<applet codebase="reportviewer" code="Viewer" width="800" height="600"> </applet>
- 07-14-2010, 12:38 PM #8
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.
- 07-14-2010, 04:33 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Keylistenr doesn't work after pressing any button from main aplication
By darkkis in forum New To JavaReplies: 6Last Post: 12-23-2009, 09:10 AM -
Writing in XLS
By selva.bics in forum Advanced JavaReplies: 1Last Post: 11-09-2009, 09:09 AM -
Hibernate aplication Problem
By Prashant.surwade in forum Advanced JavaReplies: 6Last Post: 09-18-2009, 10:32 AM -
writing to gui
By rob in forum New To JavaReplies: 2Last Post: 02-13-2009, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks