How to make a Java Application close

101 Views Asked by At

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);

    }
}
4

There are 4 best solutions below

1
On

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.)

0
On

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.

0
On

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);
 }
}
0
On

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);
}