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 ==============================
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 ==============================