3

something seems to be going wrong with the block of code that tries to set the String variable, as no matter what I do when I run the program, the Dialog Box always shows otto. Does anyone know what I'm doing wrong here?

Thanks, Ravin

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class SmallTingz extends JFrame {
    private JLabel item1;
    private JTextField tf;
    private JTextField tf2;
    private JTextField tf3;
    private JPasswordField pf;

    public SmallTingz() {
        super("The Title");
        setLayout(new FlowLayout());
        JTextField tf = new JTextField("Cool Beans");
        JTextField tf2 = new JTextField("UnCool Beans");
        JTextField tf3 = new JTextField("Hot Beans");
        JPasswordField pf = new JPasswordField("password");

        add(tf);
        add(tf2);
        add(tf3);
        add(pf);

        thehandler handler = new thehandler();
        tf.addActionListener(handler);
        tf2.addActionListener(handler);
        tf3.addActionListener(handler);
        pf.addActionListener(handler);
    }

    private class thehandler implements ActionListener {

        public void actionPerformed(ActionEvent event) {
            String string;          
            if (event.getSource() == tf)
                string=String.format("field1: %s", event.getActionCommand());
            else if (event.getSource() == tf2)
                string=String.format("field2: %s", event.getActionCommand());
            else if (event.getSource() == tf3)
                string=String.format("field3: %s", event.getActionCommand());
            else if (event.getSource() == pf)
                string=String.format("passfield: %s", event.getActionCommand());
            else
                string="otto";

            JOptionPane.showMessageDialog(null, string);        
        }
    }
}
3
  • you should use ".equals()" instead of "==" in if statement. for example if(event.getSource().equals(tf)) "tf" and "event.getSource()" must be the same type Commented Jul 21, 2011 at 14:45
  • Try running this through a debugger, breaking on actionPerformed. Then check the runtime value of event. Commented Jul 21, 2011 at 14:46
  • @fsonmezay getSource() returns a reference to the actual component that emitted the source. Thus, in this situation, it is actually correct to test on reference equality. Otherwise, if you have two sublcassed JButtons that overrode equals to be based on their labels, you couldn't tell the difference between button presses from two different buttons with equivalent labels. Commented Jul 21, 2011 at 14:49

2 Answers 2

12

In your SmallTingz() constructor, remove all variable declarations. Your declarations is hiding the member variables.

Change

JTextField tf = new JTextField("Cool Beans");
JTextField tf2 = new JTextField("UnCool Beans");
JTextField tf3 = new JTextField("Hot Beans");
JPasswordField pf = new JPasswordField("password");

to

tf = new JTextField("Cool Beans");
tf2 = new JTextField("UnCool Beans");
tf3 = new JTextField("Hot Beans");
pf = new JPasswordField("password");
Sign up to request clarification or add additional context in comments.

1 Comment

If he had made all those textfields final then this problem would have been found at compile time. :)
1

Two things.

First, I always use brackets everywhere I can with else if

if(...){
//do stuff
}
else
{
  if(...)
  {
  // other stuff.
  }
}

It makes it much easier to read. Readability is one of the most important aspect of writing code.

Second, it is a very good opportunity to learn how to use the debugger. I suspect that you are using eclipse. Click in the margin of if(event.getSource()==tf) A small red dot should appear, this is a breakpoint. Now run your code in debug mode.

The execution should stop at that line. Hover your mouse cursor over the "event" variable. You should see a popup that will show you a description of that object.

This should help you understanding why GetSource() does not return a pointer to your JTextField(s)

Maybe you'll see that tf are null like user802421 said.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.