Buscar
Social
Ofertas laborales ES

Foro sobre Java SE > Heredar icono en JDialog desde un JFrame

Cordial saludo compañeros Hispanos.

Tengo un proyecto, realizado en Netbeans, en el cual todas las ventanas eran JFrame (una mala práctica de programación), decidí hacer las cosas bien hechas, así que voy a cambiar todos esos JFrame dejando un solo JFrame y el resto JDialog. Se supone que se deben heredar los atributos desde el Jframe, pero, cuando abro el JDialog desde un botón puesto en el JFrame llamado "prueba" el ícono que he personalizado para mi proyecto, aparece en el JFrame, pero, no en el JDialog, he tratado a modo de ensayo y error haciendo varios cambios, pero siempre me aparecen errores y la verdad no he encontrado en internet una solución. Agradezco enormemente cualquier colaboración. Los códigos son los siguientes:

* Jframe llamado presentacion

package soporteinternet005;

import javax.swing.ImageIcon;

public class Presentacion extends javax.swing.JFrame {

public Presentacion() {
initComponents();
setLocationRelativeTo(null);
setResizable(false);
setTitle("Soporte Virtual de Fallas de Internet ADSL");
setIconImage(new ImageIcon(getClass().getResource("/imagenes/poporo.png")).getImage());
}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

jButton2.setText("Ingresar como administrador");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jLabel1.setText("BIENVENIDOS");

jButton3.setText("Prueba");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(57, 57, 57)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addGap(191, 191, 191)
.addComponent(jLabel1)))
.addContainerGap(67, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jButton3)
.addGap(95, 95, 95))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(jLabel1)
.addGap(33, 33, 33)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(68, 68, 68))
);

pack();
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {


}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
VentanaFallas AbrirVF = new VentanaFallas(new javax.swing.JDialog(), true);
AbrirVF.setVisible(true);
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
Prueba AbrirP = new Prueba(new javax.swing.JFrame(), true);
AbrirP.setVisible(true);
}


public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
new Presentacion().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}


* JDialog llamado prueba


package soporteinternet005;


public class Prueba extends javax.swing.JDialog {


public Prueba(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();

}


@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

jLabel1.setText("La pruebita");

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(165, 165, 165)
.addComponent(jLabel1)
.addContainerGap(181, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(74, 74, 74)
.addComponent(jLabel1)
.addContainerGap(212, Short.MAX_VALUE))
);

pack();
}// </editor-fold>


public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
Prueba dialog = new Prueba(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {

@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
// End of variables declaration
}

junio 13, 2014 | Unregistered Commenterleo

"... Se supone que se deben heredar los atributos desde el Jframe,"

Una suposición errónea. Tus JDialog no heredan nada de tu JFrame. De hecho, tanto JFrame como JDialog heredan de Window, que es la clase que declara el método setIcomImage.

Si quieres que esos JDialog usen esa imagen, usa ese método en cada JDialog.

junio 13, 2014 | Registered Commenterchoces

Gracias por la respuesta. Si debo usar un método en cada JDialog, no le veo ninguna ventaja. Tomé la decisión de cambiar a JDialog para no tener que llamar el ícono en cada una de las ventanas con JDialog. No se si haya otra forma de realizar esto.

junio 13, 2014 | Unregistered Commenterleo

Siempre puedes extender JDialog, para incluir tu icono en el constructor, y que tus JDialog lo extiendan a su vez.
De todos modos, crear dicha extensión solo por el icono, me parece "matar moscas a cañonazos" :)

junio 14, 2014 | Registered Commenterchoces