JOptionPane, example?

4

Well, JOptionPane needs all these add-ons:

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

Could you give an example of all of these complements filled out correctly? Well, I do not know what parentComponent , I do not know how to use icon.

    
asked by anonymous 18.08.2014 / 19:24

2 answers

4

Very 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 this) which is what occurs in the example above. In this way the JOptionPane interface will use the Jframe (or whatever the parent class) to render.

Message type :

I've chosen to display an informational message but you can choose any of the values from this list:

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

I suggest you read the documentation to see more details

In an analogous way, you can use JOptionPane to request a 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);
  }
}

Message type lists:

  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

And here is the list of possible answers to this:

  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION
18.08.2014 / 19:47
6
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
                       );

options can be declared like this:

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

The parent component is just to guide Dialog which window it belongs to, so it will position itself in relation to it. We usually use NULL so it stays 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 options your dialog will have the 'YES', 'NO' and 'Cancel'

JOptionPane.PLAIN_MESSAGE is the type of message you are going to 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)
    
18.08.2014 / 19:53