Click to See Complete Forum and Search --> : Cannot read or write from a Java Applet


lmarashdeh
October 5th, 1999, 03:22 AM
Dear Sir/Madam:
I built a Java Applet as described in the demo program
by Java Applet Wizard and insert in it a dialog with buttons
and Textfields and it couldn't read from the Textfield and write
to the static label .I attached to this email the whole Java
Workspace.Please help me as soon as possible.
What's the problem and give the solution.
T product name:Visual J++ 1.1, version:1.1,
product ID:59444-270-8254612-54716 ,
Operating system:windows NT 4.0 sp5 .
version :4.0, service pack: 5, using ie5 browser.
with Intel pentiumIII 450 MHZ, 128KB RAM.
Name:Loai Marashdeh, email: lmarashdeh@sps.net.sa.
telephone:966 2 682 4556.

amaresh
October 7th, 1999, 12:50 PM
If you can provide some more information, I can understand the problem better. For example, what happens when you try tio write in to the text field / label? How are you doing it? Please post the relevant part of the code so that I can see what's happening..
Thanks and Regards,
Amaresh

lmarashdeh
October 9th, 1999, 07:38 AM
Thank you for your reply.
Will my problem is that I made a java applet
in the using the java applet wizard in visual
J++ 1.1 and I made for it a java resource wizard
also ,I made a dialog that reads a text from the
textfield and write it back to the static label.
the action handle event can't work even so that I
used as written in the Demo file for the Java applet from the help files of Visual java,I thing
that the wizard in visual J++ 1.1 is not working well.I will but the three calsses for the project.
this is the main class:
//******************************************************************************
// loai6.java: Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;
import IDD_DIALOG1;
//==============================================================================
// Main Class for applet loai6
//
//==============================================================================
public class loai6 extends Applet
{
IDD_DIALOG1 ctrls;

// loai6 Class Constructor
//--------------------------------------------------------------------------
public loai6()
{
// TODO: Add constructor code here
}

// APPLET INFO SUPPORT:
// The getAppletInfo() method returns a string describing the applet's
// author, copyright date, or miscellaneous information.
//--------------------------------------------------------------------------
public String getAppletInfo()
{
return "Name: loai6\r\n" +
"Author: Unknown\r\n" +
"Created with Microsoft Visual J++ Version 1.1";
}


// The init() method is called by the AWT when an applet is first loaded or
// reloaded. Override this method to perform whatever initialization your
// applet needs, such as initializing data structures, loading images or
// fonts, creating frame windows, setting the layout manager, or adding UI
// components.
//--------------------------------------------------------------------------
public void init()
{

IDD_DIALOG1 ctrls = new IDD_DIALOG1 (this);
ctrls.CreateControls();
// 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
}

// Place additional applet clean up code here. destroy() is called when
// when you applet is terminating and being unloaded.
//-------------------------------------------------------------------------
public void destroy()
{
// TODO: Place applet cleanup code here
}

// loai6 Paint Handler
//--------------------------------------------------------------------------
public void paint(Graphics g)
{
//g.drawString("Created with Microsoft Visual J++ Version 1.1", 10, 20);
}

// The start() method is called when the page containing the applet
// first appears on the screen. The AppletWizard's initial implementation
// of this method starts execution of the applet's thread.
//--------------------------------------------------------------------------
public void start()
{
// TODO: Place additional applet start code here
}

// The stop() method is called when the page containing the applet is
// no longer on the screen. The AppletWizard's initial implementation of
// this method stops execution of the applet's thread.
//--------------------------------------------------------------------------
public void stop()
{
}

public boolean action(Event evt, Object arg)
{
if(evt.target instanceof Button)
{
String val = ctrls.IDC_EDIT1.getText();
ctrls.IDC_STATIC1.setText(val);
return true;
}
return false;
}



// TODO: Place additional applet code here

}




