how to make what is in textfeild be displayed

/*

* File: CyberPet.java

* Author: Java, Java, Java

* Description: This class represents a CyberPet that can

* eat and sleep on command. This version incorporates

* a public getState() method to report the pet's state.

*/

publicclass CyberPet

{

privateboolean isEating =true;// CyberPet's state

privateboolean isSleeping =false;

privateboolean isThinking =false;

private String name ="no name";// CyberPet's name

public CyberPet (String str)// Constructor method

{

name = str;

}

publicvoid setName (String str)// Access method

{

name = str;

}// setName()

public String getName()

{

return name;// Return CyberPet's name

}// getName()

publicvoid eat()// Start eating

{

isEating =true;// Change the state

isSleeping =false;

isThinking =false;

return;

}// eat()

publicvoid sleep()// Start sleeping

{

isSleeping =true;// Change the state

isEating =false;

isThinking =false;

return;

}// sleep()

publicvoid think()

{

isThinking =true;

isSleeping =false;

isEating =false;

return;

}

public String getState ()

{

if (isEating)

return"Eating";// Exit the method

if (isSleeping)

return"Sleeping";// Exit the method

if (isThinking)

return"Thinking";

return"Error in State";// Exit the method

}// getState()

public String toString()

{

return name +" is " + getState();

}

}// CyberPet

/*

* Description: This apply provides a graphical user

* interface to the CyberPet class. The interface consists

* of two Buttons that can be clicked to tell the CyberPet

* to eat or drink, and a TextField which reports the

* CyberPet's state.

*

* The interface is initialized in the init() method and

* user actions are handled in the actionPerformed() method.

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

//STARTING COMMENT LINE ENCLOSING CLASS

publicclass CyberPetAppletextends Appletimplements ActionListener

{

// Declare instance variables

private CyberPet pet1;// The CyberPet

private Label nameLabel;// A Label

private TextField stateField;// A TextField

private TextField petNameField;

private Button eatButton, sleepButton, thinkButton;// Two Buttons

//* The init() method instantiates the instance variables, including both the

//* CyberPet (pet1) and the GUI elements that are displayed on the applet.

publicvoid init()

{

petNameField =new TextField(20);

petNameField.setText((" "));

petNameField.addActionListener(this);

petNameField.setEditable(true);

pet1 =new CyberPet(petNameField.getText());// CyberPet

// Create the GUI components

nameLabel =new Label("Hi! My name is " + pet1.getName() +

" and currently I am : ");

stateField =new TextField(12);

eatButton =new Button("Eat!");// Buttons

eatButton.addActionListener(this);// Assign the listener for Eat

sleepButton =new Button("Sleep!");

sleepButton.addActionListener(this);

thinkButton =new Button ("Think!");

thinkButton.addActionListener(this);

// Initialize the TextField

stateField.setText(pet1.getState());

stateField.setEditable(false);

// Add the components to the applet.

add(nameLabel);

add(stateField);

add(petNameField);

add(eatButton);

add(sleepButton);

add(thinkButton);

setSize(300,150);// Set the applet's size to 300 x 150 pixels

}// init

/*

* The actionPerformed() method is called whenever

* one of the buttons is pressed.

*/

publicvoid actionPerformed( ActionEvent e)

{

if (e.getSource() == eatButton)

pet1.eat();

elseif (e.getSource() == sleepButton)

pet1.sleep();

elseif (e.getSource() == thinkButton)

pet1.think();

stateField.setText(pet1.getState());

}//actionPerformed

}

//ENDING COMMENT LINE ENCLOSING CLASS*/

is there a way to make it auto refresh or to pause untill a name is entered into that 2nd text box

any help would be great

Thanks,

Tom

[9269 byte] By [tk3445a] at [2007-11-26 12:20:30]
# 1

> is there a way to make it auto refresh or to pause

> untill a name is entered into that 2nd text box

Disable the first Textbox and add an ActionListener to the second Textbox which will enable the first Textbox if the data is validated.

ps. I didn't read your code. Post only the relevant part of your code.

tymer99a at 2007-7-7 15:10:42 > top of Java-index,Archived Forums,Socket Programming...
# 2
i dont understand how to do that any pointers?Message was edited by: tk3445
tk3445a at 2007-7-7 15:10:42 > top of Java-index,Archived Forums,Socket Programming...
# 3

I'm still not sure if I understand your initial question.

// Here's an example of adding an action listener

nameTextField.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent e) {

// Code goes here

// if nameTextField is validated then ...

}

}

);

tymer99a at 2007-7-7 15:10:42 > top of Java-index,Archived Forums,Socket Programming...