Solved

How to convert Application to Applet

Posted on 1997-10-02
1
192 Views
Last Modified: 2013-11-23
I have an application which contains a number of different frames. I would like to modify the code so that I can also run it as an applet. I'm not sure what code needs to be changed or added. Also, my application makes use of dialog boxes, frame titles, etc...) Will these still work within an applet?

Thanks for your help!

Carey Boldenow
carey.boldenow@attws.com
0
Comment
Question by:boldec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
eugenem earned 100 total points
ID: 1227794
It depends on code. If your class extends Frame, move all widgets initialization from constructor to init(), your class should extend Applet, in main() create one Frame() and one instance of your class, run init() of your class, add your class to Frame. So when class is running as Applet, it uses init(), and when as application, it uses main(). There is an example. Just look at init/main functions.

// SpecTcl generated class test, version 1.1

package DFS;
import java.awt.*;

public class test extends java.applet.Applet {            

// a slot to hold an arbitrary object pointer that can
// be filled in by the app. and referenced in actions
public Object arg;

public Button button_1;

//methods to support form introspection
public static String names[] = {
      "button_1",
};
public String[] getNames() {
      return names;
}

//There should be an easier way to do this
public Object[] getWidgets() {
      Object[] list = new Object[1];
      list[0] = button_1;
      return list;
}

public void init() {

      // main panel
      GridBagLayout grid = new GridBagLayout();
      int rowHeights[] = {0,30,30};
      int columnWidths[] = {0,30,30};
      double rowWeights[] = {0.0,0.0,0.0};
      double columnWeights[] = {0.0,0.0,0.0};
      grid.rowHeights = rowHeights;
      grid.columnWidths = columnWidths;
      grid.rowWeights = rowWeights;
      grid.columnWeights = columnWeights;

      button_1 = new Button();
      button_1.setLabel("button");
      this.add(button_1);
      

      // Geometry management
      GridBagConstraints con = new GridBagConstraints();
      reset(con);
      con.gridx = 1;
      con.gridy = 1;
      con.anchor = GridBagConstraints.CENTER;
      con.fill = GridBagConstraints.NONE;
      grid.setConstraints(button_1, con);


      // Resize behavior management and parent heirarchy
      setLayout(grid);

      // Give the application a chance to do its initialization
      super.init();
}

public boolean handleEvent(Event event) {
      if (event.id==event.KEY_ACTION && event.key==event.F4 && event.modifiers==event.ALT_MASK) {  // Alt-F4 always exits
            System.exit(3);
      } else
            return super.handleEvent(event);
      return true;
}

public static void main(String[] args) {
    Frame f = new Frame("test Test");
    test win = new test();
    win.init();
    f.add("Center", win);
    f.pack();
    f.show();
}

private void reset(GridBagConstraints con) {
    con.gridx = GridBagConstraints.RELATIVE;
    con.gridy = GridBagConstraints.RELATIVE;
    con.gridwidth = 1;
    con.gridheight = 1;
 
    con.weightx = 0;
    con.weighty = 0;
    con.anchor = GridBagConstraints.CENTER;
    con.fill = GridBagConstraints.NONE;
 
    con.insets = new Insets(0, 0, 0, 0);
    con.ipadx = 0;
    con.ipady = 0;
}

}

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question