here is the IDD_DIALOG1 class:
//------------------------------------------------------------------------------
// IDD_DIALOG1.java:
// Implementation for container control initialization class IDD_DIALOG1
//
// 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 IDD_DIALOG1;
//
// 2) Create an instance of this class in your Applet's 'init' member, and call
// CreateControls() through this object:
//
// IDD_DIALOG1 ctrls = new IDD_DIALOG1 (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 IDD_DIALOG1
{
Container m_Parent = null;
boolean m_fInitialized = false;
DialogLayout m_Layout;

// Control definitions
//--------------------------------------------------------------------------
Button IDC_BUTTON1;
TextField IDC_EDIT1;
Label IDC_STATIC1;


// Constructor
//--------------------------------------------------------------------------
public IDD_DIALOG1 (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(), 8);

m_Parent.setFont(NewFnt);
}

// All position and sizes are in Dialog Units, so, we use the
// DialogLayout manager.
//----------------------------------------------------------------------
m_Layout = new DialogLayout(m_Parent, 186, 95);
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
//----------------------------------------------------------------------
IDC_BUTTON1 = new Button ("Button1");
m_Parent.add(IDC_BUTTON1);
m_Layout.setShape(IDC_BUTTON1, 63, 42, 50, 14);

IDC_EDIT1 = new TextField ("");
m_Parent.add(IDC_EDIT1);
m_Layout.setShape(IDC_EDIT1, 57, 19, 76, 14);

IDC_STATIC1 = new Label ("Static", Label.LEFT);
m_Parent.add(IDC_STATIC1);
m_Layout.setShape(IDC_STATIC1, 35, 66, 19, 8);

m_fInitialized = true;
return true;
}
}




Here is the DialogLayout class:// This is a part of the Microsoft Visual J++ library.
// Copyright (C) 1996 Microsoft Corporation
// All rights reserved.

import java.util.Hashtable;
import java.awt.LayoutManager;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.FontMetrics;
import java.awt.Insets;
import java.awt.Label;

//
// class DialogLayout
//
// DialogLayout is a simple layout manager which works with what the Win32
// API calls "dialog logical units" (DLUs). DLUs are resolution independent
// coordinates which work well for laying out controls on a dialog box. The
// mapping from DLUs to pixels is based on the font in use in the dialog box.
// An x-coordinate DLU is described as 1/4 (.25) of the of the average character
// width of the font used in the dialog. A y-coordinate DLU is described as
// 1/8 (.125) of the character height used in the dialog. One tricky issue to
// note: The average character width is not the average of all characters --
// rather it is the average of all alpha characters both uppercase and
// lowercase. That is, it is the extent of the string "a...zA...Z" divided
// by 52.
//
// This class allows you to associate a Rectangle (x, y, width, height) with a
// Component in a Container. If called upon to layout the container, this
// layout manager will layout based on the translation of dialog units to
// pixels.
//

