Solved

How to convert Application to Applet

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.

821 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