Problem with TextArea in Applet

M

Madhavi

Hi,

I have an applet with two components one TextField and TextArea.
When I input something in TextField and press tab key to get focus to
TextArea, the focus comes to TextArea but the first typed letter
doesn't appear in textarea second letter onwards appears.
But when I shift focus using mouse there is no problem. Dissapearing
of first typed charactor happens only when I use tab key to switch
from one component to another.
Here is the sample code which I have written.

Please give some advice ASAP.

Thanks,
Madhavi Surisetty.

=================Class Findsub.java==================================
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Findsub extends Applet implements TextListener,
ItemListener {

Label lab1, lab2;
TextField text1;
TextArea area;
Checkbox caps;
String value1, value2;
int count;


//init method
//initialising all components
public void init() {

lab1 = new Label("Substring:");
text1 = new TextField(10);
lab2 = new Label("Type or paste a paragraph:");
area = new TextArea();
caps = new Checkbox("Ignore Capitalization");

add(lab1);
add(text1);
add(lab2);
add(area);
add(caps);

text1.addTextListener(this);
area.addTextListener(this);
caps.addItemListener(this);
}


public void textValueChanged(TextEvent e) {
value1 = text1.getText();
value2 = area.getText();

if (value1 != null && value1.length() > 0 &&
value2 != null && value2.length() > 0) {
count = no_of_occur(value1, value2);
repaint();
}
}


public void itemStateChanged(ItemEvent ie) {

value1 = text1.getText();
value2 = area.getText();

if (value1 != null && value1.length() > 0 &&
value2 != null && value2.length() > 0) {
if (caps.getState() == true) {
count = no_of_occur(value1.toLowerCase(),
value2.toLowerCase());
} else {
count = no_of_occur(value1, value2);
}
repaint();
}
}


// This method finds out the total no of occurrences of the
substring in a given string.
public int no_of_occur(String s1, String s2) {
int end, n;
count = 0;

if (s1.length() > s2.length()) {
return count;
}

n = s2.indexOf(s1);

while (n >= 0 && s2.length() > 0) {
end = s2.length();
count++;
n = n + s1.length();
s2 = s2.substring(n, end);
n = s2.indexOf(s1);
}

return count;
}

// ---- paint method ----
public void paint(Graphics g) {

g.drawString("Total no. of occurrences are:" + count, 180,
300);

}
} // ------------------ end Findsub class ------------
===========================================================================

===================HTML Code for Applet ======================
<html>
<title>Poly Test</title>
<body>
<applet code="Findsub.class" width=500 height=400>
</applet>
</body>
</html>
===================End HTML Code ==============================
 
A

Andrew Thompson

| Hi,
|
| I have an applet with two components one TextField and
TextArea.
| When I input something in TextField and press tab key to get
focus to
| TextArea, the focus comes to TextArea but the first typed
letter
| doesn't appear in textarea second letter onwards appears.
| But when I shift focus using mouse there is no problem.

try this
.....
| ===================HTML Code for Applet ======================
| <html>
| <title>Poly Test</title>
| <body>
| <applet code="Findsub.class" width=500 height=400>

<applet code="Findsub.class" width=800 height=400>

| </applet>
| </body>
| </html>
| ===================End HTML Code ==============================

Seriously. I could not reproduce the
behaviour you described with the mouse,
the cursor would be set to position '0' within
text area (invisible off the left) until I had
typed chars, but then, I added a call to
'validate()' in the init(), and other browser/vm
combos may allow you to start anywhere in
a text area..

HTH
 
A

Andrew Thompson

| .....
| | I have an applet with two components one TextField and..

BTW... *** Excellent example code *** :)
 
M

Madhavi

Hi,

Thank you for giving reply to my problem.U said that"I could not
reproduce the
behaviour you described with the mouse".But actually I have no
problem with the mouse.The problem comes only when I use "TAB" key on
the key board.What I mean is,after entering something in the
TextField,I am using the "TAB" key to place the cursor in the
TextArea.And after pressing tab key,if I type anything in the
TextArea, the first letter will not be shown(taken).And if use the
mouse to place the cursor in the textarea,everything will be fine.Hope
u got my problem.

Thanks,
Madhavi Surisetty.
 
A

Andrew Thompson

| Hi,
|
| Thank you for giving reply to my problem.

You are welcome.
Could I ask you not to top-post though?
Put your comments after each part, like I do.

| ...U said that"I could not
| reproduce the
| behaviour you described with the mouse".

No, the 'blinking cursor' appeared off to the
left, like with the 'tab'

|..But actually I have no
| problem with the mouse.

I did.

|..The problem comes only when I use "TAB" key on
| the key board.

It only occurs for _you_ with tab.
It happened with both tab _and_ mouse
for me..

|..What I mean is,after entering something in the
| TextField,I am using the "TAB" key to place the cursor in the
| TextArea.And after pressing tab key,if I type anything in the
| TextArea, the first letter will not be shown(taken).And if use the
| mouse to place the cursor in the textarea,everything will be fine.Hope
| u got my problem.

Yes, I _think_ I did. Let me ask you something.

What _exactly_ happened for you when you
made the change I suggested and viewed it
as a web page?

And to just repeat it..

| > | ===================HTML Code for Applet ======================
| > | <html>
| > | <title>Poly Test</title>
| > | <body>
| > | <applet code="Findsub.class" width=500 height=400>

| > <applet code="Findsub.class" width=800 height=400>

** what happens when _you_ change ** width **
to '800' and view in browser?

| > | </applet>
| > | </body>
| > | </html>
| > | ===================End HTML Code ==============================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top