Joptionpane, for example?

Asked

Viewed 30,147 times

6

Well, Joptionpane needs all these complements:

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

Could you give an example of all these complements filled in correctly? Because I don’t know what it is parentComponent, I don’t know how to use icon.

2 answers

7

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 as such:

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

The Parent Component is only to guide the Dialog to which window it belongs, in this way it will position itself in relation to it. We usually use NULL to get it 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 'YES', 'No' and 'Cancel' buttons'

Joptionpane.PLAIN_MESSAGE is the type of message you will display. This one is a type of message with a custom icon (by icon parameter). There are other:

JOptionPane.PLAIN_MESSAGE
JOptionPane.ERROR_MESSAGE
JOptionPane.INFORMATION_MESSAGE
JOptionPane.WARNING_MESSAGE
JOptionPane.QUESTION_MESSAGE
JOptionPane.PLAIN_MESSAGE (sem icone)

6


Pretty 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 above example. This way the Joptionpane interface will use Jframe (or whatever the parent class) to render itself.

Message type:

I chose to show a message informative but you can choose any of the values from that list:

  • ERROR_MESSAGE
  • INFORMATION_MESSAGE
  • WARNING_MESSAGE
  • QUESTION_MESSAGE
  • PLAIN_MESSAGE

I suggest you read the documentation for more details

Similarly you can use Joptionpane to ask for 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 message types:

  • DEFAULT_OPTION
  • YES_NO_OPTION
  • YES_NO_CANCEL_OPTION
  • OK_CANCEL_OPTION

And follow the list of possible answers to this:

  • YES_OPTION
  • NO_OPTION
  • CANCEL_OPTION
  • OK_OPTION
  • CLOSED_OPTION
  • But it can explain separately what would be Parentcomponent?

Browser other questions tagged

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