wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes TextField to the Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "TextField to the Applet" Watch "TextField to the Applet" New topic
Author

TextField to the Applet

Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
How I can add textfield which allow to user enter the interger ?
Thanks
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
If you're not using Swing, just add( textfield ); in your applet. You of course will have to create an instance of the textfield with TextField textfield = new TextField();
One thing to remember with text fields, any input in them is regarding as a String. So when you do textfield.getText();, you get a String. If you want them to enter an integer, you then have to use Integer.parseInt( string ); to get the integral value.
Hope that helps,
Jason
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Hi
I put the code in my Applet
TextField textfield = new TextField();
add( textfield );
textfield.getText();
But I don't see any kind of textfieldor box where I can enter the integer.
Here more code:
this.setForeground(Color.blue);
this.setBackground(Color.orange);
this.setFont(new Font("Sans", Font.ITALIC, 12));
this.setLayout(new FlowLayout(FlowLayout.RIGHT, 300, 350));


Button b;
b = new Button("OK");
this.add(b);

TextField textfield = new TextField();
add( textfield );
textfield.getText();
Thanks
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Oops, you probably will want to give it a size
TextField textfield = new TextField( 5 );
You should be able to see it then.
Also, don't do textfield.getText(); until you want to retrieve the text. For instance, when they press on a button or whatever. It also needs to go into a String, you can't just call it by itself.
Jason
[This message has been edited by jason adam (edited November 14, 2001).]
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Thanks ...
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Does this function takes any parameters?
textfield.getText();
Thank you
Mike Shn
Ranch Hand

Joined: May 26, 2001
Posts: 149
Hi
Would please look in my code? This code compeles but I can get a value from the applet
/ Import packages
import java.applet.*;
import java.awt.*;

public class MandelbrotApplet extends Applet {

// Declaration
private int numberOfIteration=50;
private int line=0;
private boolean InputException[]={false,false};
private double absoluteValue[];
ComplexNumber c =new ComplexNumber(0.34,-0.8);
String k;


// Main Function
public void init(){

// String k;
this.setForeground(Color.blue);
this.setBackground(Color.orange);
this.setFont(new Font("Sans", Font.ITALIC, 12));

/*
this.setLayout(new FlowLayout(FlowLayout.RIGHT, 300, 350));
*/

Button b;
b = new Button("OK");
this.add(b);

TextField textfield = new TextField(6);
this.add(textfield);
k = textfield.getText(
System.out.println(k);

// Declaration
absoluteValue=new double[numberOfIteration];


// To catch if user haven't input the ComplexNumber
try{
c = ComplexNumber.valueOf(textfield.getText()); }

// Catch that output the message
catch(Exception e) {InputException[1]=true;}

for(int p=0;p<numberOfIteration;p++)<br /> {<br /> if((absoluteValue[p]=MandelbrotUtilities.returnNthIteration(c,p).getAbs())>2)
break;
}
} // end of the init function


// Function that outputs on the screen
public void paint(Graphics G) {
System.out.println("k");
if(InputException[1]) toScreen("Default C parameters",G);

// Call the toScreen function and pass the parameters
toScreen("C(complex number) is: "+ c,G);
toScreen("Number of Iteration: "+ numberOfIteration,G);



for(int j=1;j<numberOfIteration;j++)<br /> {<br /> if(absoluteValue[j]>2)
{
toScreen("Diverge on "+ j +" iteration",G);
break;
}

G.drawLine((j-1)*this.getSize().width/100,
(int)(absoluteValue[j-1]*100)+this.getSize().height/4,j*this.getSize().width/100,
(int)(absoluteValue[j]*100)+this.getSize().height/4);
}
} // end of paint function
// Function that outputs text
private void toScreen(String text,Graphics O){

// call function
O.drawString(text, 10,((10*line)+10));
// increment number line
line++;

}// end of the toScreen function
}// end of the function
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: TextField to the Applet