The Artima Developer Community
Sponsored Link

Java Answers Forum
TextField on an Applet

7 replies on 1 page. Most recent reply: Apr 30, 2002 11:43 AM by Thang Nguyen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 7 replies on 1 page
Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

TextField on an Applet Posted: Apr 28, 2002 9:07 PM
Reply to this message Reply
Advertisement
Hi there, I am trying to set capacity for a text field on applet, but it does not follow my code (I can't strict the length of KB innput in the text box):

TextField TF = new TextField(10); //Take input with max length is 10 characters
//Or:
TextField TF = new TextField("", 10);

Also, the text field 'TF' does not automatically get the keybooard focus though the cursor is flashing in the text box. My code is:

TF.requestFocus(); //get KB focus when load applet
TF.setEditable(true); //'TF' is writable

I have to click mouse in the text box in order to write in (I'm supposed not to, as the above code should support KB focus automatically).

Please give a hint. Thanks a lot!
Thang


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: TextField on an Applet Posted: Apr 29, 2002 12:14 AM
Reply to this message Reply
These statements don't limit the size of the TextField but limits the display size of the TextField
TextField TF = new TextField(10); //Take input with max length is 10 characters 
//Or:
TextField TF = new TextField("", 10); 
You need to handle that by yourself (add KeyListening).

Thomas,

Pavan Kumar Keely

Posts: 7
Nickname: pavan
Registered: Mar, 2002

Re: TextField on an Applet Posted: Apr 29, 2002 3:55 AM
Reply to this message Reply
hi Thang....

Actually when you say "requestFocus" the focus will be transfered to that component...Thta'll absolutely work...but only constraint is that the component should be visible before you say "requestFocus"....

If you have anything else .....ot still have doubt....please do mail me at keelypavan@hotmail.com

bye

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: TextField on an Applet Posted: Apr 29, 2002 2:17 PM
Reply to this message Reply
Pavan

I actually did that but sometimes it works, sometimes it does not. I don't know why.
Here is the code:

init()
{
...

add(textfield); // add a text field to an applet
textfield.setEditable(true);
textfield.requestFocus();

...
}

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: TextField on an Applet Posted: Apr 29, 2002 3:09 PM
Reply to this message Reply
Give most of the code !
We can't do anything with that !
:-(
Thomas,

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: TextField on an Applet Posted: Apr 30, 2002 10:18 AM
Reply to this message Reply
Sorry for being to short. Here is my code

/* APPLET:
This exercise has 2 text fields which take two integers from the user.The first number will divide the second one and the applet display the result. If zero is divided, a message is displayed, "Result: cannot divide zero!" . There is a button to perform the division.
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class DivideTwo extends Applet implements ActionListener
{
Label lblPromt = new Label("Enter values");
Label lblResult = new Label();

TextField tfNum1 = new TextField(5);
TextField tfNum2 = new TextField(5);

Button btnCalc = new Button("Divide");

public void init()
{
add(lblPromt);
add(tfNum1);
tfNum1.requestFocus(); //this seems to work fine from jview /a ...,
// but not often when test on Debug /Start(F5)
add(tfNum2);
add(btnCalc);
btnCalc.addActionListener(this);
add(lblResult);
}
public void actionPerformed(ActionEvent e)
{
int iNum1, iNum2, iResult;
iNum1 = Integer.parseInt(tfNum1.getText());
iNum2 = Integer.parseInt(tfNum2.getText());
iResult = iNum1 / iNum2;

if (iNum2 == 0)
{
lblResult.setText("Result: Cannot divide zero!");
//lblResult.setLocation(30, 50);
}
else
{
lblResult.setText("Result: " + iResult);
//lblResult.setLocation(30, 50);
}
}
}

/* This applet runs fine but two problems I am facing to:

1) When iNum2 = 0, the message "Result: cannot divide zero!" does not appear!!! - It appear nothing, instead.

2) When iNum2 != 0, the message "Result: ..." is truncated!! ???

Thanks for our your help! -- Thang
*/

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: TextField on an Applet Posted: Apr 30, 2002 11:24 AM
Reply to this message Reply
I have added a paint method which sets the focus to first field. When reading two numbers and divising one by another , you should use try-catch. Your problem was that you were checking that num2 is zero or not after you were dividing num1 by num2. If num2 is zero then num1/num2 will raise exception and your if statement never gets executed. So, now I am checking if num2 is zero before dividing num1 by num2.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
 
public class DivideTwo extends Applet implements ActionListener {
	Label lblPromt = new Label("Enter values");
	Label lblResult = new Label( "Result..................................... ");
 
	TextField tfNum1 = new TextField(5);
	TextField tfNum2 = new TextField(5);
 
	Button btnCalc = new Button("Divide");
 
	public void init() {
		add(lblPromt);
		add(tfNum1);
		tfNum1.requestFocus(); //this seems to work fine from jview /a ..., 
		// but not often when test on Debug /Start(F5)
		add(tfNum2);
		add(btnCalc);
		btnCalc.addActionListener(this);
		add(lblResult);
	}
	
	public void paint ( Graphics g ) {
		tfNum1.requestFocus();
	}
	
	public void actionPerformed(ActionEvent e) {
		int iNum1, iNum2, iResult;
		try {
			iNum1 = Integer.parseInt(tfNum1.getText());
			iNum2 = Integer.parseInt(tfNum2.getText());		
			if (iNum2 == 0)	{
				lblResult.setText("Result: Cannot divide zero!");				
			}
			else {
				iResult = iNum1 / iNum2;
				lblResult.setText("Result: " + iResult);				
			}
		}
		catch ( Exception e1 ) {		
			lblResult.setText("Invalid Number(s)...");				
		}
		
	}
}
 

Thang Nguyen

Posts: 18
Nickname: thang
Registered: Apr, 2002

Re: TextField on an Applet Posted: Apr 30, 2002 11:43 AM
Reply to this message Reply
It works so perfectly, Kishori Sharan - Thanks alot.

The requestFocus() does not really work when I test the program by use Debug/Start. It works fine with jview /a ... Maybe some problem with my version.

Thang

Flat View: This topic has 7 replies on 1 page
Topic: Telnet protocol Previous Topic   Next Topic Topic: Bubblesort

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use