Ejemplo de un JDialog

Este codigo lo genere con el IDE de netbeans 6.5
//Así mandas a llamar el JDialog new PruebaDialog().setVisible(true);

package pruebasblogger;

import javax.swing.JOptionPane;

/**
*
* @author Gamma
*/
public class PruebaDialog extends javax.swing.JDialog {

   public PruebaDialog() {
       super();
       initComponents();
   }

   @SuppressWarnings("unchecked")
   // 
   private void initComponents() {

       jPanel1 = new javax.swing.JPanel();
       jLabel1 = new javax.swing.JLabel();
       tsaludo = new javax.swing.JTextField();
       jButton1 = new javax.swing.JButton();

       setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
       setAlwaysOnTop(true);

       jPanel1.setBackground(new java.awt.Color(255, 255, 255));

       jLabel1.setText("Saludo:");

       jButton1.setText("Aceptar");
       jButton1.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               jButton1ActionPerformed(evt);
           }
       });

       javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
       jPanel1.setLayout(jPanel1Layout);
       jPanel1Layout.setHorizontalGroup(
           jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel1Layout.createSequentialGroup()
               .addGap(53, 53, 53)
               .addComponent(jLabel1)
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
               .addComponent(tsaludo, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addContainerGap(153, Short.MAX_VALUE))
           .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
               .addContainerGap(185, Short.MAX_VALUE)
               .addComponent(jButton1)
               .addGap(144, 144, 144))
       );
       jPanel1Layout.setVerticalGroup(
           jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(jPanel1Layout.createSequentialGroup()
               .addGap(29, 29, 29)
               .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(jLabel1)
                   .addComponent(tsaludo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
               .addGap(26, 26, 26)
               .addComponent(jButton1)
               .addContainerGap(20, Short.MAX_VALUE))
       );

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
       getContentPane().setLayout(layout);
       layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
       );
       layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
       );

       pack();
   }// 

   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
       // TODO add your handling code here
       JOptionPane.showMessageDialog(this, tsaludo.getText());
   }

   /**
   * @param args the command line arguments
   */
   public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               PruebaDialog dialog = new PruebaDialog();
               dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                   public void windowClosing(java.awt.event.WindowEvent e) {
                       System.exit(0);
                   }
               });
               dialog.setVisible(true);
           }
       });
   }

   // Variables declaration - do not modify
   private javax.swing.JButton jButton1;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JTextField tsaludo;
   // End of variables declaration

}



7 Comentarios

  1. amigo esta bien tu ejemplo pero porque no importar las clases antes del public class?? te ahorrarias mas codigo.

    ResponderBorrar
  2. Que tal Anonimo??

    Este código se genero con Netbeans automáticamente por eso es que las declaraciones aparacen con la ruta completa de la librería al final del fuente, no son importaciones, es imposible poner un import al final del código los imports siempre van después de package en caso de que exista y en este ejemplo solo hay uno que es:

    import javax.swing.JOptionPane;

    Estos:

    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField tsaludo;

    Son declaraciones de objetos que tambien podrían ser así:

    JButton jButton1;
    JLabel jLabel1;
    JPanel jPanel1;
    JTextField tsaludo;

    Pero habría que importas las librerías correspondientes:

    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField tsaludo;

    De tal manera que asi como lo muestra el ejemplo esta nos ahorramos las importaciones. Espero haberte ayudado en algo.

    Saludooss!!

    ResponderBorrar
  3. o.o muii biien u ejemplo ...

    ResponderBorrar
  4. por que no pones solamente el ejemplo de jdialog

    ResponderBorrar
    Respuestas
    1. Regularmente tratamos de poner ejemplos funcionales por eso no solo ponemos el codigo del jdialog pero esta es la linea que hace que abra un JDialog

      JOptionPane.showMessageDialog(this, tsaludo.getText());

      si es que solo eso es lo que quieres ver

      Borrar
  5. Es un ejemplo sencillo y practico, sin embargo hay mucho código auto generado que para muchos seria código Basura ya que lo mismo se puede hacer con menos lineas de código......yo también he utilizado netbeans para hacer interfaces gráficas pero siempre ha sido un problema el código auto generado, por eso prefiero mil veces Eclipse, claro que todo depende de que tan bien nos sentimos con el IDE.
    Te invito a visitar mi blog donde tengo un ejemplo con JDialog, seria bueno contar con tu opinion ;)
    http://codejavu.blogspot.com/2013/08/ejemplo-ventanas-en-java.html

    pdta: Muy buen blog ;)

    ResponderBorrar
  6. Hola!

    Antes que otra cosa gracias por darte una vuelta pora acá. Claro sabemos que una de las desventajas de usar el IDE gráfico de Netbeans para swing es el código generado causa mucho ruido. Sin embargo para cosas sencillas como estas es muy práctivo y cómodo y esa es la intención del post solo un ejemplo de un JDialog.

    Seguro visitaremos tu blog y nos seguimos por alguna red social.

    ResponderBorrar

Publicar un comentario

Artículo Anterior Artículo Siguiente