Solved

Visual J++ Applet/Application

Posted on 1997-09-08
5
385 Views
Last Modified: 2013-11-23
I am using Visual J++ to write Java applets/application. I use
the Resource Wizard to generate code for my GUI which consists of
dialog boxes and buttons. Then I edit the the init() method
in my applet class to contain the following generic lines

init()
{
setFont(****) ;
Dialog ctrls = new Dialog(this);
ctrls.CreateContols();
}

But however the dialog shows up only when I run it as an applet
and does not show when I run it as an application. Only the frame
appears. (I am using the Applet Wizard generated class files to
run both as an applet and application). Any suggestions.
Thanks,
Ganz
0
Comment
Question by:chocka
  • 3
5 Comments
 
LVL 4

Expert Comment

by:rembo
ID: 1226639

What are you passing to the Dialog as "this"?

-Tony


0
 

Author Comment

by:chocka
ID: 1226640
Comment from Rembo: What are you passing to the Dialog as "this"?       
                                    -Tony
Reply:
Hi Tony,
Its a pointer to the Applet Wizard generated class (say AppletClass) which contains the main() method. The class is implemented as follows:

public AppletClass extends Applet implements Runnable {

//main()
//init() etc
0
 

Author Comment

by:chocka
ID: 1226641
Hi Tony,
I think you hit the nail on the head. I got to it to work
by repeating the code in the init method in the constructor
of the frame class. But I believe this can be avoided, as
the Applet Wizard generated code suggests. Waiting for your
comments.
Thanks
Ganz
0
 
LVL 1

Accepted Solution

by:
vmanocha earned 50 total points
ID: 1226642
In the file that contains your applet class, you should also have a main method which got generated using Visual J++. It should look something like this:

//========

// The following code starts the applet running within the frame window.
// It also calls GetParameters() to retrieve parameter values from the
// command line, and sets m_fStandAlone to true to prevent init() from
// trying to get them from the HTML page.
            //----------------------------------------------------------------------
MyApplet applet_MyApplet = new MyApplet();

frame.add("Center", applet_MyApplet);
applet_MyApplet.m_fStandAlone = true;
applet_MyApplet.GetParameters(args);
applet_MyApplet.init();
applet_MyApplet.start();
frame.show();

//=======

this code here calls the init and start for you applet, so you should not need to put the same code in the frame class constructor. can you check to make sure if you have similar pice of code in your applet file. if you have the code and it still does not work, then i would appreciate if you can put your code here.

thanks.
0
 

Author Comment

by:chocka
ID: 1226643
Hi Mano,
Thanks for the help. But I do have the main() as you posted. It does not work despite that. I have posted the three classes of my code below.I have edited out unrelevant methods. The files I have posted are 1) the applet class file 2) The resource wizard generated file 3) The Frame class file in that order.

File 1. The Applet Class File.
=================================================================
//******************************************************************************
// Bulls.java:  Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import java.util.*;
import BullsFrame;
import TeamDlg;

//==============================================================================
// Main Class for applet Bulls
//
//==============================================================================
public class Bulls extends Applet
{
        TeamDlg ctrls ;
        private Vector Name = new Vector(3, 3);
        private Vector Ht = new Vector(3, 3);
       
        // STANDALONE APPLICATION SUPPORT:
        //              m_fStandAlone will be set to true if applet is run standalone
        //--------------------------------------------------------------------------
        private boolean m_fStandAlone = false;

