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 ..
[GUI] How to compare the text to the String
Page 1 of 14 Replies - 263 Views - Last Post: 25 April 2012 - 07:40 PM
Replies To: [GUI] How to compare the text to the String
#2
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


Use the .equals() method and not ==

Use the .equals() method and not ==
#3
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 ..
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");
}
#4
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))
if (name.equalsIgnoreCase(authorized))
#5
Re: [GUI] How to compare the text to the String
Posted 25 April 2012 - 07:40 PM
Thanks... it's working
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|