0

I have an assignment where I have to set get user input and then display the input as well as an image associated with the input. I'm stuck with my code and don't know where the problem. I think the main issue I'm having is getting tf1.getText() == st1.getInfo() value.

I have also checked to make sure the images are in the correct folder and labels correctly.

Sorry for having to post so much code. Thanks for any help.

Code:

MyJpanel1

public class MyJPanel1 extends JPanel implements ChangeListener, ActionListener
{
    JTextField tf1,tf2;
    JSlider js1;
    JLabel lab6;
    JTextField message;
    JButton ok;
    myJPanel2  p2;
    int count2;

   public MyJPanel1(MyJPanel2 informedp2)
    {
    p2 = informedp2;
    setLayout(new GridLayout(7,1));
    lab6 = new JLabel("Enter Student Name");
    add(lab6,"North");

    tf1 = new JTextField(10);

    add(tf1);

    tf2 = new JTextField(15);

    add(tf2);


     message = new JTextField("",50);

js1 = new JSlider(JSlider.HORIZONTAL,0,50,20);
js1.setBorder(BorderFactory.createTitledBorder("Use Slider to Enter Age"));
    js1.setMajorTickSpacing(1);
    js1.setPaintTicks(true);
    add(js1);
add(message);

    js1.addChangeListener(this); 

     ok = new JButton("Ok");
    ok.addActionListener(this);
    add(ok);



    }
    public void stateChanged(ChangeEvent e) 
{
    JSlider obj = (JSlider)e.getSource();
    int count = obj.getValue();
    if(obj == js1)
    {
        message.setText("Age =" + count);
    }
        count2 = count;
    }
     public void actionPerformed(ActionEvent event) 
    {
     Object obj = event.getSource();
     String result = p2.st1.getInfo(); // value stored should be "Fred" 

     if (obj == ok)
     {
            p2.j2.setText("Student Name = " + tf1.getText()+" " + tf2.getText()+ " "+ "Age = " + count2);


     if (result ==tf1.getText()) // tf1.getText() value should be "Fred" which should then display the image. 
     {

         p2.j3.setIcon(p2.imageFred);
     }
     }
    }

}

MyJPanel2

public class myJPanel2 extends JPanel
{

student st1 = new student("Fred"); // "Fred" value should be the st1.getInfo() value 
    JButton j2 = new JButton("the user clicks on the button in the UPPER panel" );
    JButton j3 = new JButton("Pic");
     ImageIcon imageFred = new ImageIcon("images/fred.gif");
public myJPanel2()
{
    super();

    setBackground(Color.pink);
    setLayout(new GridLayout(3,1));
            add(j2);
            add(j3);
    }
}

student:

 public class student 
{
    String firstName;
    String lastName;
    int age;

    public student(String a)// String b, int x)
    {   
        super();
        firstName = a;
        //lastName = b;
        //age = x;

    }

    String getInfo()
    {
        return firstName; // This value should return "Fred" since st1 = new student("Fred")correct? 
    }



    String whatsUp()
    {
        double r = Math.random();
        int myNumber = (int) (r * 3f); //comment: a random number between 0 and 2
        String answer = "I don't know";
        if(myNumber == 0) answer = "searching the web";
        if(myNumber == 1) answer = "doing Java";
        if(myNumber == 2) answer = "Listening to endless lecture";
        return answer;
    }

}

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user1566796
  • 35
  • 2
  • 9
  • 1
    Please describe your problem more completely. We see a lot of code but not a lot of explanation as to what is wrong with the code. Assume that we know nothing about what you're trying to do, what your current code is doing, what's working and what's not working. – Hovercraft Full Of Eels Oct 12 '13 at 20:19
  • My problem is getting the name entered in tf1 jtextfield to match a st1.getInfo() value when the "Ok" button is clicked that way I can change Jbutton j3 in myjpanel2 to an image value. I got j2 in myjpanel2 to change just fine when the "ok" button is clicked, but I can't get the j3 button to change to an image and display and I have to use the student class to store the name value for st1. – user1566796 Oct 12 '13 at 20:30

2 Answers2

1

You can't compare strings using ==. This will not compare the content of both strings but their "addresses" (ie, you are comparing if they are the same object). You can find a more detailed explanation here.

To compare them use the String.equals method:

  if ( tf1.getText().equals(st1.getInfo()) ) {
   // (...) 
  }
Community
  • 1
  • 1
Salem
  • 12,808
  • 4
  • 34
  • 54
0

This is wrong:

if (result == tf1.getText()) {
  p2.j3.setIcon(p2.imageFred);
}

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373