Reputation: 53
I'm trying to get the 'enlargeListener implements ActionListener' class to take the input from the shapeName JTextField and apply it into a for loop. The specifications being that if what is returned from the shapeName.getText() that is taken from the shapeName JTextfield matches what is specified in the loop (aka == "rectangle" or == "circle") it will implement the enlarge or shrink method.
The problem is I know it returns what is in the JTextField...but even if it meets the qualifications for the loop..the loop still fails and I can't figure out why.
For example..if I typed 'rectangle' into the first text box...it would return to the for loop under the 'enlargeListener implements' class as a string "rectangle". Yet when it is tested against == "rectangle" it still fails. I've combed through many, many documents to try to figure it out...but still I cannot. Any help would be appreciated :)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TestShape extends JFrame
{
Circle c;
Rectangle r;
JTextField shapeName;
JTextField areaText;
String input;
String area;
public TestShape()
{
JPanel jp = new JPanel();
c = new Circle(65);
r = new Rectangle(60,80);
add(r);
add(c);
r.setBounds(10, 0, 300,300);
c.setBounds(250, 0, 300, 300);
getContentPane().add(jp);
jp.setLayout(null);
shapeName = new JTextField(" ");
shapeName.setBounds(150, 200, 335, 22);
jp.add(shapeName);
add(jp);
areaText = new JTextField();
areaText.setBounds(150, 222, 335, 22);
jp.add(areaText);
add(jp);
JButton enlarge = new JButton("Enlarge");
enlarge.setBounds(150, 300, 80, 28);
jp.add(enlarge);
add(jp);
enlarge.addActionListener(new enlargeListener());
JButton shrink = new JButton("Shrink");
shrink.setBounds(235, 300, 80, 28);
jp.add(shrink);
add(jp);
shrink.addActionListener(new shrinkListener());
}
public static void main(String[] args)
{
TestShape test = new TestShape();
test.setVisible(true);
test.setSize(500,500);
test.setLocation(500,100);
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
class enlargeListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent arg0)
{
input = shapeName.getText().toString();
if(input == "rectangle")
{
r.enlarge();
//areaText.setText(r.getArea());
}
else if (input == "circle")
{
c.enlarge();
}
else
{
System.out.print("cannot do");
}
}
}
class shrinkListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(shapeName.getText() == "rectangle")
{
r.shrink();
}
else
{
c.shrink();
}
}
}
}
Upvotes: 2
Views: 150
Reputation: 83517
One problem is with this comparison (and others like it):
input == "rectangle"
This compares two String references and will only evaluate to true if the references point to the exact same String objects. In order to compare that the strings' contents are the same, use the equals()
method:
"rectangle".equals(input)
You will need to fix all of your if
statements to resolve this issue.
Upvotes: 1