        // STANDALONE APPLICATION SUPPORT
        //      The main() method acts as the applet's entry point when it is run
        // as a standalone application. It is ignored if the applet is run from
        // within an HTML page.
        //--------------------------------------------------------------------------
        public static void main(String args[])
        {
                // Create Toplevel Window to contain applet Bulls
                //----------------------------------------------------------------------
                BullsFrame frame = new BullsFrame("Bulls");

                // Must show Frame before we size it so insets() will return valid values
                //----------------------------------------------------------------------
                frame.show();
        frame.hide();
                frame.resize(frame.insets().left + frame.insets().right  + 320,
                                         frame.insets().top  + frame.insets().bottom + 240);

                // The following code starts the applet running within the frame window.
                // It also calls GetParameters() to retrieve parameter values from the
                // command line, and sets m_fStandAlone to true to prevent init() from
                // trying to get them from the HTML page.
                //----------------------------------------------------------------------
                Bulls applet_Bulls = new Bulls();

                frame.add("Center", applet_Bulls);
                applet_Bulls.m_fStandAlone = true;
                applet_Bulls.init();
                applet_Bulls.start();
        frame.show();
               
        }
        public void init()
        {
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------
        //resize(320, 240);

                // TODO: Place additional initialization code here
                setFont( new Font("Helvetica", Font.PLAIN, 12));
                ctrls = new TeamDlg ( this);
        ctrls.CreateControls();
        }

=================================================================
2. The Resource Wizard Generated Class File is as follows
=============================================================
//------------------------------------------------------------------------------
// TeamDlg.java:
//              Implementation for container control initialization class TeamDlg
//
// WARNING: Do not modify this file.  This file is recreated each time its
//          associated .rct/.res file is sent through the Java Resource Wizard!
//
// This class can be use to create controls within any container, however, the
// following describes how to use this class with an Applet.  For addtional
// information on using Java Resource Wizard generated classes, refer to the
// Visual J++ 1.1 documention.
//
// 1) Import this class in the .java file for the Applet that will use it:
//
//      import TeamDlg;
//
// 2) Create an instance of this class in your Applet's 'init' member, and call
//    CreateControls() through this object:
//
//      TeamDlg ctrls = new TeamDlg (this);
//      ctrls.CreateControls();
//
// 3) To process events generated from user action on these controls, implement
//    the 'handleEvent' member for your applet:
//
//      public boolean handleEvent (Event evt)
//      {
//
//      }
//
//------------------------------------------------------------------------------
import java.awt.*;
import DialogLayout;

public class TeamDlg
{
        Container    m_Parent       = null;
        boolean      m_fInitialized = false;
        DialogLayout m_Layout;

        // Control definitions
        //--------------------------------------------------------------------------
        Button        ADD;
        Button        LIST;
        TextField     NAME;
        TextField     HEIGHT;
        Label         IDC_STATIC1;
        Label         IDC_STATIC2;
        List          LISTBOX;


        // Constructor
        //--------------------------------------------------------------------------
        public TeamDlg (Container parent)
        {
                m_Parent = parent;
        }

        // Initialization.
        //--------------------------------------------------------------------------
        public boolean CreateControls()
        {
                // Can only init controls once
                //----------------------------------------------------------------------
                if (m_fInitialized || m_Parent == null)
                        return false;

                // Parent must be a derivation of the Container class
                //----------------------------------------------------------------------
                if (!(m_Parent instanceof Container))
                        return false;

                // Since there is no way to know if a given font is supported from
                // platform to platform, we only change the size of the font, and not
                // type face name.  And, we only change the font if the dialog resource
                // specified a font.
                //----------------------------------------------------------------------
                Font OldFnt = m_Parent.getFont();
               
                if (OldFnt != null)
                {
                        Font NewFnt = new Font(OldFnt.getName(), OldFnt.getStyle(), 12);

                        m_Parent.setFont(NewFnt);
                }

                // All position and sizes are in Dialog Units, so, we use the
                // DialogLayout manager.
                //----------------------------------------------------------------------
                m_Layout = new DialogLayout(m_Parent, 320, 240);
                m_Parent.setLayout(m_Layout);
                m_Parent.addNotify();

                Dimension size   = m_Layout.getDialogSize();
                Insets    insets = m_Parent.insets();
               
                m_Parent.resize(insets.left + size.width  + insets.right,
                        insets.top  + size.height + insets.bottom);

                // Control creation
                //----------------------------------------------------------------------
                ADD = new Button ("Add");
                m_Parent.add(ADD);
                m_Layout.setShape(ADD, 42, 65, 23, 13);

                LIST = new Button ("List");
                m_Parent.add(LIST);
                m_Layout.setShape(LIST, 179, 87, 23, 14);

                NAME = new TextField ("");
                m_Parent.add(NAME);
                m_Layout.setShape(NAME, 29, 18, 84, 12);

                m_fInitialized = true;
                return true;
        }
}

================================================================
3. The Frame Class File App Wizard generated, I left unaltered.
I have included it below.
================================================================

//******************************************************************************
// BullsFrame.java:    
//
//******************************************************************************
import java.awt.*;
import java.util.*;
import TeamDlg;
//==============================================================================
// STANDALONE APPLICATION SUPPORT
//      This frame class acts as a top-level window in which the applet appears
// when it's run as a standalone application.
//==============================================================================
class BullsFrame extends Frame
{
        TeamDlg ctrls ;
        private Vector Name = new Vector(3, 3);
        private Vector Ht = new Vector(3, 3);

        // BullsFrame constructor
        //--------------------------------------------------------------------------
        public BullsFrame(String str)
        {
                // TODO: Add additional construction code here
                super (str);
      }
//handleEvent() etc..





0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

759 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