Solved

How to convert Application to Applet

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 39
Java Server Faces parameter pass? 6 39
JList custom Cell Renderer refresh 15 53
eclipse shortcuts 9 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

895 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