Solved

How to convert Application to Applet

Posted on 1997-10-02
1
188 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
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now