By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,441 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,441 IT Pros & Developers. It's quick & easy.

converting java gui class to applet??

P: 13
hello all, I have tried my hand at this for a couple hours but no luck. I am using JCreator.

I used these two links for reference:

Here is my situation: trying to turn a 3000 line main class into an applet, or create an applet that calls it. whatever works.

in a nut shell, here is the structure:
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.border.*;
  5. import java.applet.*;
  7. public class project4 extends JFrame implements ActionListener, ItemListener{
  9. /**bunch of static elements and data types*/
  11. public project4() { /**stipulating variables - panel/card layout*/ }
  13. /** Main method */
  14.     public static void main(String[] args) {
  15.         project4 frame = new project4();
  16.         frame.setSize(800, 700);
  17.         frame.setLocation(100,60);
  18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         frame.setTitle("Project Video");
  20.         frame.setVisible(true);
  21.     }
  22. /**handle button action*/
  23. public void actionPerform (ActionEvent e){}
  25. public void itemStateChanged (ItemEvent e){}
  27. /**various class void and string methods (11 or so) like:*/
  29. public void addVideoGameItems () {}
  30. public String gameRentalDescriptionMethod () {}
  32. }// end project4
here is how i have tried modifying...with no luck...
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.border.*;
  5. import java.applet.*;
  7. public class applet extends JApplet{ 
  8. public static class project4 extends JFrame implements ActionListener, ItemListener{
  10. /**bunch of non-static private elements and data types*/
  12. public project4() { /**stipulating variables - panel/card layout*/ }
  15. /**handle button action*/
  16. public void actionPerform (ActionEvent e){}
  18. public void itemStateChanged (ItemEvent e){}
  20. /**various class void and string methods (11 or so) like:*/
  22. public void addVideoGameItems () {}
  23. public String gameRentalDescriptionMethod () {}
  25. }// end project4
  27.      public void init() {
  28.       System.out.println("Applet initializing");
  29.       project4test d = new project4test();
  30.       getContentPane().add(d);
  31.     } 
  33. /** Main method */
  34.     public static void main(String[] args) {
  35.         project4test frame = new project4test();
  36.         frame.setSize(800, 700);
  37.         frame.setLocation(100,60);
  38.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.         frame.setTitle("Project Video");
  40.         frame.setVisible(true);
  41.         applet a = new applet();
  42.         a.init();
  43.         frame.getContentPane().add(a);
  44.     }
  45. }//end applet class
so...I have got it to compile without any errors, but it still wont display. i have done simple applets in the past, but nothing like this...might have something to do with JFrame...any ideas?

thanks very much.
Feb 6 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 1,216
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

Feb 6 '08 #2

Expert 100+
P: 1,216
I think the best way to go about writing code that works when placed in a JFrame or JApplet is to write the core of your code so that it doesn't mention either! Start with the content panel. Then wrapping a JFrame around this is easy, as is wrapping a JApplet. Unless you want your applet to have a JFrame pop up from it? That's not the default UI experience people expect from an applet. But what is your goal?
Feb 6 '08 #3

P: 13
Thank you for your replies. I apologize for not using CODE tags, it won't happen again.

I figured it out, took a couple of hours though. For anyone who has the same problem please see

they were of great help to me. Really, all you need to do is change JFrame to JApplet (not Applet)

however, there are little minute differences that will, if your code is large in size, will take a while to debug. mine was this: when using ImageIcon, JFrame allows for just inputing the file name in the common directory (ie "picture.gif"), JApplet however requires the full path name ("c:/whatever/picture.gif") also notice this is a foreslash not backslash

thanks again
Feb 7 '08 #4

P: 13
java.awt.Image is abstract; cannot be instantiated
malformed URL

This took me about 8 stupid hours to figure out. Above is a few errors I got a long the way, can't remember the rest.

Note to those who want to call an image from the web for the imageicon for a converted JFrame to JApplet program:
Expand|Select|Wrap|Line Numbers
  1. new JLabel(new ImageIcon(""));
did not work as planned

here is what did work (code snippets):
Expand|Select|Wrap|Line Numbers
  1. JLabel text;
  2.  public void init(){
  3.     try{
  4.     URL url = new URL("");
  5.     ImageIcon ii = new ImageIcon(url);
  7.     text = new JLabel(ii);
  8.     }
  10.     catch(Exception e){
  11.         text = new JLabel("I'm a Simple Program");
  12.     }
  13.     }
hope this helps someone
Feb 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.