0

I'm creating a user name confirm box, and I need to confirm if the text typed into the text field matches with the already defined type in a variable name.

I don't know how to get it. I had been trying by implementing the following code, which in the action performed once the button is pressed it will verify if the textfield matches with the variable name.

code is as follows.

public class Action extends JFrame implements ActionListener
{
    JLabel l;
    JTextField t;
    JButton b;
    final String name = "harry";


    public Action() 
    {
        l = new JLabel("Name");
        l.setBounds(10, 10, 100, 33);

        t = new JTextField();
        t.setBounds(60, 10, 100, 30);

        b = new JButton("send text");
        b.setBounds(80, 120, 100, 40);



        add(l);
        add(t);
        add(b);


        setSize(300, 300);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }



    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if (t.getText() == name)
        {
            JOptionPane.showMessageDialog(this, "you mach");
        }
        else
        {
            JOptionPane.showMessageDialog(this, "you dont");
        }

    }

    public static void main(String[] args) 
    {
         new Action();

    }

}
Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27
  • 2
    `==` is not how you compare `String`s in Java, instead you should be using `t.getText().equals(name)` instead – MadProgrammer May 03 '16 at 01:22
  • @MadProgrammer but stil no prints nothing –  May 03 '16 at 01:25
  • 1
    I'm voting to close this question as off-topic because the answer can readily be found by reading an appropriate tutorial like [How to Write an Action Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and [How to Use Buttons, Check Boxes, and Radio Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – MadProgrammer May 03 '16 at 01:27
  • I don't think your actionlistener is ever added to the button and is thus never gets called. You're better off reading the Swing tutorials, though. – pvg May 03 '16 at 01:32
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 03 '16 at 02:21

1 Answers1

2

In your Action() constructor, you have to add an actionlistener to the frame:

addActionListener(this);

in order for it to work;

Also, you compare strings by .equals() because strings are objects. Stack does not store the string's value, the heap does. To compare the string's value, you have to call t.getText().equals(name)

Change that in your actionPerformed() class and you are good to go!

sguan
  • 1,039
  • 10
  • 26