Joptionpane cancel button

Asked

Viewed 238 times

0

In the following code, it asks to be filled in the name, but if you do not enter anything, and I click cancel it returns a message (The name you entered was Null).

My question is how do I stop when clicking cancel it directly close the application or appear another message, do not appear the "null".

Follow the code part:

mensagem.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String nome;
        nome = JOptionPane.showInputDialog("Qual seu nome?"); 
        JOptionPane.showMessageDialog(janela, "O nome que informou foi:\n"+nome);
    }
});

1 answer

0


mensagem.addActionListener( new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String nome;
        nome = JOptionPane.showInputDialog("Qual seu nome?"); 
        if(nome != null) JOptionPane.showMessageDialog(janela, "O nome que informou foi:\n"+nome);
        else {
            //faz outra coisa
        }
   }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.