Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField/Reading Content

 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i guess its because an empty string ("") is diferent from a null string
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTextArea.getText() never returns null. You have to check for the empty string, as you have figured out.
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you adding an instance of the new outer class (OutClass) as an action listener on your text field?
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're a genius!
And an unselfish person, too.



Thank you so much.
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Or you can use

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic