JOptionPane cancel button

0

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

My question is how do I to click on cancel it directly close the application or appear another message, not appear "null".

Here's 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);
    }
});
    
asked by anonymous 27.09.2017 / 18:11

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
        }
   }
});
    
27.09.2017 / 18:44