JOptionPane, example?

Well, JOptionPane needs all these add-ons:

  JOptionPane.showOptionDialog(parentComponent, message, title,
              optionType, messageType, icon, options, initialValue);

Could you give an example of all these correctly filled add-ons? Because I don't know what parentComponent is, I don't know how to use icon.

Author: Mansueli, 2014-08-18

2 answers

Basic example:

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CriaDialogo
{
  public static void main(String[] args)
  {
   // Algo que deseja mostrar (aviso, mensagem de erro)
    String erro = "Erro 404: não foi possível encontrar o batman";

    // Cria um JFrame
    JFrame frame = new JFrame("JOptionPane exemplo");

    // Cria o JOptionPane por showMessageDialog
    JOptionPane.showMessageDialog(frame,
        "Houve um problema ao procurar o batman:\n\n '" + erro + "'.", //mensagem
        "Erro 404", // titulo da janela 
        JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
  }
}

Explanation:

ParentComponent :

JOptionPane needs a component from which it will be derived, the most common is to use (and create a JFrame for it) which is what occurs in the example above. This way the JOptionPane interface will use the jframe (or whatever the parent class is) to render itself.

Message type :

I chose to show an informative message but you can choose any of the values in this list:

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

I suggest you read the documentation to see more details

Similarly, you can use JOptionPane to request user data:

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CriaDialogo
{
  public static void main(String[] args)
  {
    // Cria um JFrame
    JFrame frame = new JFrame("JOptionPane exemplo");

    // Cria o JOptionPane por showMessageDialog
    int resposta = JOptionPane.showConfirmDialog(frame,"escolha um", "escolha dois", JOptionPane.YES_NO_OPTION);
    //verfica se a resposta é verdadeira
    if (resposta == JOptionPane.YES_OPTION) {
        JOptionPane.showMessageDialog(null, "Olá");
      }
      else {
         JOptionPane.showMessageDialog(null, "Adeus");
         System.exit(0);
      }
    System.exit(0);
  }
}

Lists of types of message:

  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

And follows the list of possible answers to this:

  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION
 4
Author: Mansueli, 2014-08-18 17:59:47
JOptionPane.showOptionDialog(
                           null
                         , "Pergunta?"        // Mensagem
                         , "Titulo"               // Titulo
                         , JOptionPane.YES_NO_OPTION  
                         , JOptionPane.PLAIN_MESSAGE                               
                         , null // Icone. Você pode usar uma imagem se quiser, basta carrega-la e passar como referência
                         , opcoes // Array de strings com os valores de cada botão. Veja o exemplo abaixo **
                         , "Botao 3"    // Label do botão Default
                       );

Opcoes can be declared like this:

String[] choices = {"Botao 1", "Botao 2", "Botao 3", "Botao 4"};

The parent component is just to guide the Dialog to which window it belongs, in this way it will position itself in relation to it. We usually use NULL so that it sits in the center of the Desktop.

To display an image you can use a BufferedImage.

JOptionPane.YES_NO_OPTION : if you pass NULL instead of opcoes Your dialog will have the buttons 'YES', 'No' and 'Cancel'

JOptionPane.PLAIN_MESSAGE is the type of message you will show. This in particular is a message type with a custom icon (by the icon parameter). There are others:

JOptionPane.PLAIN_MESSAGE
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE (sem icone)
 6
Author: Edgar Muniz Berlinck, 2014-08-18 17:59:13