Solved

Visual J++ Applet/Application

Posted on 1997-09-08
5
389 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

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
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…
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…

919 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

21 Experts available now in Live!

Get 1:1 Help Now