public class DialogLayout
implements LayoutManager
{
protected Hashtable m_map = new Hashtable();
protected int m_width;
protected int m_height;

// DialogLayout methods

public DialogLayout(Container parent, int width, int height)
{
Construct(parent, width, height);
}

public DialogLayout(Container parent, Dimension d)
{
Construct(parent, d.width, d.height);
}

public void setShape(Component comp, int x, int y, int width, int height)
{
m_map.put(comp, new Rectangle(x, y, width, height));
}

public void setShape(Component comp, Rectangle rect)
{
m_map.put(comp, new Rectangle(rect.x, rect.y, rect.width, rect.height));
}

public Rectangle getShape(Component comp)
{
Rectangle rect = (Rectangle)m_map.get(comp);
return new Rectangle(rect.x, rect.y, rect.width, rect.height);
}

public Dimension getDialogSize()
{
return new Dimension(m_width, m_height);
}

// LayoutManager Methods

public void addLayoutComponent(String name, Component comp) { }
public void removeLayoutComponent(Component comp) { }

public Dimension preferredLayoutSize(Container parent)
{
return new Dimension(m_width, m_height);
}

public Dimension minimumLayoutSize(Container parent)
{
return new Dimension(m_width, m_height);
}

public void layoutContainer(Container parent)
{
int count = parent.countComponents();
Rectangle rect = new Rectangle();
int charHeight = getCharHeight(parent);
int charWidth = getCharWidth(parent);
Insets insets = parent.insets();
FontMetrics m = parent.getFontMetrics(parent.getFont());

for (int i = 0; i < count; i++)
{
Component c = parent.getComponent(i);
Rectangle r = (Rectangle)m_map.get(c);
if (r != null)
{
rect.x = r.x;
rect.y = r.y;
rect.height = r.height;
rect.width = r.width;
mapRectangle(rect, charWidth, charHeight);
if (c instanceof Label)
{
// Adjusts for space at left of Java labels.
rect.x -= 12;
rect.width += 12;
}

rect.x += insets.left;
rect.y += insets.top;
c.reshape(rect.x, rect.y, rect.width, rect.height);
}
}
}

// Implementation Helpers

protected void Construct(Container parent, int width, int height)
{
Rectangle rect = new Rectangle(0, 0, width, height);
mapRectangle(rect, getCharWidth(parent), getCharHeight(parent));
m_width = rect.width;
m_height = rect.height;
}

protected int getCharWidth(Container parent)
{
FontMetrics m = parent.getFontMetrics(parent.getFont());
String s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int width = m.stringWidth(s) / s.length();

if (width <= 0)
width = 1;
return width;
}

protected int getCharHeight(Container parent)
{
FontMetrics m = parent.getFontMetrics(parent.getFont());
int height = m.getHeight();
return height;
}

protected void mapRectangle(Rectangle rect, int charWidth, int charHeight)
{
rect.x = (rect.x * charWidth) / 4;
rect.y = (rect.y * charHeight) / 8;
rect.width = (rect.width * charWidth) / 4;
rect.height = (rect.height * charHeight) / 8;
}
}

and thank you for atempting to help me.
Regards,
Loai.

poochi
October 9th, 1999, 01:56 PM
??? Are u using JDK1.0.x ? Are u using VisualJ++ 1.x ? Anyway ...



public void init()
{
// Here is the problem ...

IDD_DIALOG1 ctrls = new IDD_DIALOG1 (this); // You are creating a Dialog and
// assign it to a local variable
// instead of assigning it to the global variable , ctrls .
ctrls.CreateControls();





Do like this ...



ctrls = new IDD_DIALOG1 (this);
ctrls.CreateControls();





Java has improved alot. Try to use newer versions of IDEs and JDK.

Poochi...

lmarashdeh
October 10th, 1999, 04:11 AM
Man you are a genius.
From my heart thank you very very very much.
It works now. Will actually I am using VisualJ++1.1
What’s your advise about using this version. I have
A better version its visualJ++6.0,but I can’t use
Very much, I am not used to it I feel that VJ++1.1
Is better.Once again you saved my life THANK YOU.

Best Regards;
Loai Marashdeh.

poochi
October 10th, 1999, 08:54 AM
You are welcome Loai. :-) I have used VJ++ 1.1 and VJ++ 6.0. The VJ++ 1.1's Working
environment is better than VJ++6.0. But VJ++ is using older version of JDK. It's 1.0.x.
In java , the message handling concept has been totally changed in JDK1.1.x. Nowadays
every one is using JDK1.2 and everyone is talking about swing. All projects are
written in JDK1.1.x and higher version of java. No one is using JDK1.0.x. So ,
it's waste of time to work on JDK1.0.x. If you dont like VJ++6.0 , download VisualCafe.
In my opinion , It's the best IDE. Try it. And one more problem with VJ++ is microsoft has announced that
it's not going to support VJ++ anymore. Sun has filed a case against microsoft. It says
microsoft has voilated license agreement. VJ++'s future is in dark.

Good luck..

Poochi...