Featured Posts

Se vienen el Barcamp Loxa 09 Se viene el #barcamploxa09, que se desarrollara en la ciudad de Loja Ecuador, ¿Qué es un BarCamp? Un BarCamp es un evento donde todos los asistentes...

Readmore

Problemas maritales al diseñador de google En este post se muestra un interesante colección de logotipos donde muestra que el diseñador de logos de google, está teniendo problemas maritales [caption...

Readmore

Lady Gaga - Bad Romance Acá el nuevo video de Lady Gaga, esta polémica artista que presenta su video Bad Romance [youtube ACm9yECwSso]

Readmore

Partido de futbol de mujeres violento Acá les dejo un  interesante video, donde se demuestre que la falta del fair play no se aplica unicaménte a los hombres, sino que las mujeres también...

Readmore

CSI Miami bate record guiness La serie CSI Miami, bate un record al filmar la escena mas cara de la historia utilizando la técnica bullet time que consiste en una extremada ralentización...

Readmore

Edisoncor Weblog Rss

Componentes sobre frame Transparente

Posted on : 08-06-2009 | By : edisoncor | In : Netbeans, Swing, java

4

Frame Transparente

Frame Transparente

A veces se necesita que el JFrame de una aplicación java este transparente pero no los controles para eso es necesario poner el siguiente codigo antes de que el JFrame este visible

        setUndecorated(true);
        setResizable(false);
        AWTUtilities.setWindowOpaque(this, false);

Lo que quita los controles de la ventana y a continuación el JFrame quedara transparente pero no sus componentes,

AWTUtilities.setWindowOpaque(this, false); en la versión jdk 7 quedará obsoleto y será remplazado por Window.setBackground(Color) donde al pasar new Color(0,0,0), se logrará el efecto de transparencia

Aqui un ejemplo creado en Netbeans

public class VentanaFlotante extends javax.swing.JFrame {
 
    /** Creates new form VentanaFlotante */
    public VentanaFlotante() {
        setUndecorated(true);
        setResizable(false);
        AWTUtilities.setWindowOpaque(this, false);
        initComponents();
    }
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //
    private void initComponents() {
 
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
 
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 
        jScrollPane1.setViewportView(jTree1);
 
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(275, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        jTabbedPane1.addTab("tab1", jPanel1);
 
        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 449, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 206, Short.MAX_VALUE)
        );
 
        jTabbedPane1.addTab("tab2", jPanel2);
 
        jButton1.setText("cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
 
        jTextField1.setText("jTextField1");
 
        jLabel1.setText("Ventana de prueba");
 
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 454, Short.MAX_VALUE))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
 
        pack();
    }// 
 
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        System.exit(0);
    }
 
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaFlotante().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.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
    // End of variables declaration
 
}

Comments (4)

No puedes incluir la carpeta del ejemplo con todo? Es que prove el codigo pero me da un error en “AWTUtilities.setWindowOpaque(this, false);”. NO es que se ocupa alguna libreria?

Hola, es la primera vez que paso por aca, no conocía tu blog, y tengo una pregunta.
He visto que has colocado el AWTUtilities en varios post, pero tengo conocimiento que al compilar, por ejemplo con netbeans, advierte que es una clase propietaria de Sun y que podría no ser incluida en futuros releases… que piensas de eso?… si es bueno hacer esto en aplicaciones empresariales que lo requieran?

Hola Danilo AWTUtilities.setWindowOpaque(this, false); en la versión jdk 7 quedará obsoleto y será remplazado por Window.setBackground(Color) donde al pasar new Color(0,0,0), se logrará el efecto de transparencia, lo que puedes intentar es

 try {
   Class clazz =  
           Class.forName("com.sun.awt.AWTUtilities");
   Method method = 
          clazz.getMethod("setWindowOpaque", 
                               java.awt.Window.class, 
                               Boolean.TYPE);
   method.invoke(clazz, window, false);
   } catch (Exception e) {
         //Aqui el código de la acción
        //ha realizarse si no soprta la version de jdk
 
   }

Está excelente!!!!!! pero fijate que me tira este error:

Exception in thread “main” java.lang.IllegalArgumentException: The window must use a translucency-compatible graphics configuration
at com.sun.awt.AWTUtilities.setWindowOpaque(AWTUtilities.java:378)

que crees que puedo hacer????
como asignarle una configuración gráfica compatible a la ventana????

Write a comment

Advertise Here