2

I wrote this code and I wonder if you guys can help me figure out how to make my application close whenever i enter the name "Bob/BOB/bob" in text1.

public class Event extends JFrame {
    //create items

    private JTextField text1;
    private JTextField text2;
    private JTextField text3;
    private JPasswordField pass1;


    public Event(){
        super("The title");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        text1 = new JTextField(10);
        add(text1); 
        text2 = new JTextField("enter text here");
        add(text2);
        text3 = new JTextField("uneditbale", 15);
        text3.setEditable(false);
        add(text3); 
        pass1 = new JPasswordField("enter your password", 10);
        add(pass1);

        //create object
        thehandler handler = new thehandler();
        text1.addActionListener(handler);
        text2.addActionListener(handler);
        text3.addActionListener(handler);
        pass1.addActionListener(handler);

        //constructor Event ends
    }

    private class thehandler implements ActionListener{

        public void actionPerformed(ActionEvent event){

            String string = "";

            if(event.getSource()==text1)
                string=String.format("field 1: %s",event.getActionCommand());
            else if(event.getSource()==text2)
                string=String.format("field 2: %s",event.getActionCommand());
            else if(event.getSource()==text3)
                string=String.format("field 3: %s",event.getActionCommand());
            else if(event.getSource()==pass1)
                string=String.format("Password is : %s",event.getActionCommand());

            JOptionPane.showMessageDialog(null, string);
        }

    }
    public static void main(String[] args) {
        Event ev = new Event();
        ev.setSize(350, 100);
        ev.setVisible(true);

    }
}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
LawZ
  • 21
  • 1

4 Answers4

1

Use this code. I have made a textfield and when it get's text "Bob/BOB/bob" it exit Jframe.

public class Example extends JFrame {

Example() {

    JTextField textfield1 = new JTextField(10); 
    textfield1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {

            if(textfield1.getText().equals("Bob/BOB/bob"))
                    System.exit(0);

        }
    });

    add(textfield1);
 }
}
Programmer
  • 445
  • 4
  • 12
0

You can use System.exit(0), if you want to terminate the entire process. The dispose() method can be used to close the window only.

(To be honest, I got seriously confused by the JFrame called Event, unfortunately it's an awful name.)

gorhawk
  • 158
  • 1
  • 10
  • 1
    `The dispose() method can be used to close the window only.` and if the frame was the only open frame in the application the JVM will shut down. – camickr Jun 07 '15 at 17:27
0

See https://stackoverflow.com/a/7513639/4172242 for an explanation of the process on how to perform a JTextField and String equality check.

To terminate the application, System.exit(0) should work for your purposes.

Community
  • 1
  • 1
mcw
  • 50
  • 5
0

I understand you question differently...

(...) enter the name "Bob/BOB/bob"

If you want to make you code case insensitive, try to use string.toLowerCase() or string.toUpperCase()

For example:

if("bob".equals(inputString.toLowerCase()) {
    // app close logic
    System.exit(0);
}
radekEm
  • 4,617
  • 6
  • 32
  • 45