This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JTextField/Reading Content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JTextField/Reading Content" Watch "JTextField/Reading Content" New topic
Author

JTextField/Reading Content

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
I don't understand why this snippet don't seem to do what I want it to do:

JTextField textField = new JTextField(50);

//more code


//This is called by a button event:

if(textField.getText() != null)
{
//do something is there is 'some' text (ecen just a character
//in the textField

}
else
{
//do something is there is 'no' text in the textField
}

The problem I have is that even if I don't type anything in the textField it still goes inside the if statement. It seems, when I debug it, the textField.getText() still returns "".

Basically, what I want is to check if the textField has text, then do something, else do something else.


amerzil co-ed student<br />"Praise be the Code"
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
i guess its because an empty string ("") is diferent from a null string


java amateur
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
JTextArea.getText() never returns null. You have to check for the empty string, as you have figured out.


James Carman, President<br />Carman Consulting, Inc.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
OK.
I changed my code to:

if(textField.getText() != "")
{

}
else
{
}

It still goes inside the if statement even if I don't type anything in the textField.

I'm sure I'm missing something. Being a beginner, I'm at a loss.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Thanks.
It works.
I've tried that before but did not work because I got confused, thinking that the equals() takes an object as an argument. I didn't know you could just put in the string representation directly as its argument.

I would appreciate if you could explain a bit on this. I'm the kind of poerson who likes to know the rationale behind it rather than just accepting it without understanding why. It would certainly help a great deal in knowledge retention. Don't you think so?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Sure thing! The java.lang.String class is just that, a class. So, every instance of String is an Object, even Literals. You could also have done...



That would have worked fine, too. I know, it's a bit weird looking, but it does have one nice side-effect. You won't get null pointers when you use this technique, because you know the "" literal reference is always non-null.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Thanks.

The if-else statement I have is contained in an inner class.

Say, for example, this inner class is contained in an outer class called OutClass.

If I don't want to use an inner class, but instead put this in another outer class.

The code uses inner class.




//import all that's required here


public class OutClass
{
JTextField textField = new JTextfield(40);
JTextArea textArea = new JTextArea(10, 60);

public static void main(String[] args)
{
OutClass oc = new OutClass();
oc.gui();
}

public void gui()
{
//assume i have the code to create the frame, and a JButton,
// called, transferButton, a JTextField, and a JTextArea

buttonTransfer.addActionListener(new buttonTransferListener));
}


//inner class
class buttonTransferListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(!textField.getText().equals(""))
{
textArea.append(textField.getText() + "\n");
}
}
}//end of inner class
}//end of class OutClass


If I don't want to use an inner class, I create another class, saved as another file.


import java.awt.event.*;

//extends to OutClass so it can see the textField and TextArea objects
class Sample extends OutClass implements ActionListener
{

//transfer the if statement here
public void actionPerformed(ActionEvent e)
{
if(!textField.getText().equals(""))
{
textArea.append(textField.getText() + "\n");
}
}

}


When I debug it, it can jump right into the if statement of this new separate outer class. However, it seems it can't copy the text from the fieldText to the textArea.


Help.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
I don't think 'extend' will work. But I don't know the way to make sure the new class (Sample) can still have access to the same textField and TextArea objects. Static?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Are you adding an instance of the new outer class (OutClass) as an action listener on your text field?
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
in the class OutClass, instead of having the line, which I commented out:

buttonTransfer.addActionListener(new buttonTransferListener));


I replace it with:
buttonTransferListener bt = new buttonTransferListener();
buttonTransfer.addActionListener(bt);



So, when debugging, I can get it to go to this new class, called
public class buttonTransferListener extends OutClass implements ActionListener.

The problem is, regardless, if the textField has some string or strings or nothing at all, it just wouldn't process the if statement. It just immediately jumpt to the closing brace of the new class that i created.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The extend won't work for you as you expected. When you extend it, the object you instantiate and use as a listener has its own textField and textArea variables and that's what it's calling the methods on. Does that make sense? Try this...



Then, add an instance of MyActionListener as a listener on your text field. But, when you construct it, you'll have to pass in your textArea and textField references to the constructor. Note, you could also find the textField by asking for the source of the ActionEvent (and casting it to JTextField). Anyway, see if that helps.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
You're a genius!
And an unselfish person, too.



Thank you so much.
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Is it the Java convention (I have yet to familiarize myself of this)to still declare it 'final' even if it's already 'private'?
Or is it just something to show to beginners like myself to impress on our minds that private is implicitly final, as it won't affect the outcome anyway?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The private doesn't mean or imply final. In fact, I have many private variables that change all the time...



The reason I made it final in that example was to force myself to initialize it in the constructor. If I know it doesn't need to change, I make it final. I'm not saying that's the norm. I probably use the final keyword more than the next guy.
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341


Or you can use

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTextField/Reading Content
 
Similar Threads
Blank space question
String to integer??
I would like save the user inputs form JTextField ab,l m,n,o etc. how can I do this please help
FocusEvent help......
Regarding JTextField