hello everyone i had been doing a program using java and hit this error that i don't know how to solve .. it seem logical to me but it just dun work .. i wrote some codes to show u all what i mean..
public class Strange extends JFrame implements ActionListener { JTextField aTextField; JButton aButton;
public Strange() { setVisible(true); JPanel myPanel = new JPanel(); aTextField = new JTextField(20); aButton = new JButton("enter"); aButton.addActionListener(this); myPanel.add(aTextField); myPanel.add(aButton); pack();
getContentPane().add(myPanel); pack(); } public void actionPerformed(ActionEvent evt) { A a = new A(); if (evt.getSource() == aButton) { String textFieldValue = aTextField.getText();
by textFieldValue.equals( a.getx() ) then you will true in your message box.== operator compares the references of two opererands. textFieldValue always contains reference to a new string object. I think you have read about string pool and you are thinking that since you are using "abc" literal inside class A, == should return true. You are getting false because of getText() method on text field is always returning new reference. To compare the content, as you intend, use equals() method instead of == operator.