Showinputdialog com icones e tudo que tem direito :)

3 respostas
M

como faço um dialog completo ?

3 Respostas

Overload

E aí rapaz, tudo certo!?
Bom, esses dias eu estava me perguntando a mesma coisa, e acabei achando (não me lembro em que fórum, mas acho que foi aqui mesmo :smiley: )
um código postado com um programinha que simula todos os tipos de janelas da classe swing.JOptionPane!!
Então eu vou recolocar o código aqui, porque eu não lembro em que fórum eu vi isso :stuck_out_tongue:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class OptionDialogTeste {

   public static void main(String[] args) {

      OptionDialogFrame frame = new OptionDialogFrame();
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      frame.setVisible( true );
      
   }

}

/**
* Um painel com botões de seleção dentro de uma borda com título;
*/

class ButtonPanel extends JPanel{
   
   public ButtonPanel( String titulo, String[] opcoes ){
      
      setBorder( BorderFactory.createTitledBorder( BorderFactory.createEtchedBorder(), titulo ) );
      setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) );
      group = new ButtonGroup();
      
      for( int i = 0; i < opcoes.length; i++ ){
         JRadioButton b = new JRadioButton( opcoes[i] );
         b.setActionCommand( opcoes[i] );
         add( b );
         group.add( b );
         b.setSelected( i == 0 );
         
      }
      
   }
   
   /**
    * Obtem a opção selecionada no momento;
    * @return retorna o rótulo do botão de seleção marcado no momento
    */
   
   public String getSelection(){
      return group.getSelection().getActionCommand();
   }
   
   private ButtonGroup group;
   
}

/**
* Um Frame que contém configurações para selecionar várias caixas de diálogo de opção;
*/

class OptionDialogFrame extends JFrame{
   
   public OptionDialogFrame(){
      
      setTitle("OptionDialogTeste");
      setSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
      
      JPanel gridPanel = new JPanel();
      gridPanel.setLayout( new GridLayout( 2, 3 ) );
      
      typePanel = new ButtonPanel("Type", new String[] { "Message", "Confirm", "Option", "Input"} );
      messageTypePanel = new ButtonPanel("Message Type", new String[] { "ERROR_MESSAGE", "INFORMATION_MESSAGE", "WARNING_MESSAGE", "QUESTION_MESSAGE", "PLAIN_MESSAGE" } );
      messagePanel = new ButtonPanel("Message", new String[] { "String","Icon","Component","Other","Object[]" } );
      optionTypePanel = new ButtonPanel("Confirm", new String[] { "DEFAULT_OPTION", "YES_NO_OPTION", "YES_NO_CANCEL_OPTION", "OK_CANCEL_OPTION" } );
      optionsPanel = new ButtonPanel("Option", new String[]{ "String[]", "Icon[]", "Object[]" } );
      inputPanel = new ButtonPanel("Input", new String[] { "Text field", "Combo box" } );
      
      gridPanel.add( typePanel );
      gridPanel.add( messageTypePanel );
      gridPanel.add( messagePanel );
      gridPanel.add( optionTypePanel );
      gridPanel.add( optionsPanel );
      gridPanel.add( inputPanel );
      
      JPanel showPanel = new JPanel();
      JButton showButton = new JButton("Show");
      
      showButton.addActionListener( new ShowAction() );
         
      showPanel.add( showButton );
      
      add( gridPanel, BorderLayout.CENTER );
      add( showPanel, BorderLayout.SOUTH );
      
   }
   
   public Object getMessage(){
      
      String s = messagePanel.getSelection();
      
      if( s.equals("String"))
         return messageString;
      else if( s.equals("Icon"))
         return messageIcon;
      else if( s.equals("Component"))
         return messageComponent;
      else if( s.equals("Object[]"))
         return new Object[]{ messageString, messageIcon, messageComponent, messageObject };
      else if( s.equals("Other"))
         return messageObject;
      else return null;      
      
   }
   
