Hi Zeynel,
The problem you're having is due to comparing String values using the
"==" operator instead of the "equals()" method.
If your code read ...
if ( !jTextfield.getText().equals("") ) {
// ...
}
... it would produce the result you're expecting.
Hope this helped.
Have a good day,
Geoff.
PS: Here's a code example to illustrate the point:
import javax.swing.*;
import java.awt.event.*;
public class Main {
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TestFrame();
}
});
}
}
class TestFrame extends JFrame implements ActionListener {
JTextField field = null;
JButton button = null;
public TestFrame() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
field = new JTextField(15);
button = new JButton("Test Text Field");
button.addActionListener(this);
JPanel content = new JPanel();
content.add(field);
content.add(button);
this.getContentPane().add(content);
this.pack();
this.setSize(250, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button) {
if (field.getText() != "") {
System.out.println("Using \"==\", the value in field
is not \"\"");
} else {
System.out.println("Using \"==\", the value in field
is \"\"");
}
if (!field.getText().equals("")) {
System.out.println("Using \".equals()\", the value in
field is not \"\"");
} else {
System.out.println("Using \".equals()\", the value in
field is \"\"");
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---