Embedding a JFrame in an Applet - java

I have found an open source application that creates a JFrame to display some content. I would like to "embed" this JFrame into an applet, so everything that is displayed in the Jframe will be displayed in the applet - is this possible?
Thanks for your help!

Create an instance of the frame.
Get the content pane of the frame.
Add the content pane to the applet.

..open source application that creates a JFrame..
Since the source is available, take whatever they do in the frame, and do that instead in an applet (with some slight variants & gotchas).
Some typical problems with using the frame content in an applet are that:
The programmers might have set the GUI visible in the constructor, meaning you cannot get access to the content pane without showing a free floating GUI element on-screen.
Custom painting direct to the frame (eeek). There's no getting that stuff. But then, any programmer that incompetent should not be 'open'ing their source in the first place.
A frame programmer would typically use EXIT_ON_CLOSE as an exit behavior for the frame. It is not permitted (or necessary) for a sand-boxed applet to end the VM. Even a trusted applet would (and should) normally be prevented from calling System.exit(int).
Frame based code often does things that either require trust in an applet, or alternate strategies to achieve the same goal.
Points 3 & 4 are less applicable/relevant if the frame was designed to be launched using web start. And since I mention webstart..
Note that it is generally easier to 'convert' a frame to be launched using Java Web Start than it is either to create and deploy an applet, or to convert a frame to an applet. Unless there is some functionality of an applet that is vital & not available to an application (which is unlikely, given you started with the application), I would recommend launching the app. via JWS, rather than doing a conversion.

Related

Can you use JSF/Primefaces to update a JARs JFrame content?

I'm designing an executable Java program that is going to behave similarly to a webpage, but there won't be internet. Everything will just be in a JFrame and the contents are going to be updated for simulate going to a new page on the site. Would it be possible to use JSF and Primefaces for this? I've only ever done JSF with a webpage and am not sure if I could use it for a JFrame type program. I realize I can't recompile, I'm just wondering if I can use xml style and JavaBeans with JSF in a JFrame rather than the internet and how to go about that.

Changing Desktop Icon for Java Application

I wish to thank you in advance for taking the time to read my question, and I would greatly appreciate any comments, answers, insights, techniques and critiques that you may be able to provide.
I'm looking for a useful method for changing the desktop icon for a Java application. I've looked into this for a few days now, but am not finding an accurate result.
Before you mark this down and call it a duplicate, I have read: How do I change the default application icon in Java? to others who asked this question), but this does not address my specific problem. I know that their method utilizes a url location instead of an import, but I am trying to learn how to use this with the import(if that is, in fact, possible). When I attempt to use their method for changing by source location. Besides that, the url example doesn't seem to work for a file stored on the computer. I get an "uncaught error" message when I attempt to run it.
I use the following format to declare an image that I have imported into NetBeans:
Image image = new ImageIcon("imported.png").getImage();
frame.setIconImage(image);
Now this works fine for the icon that displays in the toolbar and it also appears in the upper left-hand corner of the frame, but I still have the Java coffee-cup as the icon for the application when I clean and build it.
For additional resources to the code that I am using to attempt this:
import java.awt.Image;
import javax.swing.*;
public class Check {
JFrame frame;
public static void main(String[] args) {
new Check().go();
}
private void go() {
frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Image image = new ImageIcon("owl.gif").getImage();
frame.setIconImage(image);
frame.setVisible(true);
frame.setSize(300, 300);
}
}
The "owl.gif" bit is what I imported into NetBeans by click and drag method (as described in one of the books that I read that focused on NetBeans).
I'm looking for a way to make a file that I already have saved on my computer the desktop icon for my application after it is built.
For deploying Java desktop apps., the best option is usually to install the app. using Java Web Start1. JWS works on Windows, OS X & *nix.
JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations, automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..
The 'desktop integration' will use the image identified in the launch file as the desktop or menu item icon.

JNLP - application-desc vs applet-desc - is one better than the other when not running in browser?

I have an applet which I no longer want to run in the browser - but in stead want to run as a downloadable, auto-updating, oflineable Java application via JNLP.
I do not want to maintain the ability to run it as an applet.
I have no problem with either wrapping the existing applet in a JFrame and a main-menthod, or simply rewriting it and replacing the init() and others all together.
But my question is:
Are there any advantages or disadvantages to to using application-desc vs applet-desc?
The first advantage of converting to JFrame based, is that you have immediate control of the top level container for things like:
menus
look & feel
default close operation
location
size by pack() (hooRAY!)
..
Go with applicaton-desc for a free floating deployment. But don't put the applet in a frame, instead put the applet content into a panel, and put the panel into the frame (or applet, or window, or..).

Difference of JApplet File Types In Netbeans

I would like to create an interactive Java Applet for assessment questions. Something like an (Applet Based Examination) that would run on Internet Explorer(v.7) browser for my students to take the exam online.
I am using NetBeans to create my Applet but I couldn't understand the difference between each of the following JApplet files/forms.
New File -> Java -> JApplet
New File -> Java -> Applet
New File -> Swing GUI Forms -> JApplet Form
New File -> AWT GUI Forms -> Applet Form
Which file/form should I choose and what are the considerations to be taken for choosing each file/form?
On another note, I would like to implement a video inside my applet (particular for a section on video analysis questions portion). Any suggestions on how could I achieve this?
Answer:
e) None of the above
For a GUI..
..for my students to take the exam online.
Create a JFrame based application, and launch it from a link using Java Web Start.
Netbeans probably has a wizard for JWS projects. Perhaps see Enabling Java Web Start in the NetBeans IDE for further details.
Update 1: AWT vs. Swing
One of the most relevant differences between AWT (e.g. Applet/Frame) & Swing (JApplet/JFrame) is that AWT is last millennium's GUI toolkit. Use Swing this millennium.
See a quick overview of the advantages of Swing over AWT for more details.
Update 2: GUI designer vs. coding by hand
I recommend not choosing any Netbeans GUI making project that ends in 'Form', if that means the GUI designer. A GUI designer offers great productivity gains to developers who already understand the layouts1, and how to nest2 them inside one another to layout a GUI in a logical and resizable way. But for a new programmer, they just get in the way, and produce horrendous code that few people will look at, let alone help debug.
See Laying Out Components Within a Container for more details on layouts.
There is a nice screenshot of a nested layout (with links to the code) in the post mentioned above discussing Swing vs. AWT.
Update 3: Further clarification
Do you have any examples of such JFrame based application for questionaires?
Not offhand (again, worthy of a question of its own - please stop thinking of 'a' question as some sort of 'one-stop shop').
Will this also be able to be integrated with MySQL Database as the questions will be pooled from there?
MySQL integration:
Server-side: Just as easy for an applet or JWS application if the DB is on the same server as the app. The app. needs to be trusted either way to reach out to a remote server.
Client side. Easier to get up and running using a JWS app.
JApplet :
Creates a new JFC (Swing) applet. An applet is a Java class that can run in any Java-enabled browser. Note: This template does not contain form code that allows you to design the applet visually in the Form Editor. For visual design, start with the JApplet template under Java GUI Forms.
Applet :
Creates a new AWT (Abstract Window Toolkit) applet. An applet is a Java class that can run in any Java-enabled browser. Note: This template does not contain form code that allows you to design the applet visually in the Form Editor. For visual design, start with the Applet template under Java GUI Forms | AWT Forms.
JApplet Form :
Creates a new JFC (Swing) Applet. An applet is a Java class that can be run in any Java-enabled browser.
Applet Form :
Creates a new AWT (Abstract Window Toolkit) Applet. An applet is a Java class that can run in any Java-enabled browser.
So the main differences are :
Use AWT or Swing
Use the Netbeans GUI designer (for Form) or not

wrapping a jframe

is there a way to open a program, that usually opens a new jframe, into an existing jframe?
here is the explanation, I have downloaded a java game one of those reflexes ones and it opens in a jframe with a bunch of sub panels inside of the frame, what i want to do is wrap the existing jframe in another frame or canvas or something so i can build internal scripts for it as apposed to building external scripts that require screenshots and getting pixel data. by internal scripts i mean scripts that run inside the new jframe
The usual way to achieve custom functionality would be to extend the class and override methods to add new/altered components and & new/altered methods.
OTOH I doubt that someone who refers to Java code as 'internal scripts' has the experience needed for this task. It would be better to start with simpler goals.

Categories