I started making my own little applet with buttons and labels and text fields, but I want to when you enter a certain thing in the text field it does something. I tried if(fieldTextField.equals(mystringhere)) {
but it doesn't work. Could you post an example of what I want?
Asked
Active
Viewed 102 times
-1

Cœur
- 37,241
- 25
- 195
- 267

Naame Nameee
- 63
- 1
- 3
- 8
-
Post the entire code please. – BackSlash Apr 07 '13 at 09:07
-
someone might steal it no. – Naame Nameee Apr 07 '13 at 09:08
-
@NaameNameee do you work for NASA? no, so don't be afraid, noone is stealing your code. – BackSlash Apr 07 '13 at 09:10
-
fine. here: http://pastebin.com/ABJixz6Z – Naame Nameee Apr 07 '13 at 09:14
2 Answers
0
Take a look at event listeners (http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html). Also you shouldn't be comparing the TextField itself to the String, but it's text, using the getText() method.
EDIT: Better than the link above: http://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

laobeylu
- 283
- 3
- 14
0
You have to use a KeyListener, so that you can listen to keyboard and check your text everytime the user types something in your field:
KeyListener kl = new KeyAdapter(){
public void keyTyped(KeyEvent evt)){
if(yourTextField.getText().equals(yourString){
//do something here
}
}
};
yourTextField.addKeyListener(kl);
Note that yourTextField
must be an instance variable or a local final variable in order to be used in the KeyListener methods

BackSlash
- 21,927
- 22
- 96
- 136
-
-
@NaameNameee Well, if your variable is `field1TextField` it already is an instance variable, so you don't need to make it final. Also, i see that all your GUI components are from java.awt package. When you can, please use javax.swing package, it is much better, there are two links that explain you why swing is better than awt: [link 1](http://stackoverflow.com/questions/408820/what-is-the-difference-between-swing-and-awt) [link 2](http://edn.embarcadero.com/article/26970) – BackSlash Apr 07 '13 at 09:33
-
my field1TextField is a simple textfield. `TextField field1TextField = new TextField("Insert Text Here");` – Naame Nameee Apr 07 '13 at 09:34
-
and one more thing, javax.swing? i dont know how to use that. ill post another question about that – Naame Nameee Apr 07 '13 at 09:38
-
-