4 Replies - 263 Views - Last Post: 25 April 2012 - 07:40 PM Rate Topic: -----

#1 swagen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 12

[GUI] How to compare the text to the String

Posted 24 April 2012 - 08:42 PM

private JLabel lb = new JLabel("Enter text :");
private JTextField jtf1 = new JTextField(10);
private JButton jb = new JButton("ENTER");
private JLabel lb2 = new JLabel(" ");

public void init() {
add(lb);
add(jtf1);
add(jb);
add(lb2);
jb.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
String authorized = "Lawrence";
String name = jtf1.getText();
if (e.getSource() == jb) {
if (authorized == name)
lb2.setText("Access Granted");
else
lb2.setText("Wrong Name);
}

Example: i have to key in the name called "Lawrence" once i key in and i press the "Enter" button , it will show the text("Access Granted") in a Label field. If key in other name it will show ("Wrong Name")in a Label field.

I guess there is something wrong with my code for the comparison string..

Please give some guidance ..thanks ..

Is This A Good Question/Topic? 0
  • +

Replies To: [GUI] How to compare the text to the String

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,463
  • Joined: 02-June 10

Re: [GUI] How to compare the text to the String

Posted 24 April 2012 - 08:48 PM

Please fix your post to include code tags, just like it says in the box where you pasted the code
:code:

Attached Image

Use the .equals() method and not ==
Was This Post Helpful? 1
  • +
  • -

#3 swagen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 12

Re: [GUI] How to compare the text to the String

Posted 24 April 2012 - 09:15 PM

Thanks for reply.. i cant find any edit post for my main post...

OK, i have modified my code , but it still don't give the result ..

    public void actionPerformed(ActionEvent e) {
    String authorized = "Lawrence";
    String name = jtf1.getText();
    if (e.getSource() == jb) {
        if (name.equals(authorized))
            lb2.setText("Wrong name");
        else
            lb2.setText("Access Granted");
    }

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8018
  • View blog
  • Posts: 31,126
  • Joined: 06-March 08

Re: [GUI] How to compare the text to the String

Posted 25 April 2012 - 03:56 AM

should work. May be you would like:

if (name.equalsIgnoreCase(authorized))
Was This Post Helpful? 1
  • +
  • -

#5 swagen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-April 12

Re: [GUI] How to compare the text to the String

Posted 25 April 2012 - 07:40 PM

Thanks... it's working :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1