   public Object[] getOptions(){
      
      String s = optionsPanel.getSelection();
      if( s.equals("String[]"))
         return new String[] { "Yellow","Blue","Red" };
      else if( s.equals("Icon[]"))
         return new Icon[] {
            new ImageIcon("yellow-ball.gif"),
            new ImageIcon("blue-ball.gif"),
            new ImageIcon("red-ball.gif")
      };
      else if ( s.equals( "Object[]" ) )
         return new Object[]{
            messageString, messageIcon, messageComponent, messageObject
      };
      else
         return null;
      
   }
   
   public int getType( ButtonPanel panel ){
      
      String s = panel.getSelection();
      
      try{
         return JOptionPane.class.getField(s).getInt(null);
      }
      catch( Exception e )
      {
         return -1;
      }
      
   }
   
   private class ShowAction implements ActionListener{
      
      public void actionPerformed( ActionEvent event){
         
         if( typePanel.getSelection().equals("Confirm") )
            JOptionPane.showConfirmDialog( OptionDialogFrame.this, getMessage(), "Titulo", getType( optionTypePanel), getType( messageTypePanel ) );
         else if( typePanel.getSelection().equals("Input") )
         {
            if( inputPanel.getSelection().equals("Text field") )
               JOptionPane.showInputDialog( OptionDialogFrame.this, getMessage(), "Titulo", getType( messageTypePanel ) );
            else
               JOptionPane.showInputDialog( OptionDialogFrame.this, getMessage(), "Titulo", getType( messageTypePanel), null, new String[] { "Yelloy", "Blue", "Red" }, "Blue" );
         }
         else if( typePanel.getSelection().equals("Message") )
            JOptionPane.showMessageDialog( OptionDialogFrame.this, getMessage(), "Titulo", getType( messageTypePanel ) );
         else if( typePanel.getSelection().equals("Option") )
            JOptionPane.showOptionDialog( OptionDialogFrame.this, getMessage(), "Titulo", getType( optionTypePanel), getType( messageTypePanel), null, getOptions(), getOptions()[0] );
         
      }
   }
   
   private ButtonPanel typePanel;
   private ButtonPanel messageTypePanel;
   private ButtonPanel messagePanel;
   private ButtonPanel optionTypePanel;
   private ButtonPanel optionsPanel;
   private ButtonPanel inputPanel;
   
   private String messageString = "Message";
   private Icon messageIcon = new ImageIcon("blue-ball.gif");
   private Object messageObject = new Date();
   private Component messageComponent = new SamplePanel();
      
   public final static int DEFAULT_WIDTH = 600;
   public final static int DEFAULT_HEIGHT = 400;
   
}

class SamplePanel extends JPanel{
   
   public  void paintComponent( Graphics g ){
      
      super.paintComponent(g);
      Graphics2D g2 = ( Graphics2D ) g;
      
      Rectangle2D rect = new Rectangle2D.Double( 0, 0, getWidth() - 1, getHeight() - 1 );
      g2.setPaint( Color.YELLOW );
      g2.fill( rect );
      g2.setPaint( Color.BLUE );
      g2.draw( rect );
      
   }
   
   public Dimension getMininumSize(){
      return new Dimension( 10, 10 );
   }
   
}

Ele é bom pra você estudar sobre todos os tipos de janelas!!
Espero ter ajudado amigo.
Abraço.

T

Que tal assim?

import javax.swing.JOptionPane;

public class InputDialog {
	
	public static void main(String[] args) {
		String nomeErrado, nomeCerto;
		nomeErrado = JOptionPane.showInputDialog (null,
				"Entre o nome errado pra teste", "Prompt de nomes errados",
				JOptionPane.ERROR_MESSAGE);
		
		nomeCerto = JOptionPane.showInputDialog (null,
				"Entre o nome certo pra teste", "Prompt de nomes errados",
				JOptionPane.QUESTION_MESSAGE);
		
		JOptionPane.showMessageDialog(null,
		 "O nome errado digitado foi " + nomeErrado, "Nome digitado",
		 		JOptionPane.WARNING_MESSAGE);
		
		JOptionPane.showMessageDialog(null,
		"O nome certo digitado foi " + nomeCerto, "Nome digitado",
				 		JOptionPane.PLAIN_MESSAGE);
	}
}
M

e como eu faço para colocar um icone no ShowInpoutDialog .

Criado 4 de junho de 2006
Ultima resposta 5 de jun. de 2006
Respostas 3
Participantes 3