Problem with background image in JDialog

Hi, i'm using the next source code to paint a image in different JDialog and different JFrame with different widht and height each one:

import java.awt.*;

import javax.swing.*;

public class FondoGUI extends JPanel {

private static final long serialVersionUID = 1L;

//es una cargador de imagen un objeto que me dice cuando esta listo la imagen

MediaTracker tracker;

Image imagenFondo;

String nombreImagen ;

public FondoGUI(String nombreImagen){

this.nombreImagen = nombreImagen ;tracker=new MediaTracker(this);

imagenFondo=Toolkit.getDefaultToolkit().getImage

(nombreImagen);

//aade al cargador la imagen fondo

tracker.addImage(imagenFondo,0);

//el try intenta hacer lo que tiene

try{

tracker.waitForID(0); //.waitForALL();

//el parametro 0 depende de cuando adicionamos (imagenFondo,0) deben ser iguales

}

catch(InterruptedException e){}//por que el cargador utiliza hilos

}

}

public void paint(Graphics g){

g.drawImage(imagenFondo,0,0,getSize().width,getSize().height,this);

}

}

-

In the source code of windows JFrame i have written:

Container contenedor=getContentPane();

contenedor.setLayout(null);

FondoGUI pBackGround=new FondoGUI(pathImagenesProyecto + "Cuadro.JPG");

pBackGround.reshape(0,0,800,570);

contenedor.add(pBackGround);

the code works and not me lapel with nothing component, but if i use this code in JDialog me lapel. Why ?

Thanks a lot.

[1580 byte] By [pakkkAttacka] at [2007-11-27 10:41:00]
# 1

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

Custom painting is done by overriding the paintComponent(...) method, not the paint(...) method.

camickra at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 2

I feel it by my format and my english.

I change the method name but follows the same problem.

Why?

Thanks.

pakkkAttacka at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 3

> Why?

Don't know. Your code isn't compileable or executable.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

camickra at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 4

My "full" code is the next:

public class GestionUserGUI extends JDialog{

public GestionUserGUI(...){

...

Container contenedor=getContentPane();

contenedor.setLayout(null);

pBackGround=new FondoGUI(pathImagenesProyecto + "Cuadro.JPG");

pBackGround.reshape(0,0,800,570);

contenedor.add(pBackGround);

}

// In this constructor, i create a User Interface with the next component: JTabbbedPane (into this component: JLabel,JButton,JComboBox...)

}

The code that implements FondoGUI is the one of the first question.

For JFrame instead of JDialog, the code running perfectly, but JDialog the Image is put in first plane and single she appears this one.

Thanks

pakkkAttacka at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 5

That is not a SSCCE!

How to I copy, paste and compile that code?

camickra at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 6

GestionUsersGUI:

import java.awt.BorderLayout;

import java.awt.Container;

import javax.swing.*;

public class GestionUsersGUI extends JDialog{

/**

*

*/

private static final long serialVersionUID = 1;

JTabbedPane tabbedPane;

JPanel pInsercion,pModificacion,pBorrado ;

final String pathImagenesProyecto = "c:\\wamp\\ImagenesProyecto\\" ;

FondoGUI pBackGround;

// INSERCION DE USUARIOS

JLabel infoCiudad, infoPermiso ;

JComboBox ciudadesI, permisosI;

JLabel etiqUsuarioI ;

JTextFieldRest usuarioI ;

JTextFieldRest passwordI ;

JButton aplicarCambiosI;

//MODIFICACION DE USUARIOS

JComboBox ciudadesM,usuarioM,permisoM;

JButton aplicarCambiosM ;

JLabel infoCiudadM, infoUsuarioM, infoPermisoM;

// BORRADO DE USUARIOS

JLabel infoCiudadB,infoUsuarioB;

JComboBox ciudadesB,usuarioB;

JButton aplicarCambiosB ;

// CERRAR VENTANA

JButton cerrarVentana ;

// Informacion sobre INSERCION,BORRADO O MODIFICACION

//JLabel informacion;

//

String constante ;

/***

El String constante lo que nos dice es:

constante = 1 --> El que ha llamado a la ventana es adm. General

--> y adems no hay ninguna ciudad en el sistema

constante = 2 --> El que ha llamado a la ventana es adm. General

--> y adems YA EXISTEN EN EL SISTEMA CIUDADES.

--> En el array vienen todas las que puede gestionar

constante = 3 --> El que ha llamado a la ventana es adm. Ciudad

--> el cual en el array trae las ciudades en las

--> que puede hacer gestiones de usuario

*/

public GestionUsersGUI(GUI ventana,String constante){

super(ventana,"GESTION USUARIOS",true);

this.constante = constante ;

JPanel topPanel = new JPanel();

topPanel.setLayout( new BorderLayout() );

getContentPane().add( topPanel );

tabbedPane = new JTabbedPane();

panelInsertar();

tabbedPane.addTab("Insertar",pInsercion);

if (!constante.equals("1")){// No tiene sentido que solo exista la ciudad "TODAS LAS CIUDADES" y se modifiquen datos

panelModificar();

tabbedPane.addTab("Modificar",pModificacion);

}

panelBorrado();

tabbedPane.addTab("Borrar",pBorrado);

cerrarVentana = new JButton("Cerrar");

cerrarVentana.setBounds(52,300,150,25);

topPanel.add(cerrarVentana);

//informacion = new JLabel("OK");

topPanel.add( tabbedPane, BorderLayout.CENTER );

//topPanel.add( informacion, BorderLayout.SOUTH);

setResizable(false);

// PONERLE COLOR DE FONDO A LA VENTANA

Container contenedor=getContentPane();

contenedor.setLayout(null);

pBackGround=new FondoGUI(pathImagenesProyecto + "Cuadro.JPG");

pBackGround.reshape(0,0,300,450);

contenedor.add(pBackGround);

}

public void panelInsertar(){

pInsercion = new JPanel();

pInsercion.setLayout(null);

infoCiudad = new JLabel("Ciudad sobre la que se inserta el usuario:");

infoPermiso = new JLabel("Permiso que le asignamos:");

ciudadesI = new JComboBox();

ciudadesI.addItem("Ciudades...");

permisosI = new JComboBox();

permisosI.addItem("Permisos...");

etiqUsuarioI = new JLabel("Nombre de usuario: ");

usuarioI = new JTextFieldRest("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_",12,null,null);

JLabel etiqPasswordI = new JLabel("Password:");

passwordI = new JTextFieldRest("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",12,null,null);

aplicarCambiosI = new JButton("Aplicar");

infoCiudad.setBounds(5,5,250,25);

pInsercion.add(infoCiudad);

ciudadesI.setBounds(50,30,150,25);

pInsercion.add(ciudadesI);

infoPermiso.setBounds(5,55,250,25);

pInsercion.add(infoPermiso);

permisosI.setBounds(50,80,150,25);

pInsercion.add(permisosI);

etiqUsuarioI.setBounds(5,105,250,25);

pInsercion.add(etiqUsuarioI);

usuarioI.setBounds(50,130,150,25);

pInsercion.add(usuarioI);

etiqPasswordI.setBounds(5,155,250,25);

pInsercion.add(etiqPasswordI);

passwordI.setBounds(50,180,150,25);

pInsercion.add(passwordI);

aplicarCambiosI.setBounds(50,230,150,25);

pInsercion.add(aplicarCambiosI);

}

public void panelModificar(){

pModificacion = new JPanel();

pModificacion.setLayout(null);

infoCiudadM = new JLabel("Ciudad sobre la que se modifica el permiso:");

infoUsuarioM = new JLabel("Usuario al cual se le modifica:");

infoPermisoM = new JLabel("Permiso nuevo:");

ciudadesM = new JComboBox();

ciudadesM.addItem("Ciudades...");

usuarioM = new JComboBox();

usuarioM.addItem("Usuarios...");

permisoM = new JComboBox();

permisoM.addItem("Permiso nuevo...");

aplicarCambiosM = new JButton("Aplicar");

infoCiudadM.setBounds(5,5,250,25);

pModificacion.add(infoCiudadM);

ciudadesM.setBounds(50,30,150,25);

pModificacion.add(ciudadesM);

infoUsuarioM.setBounds(5,55,250,25);

pModificacion.add(infoUsuarioM);

usuarioM.setBounds(50,80,150,25);

pModificacion.add(usuarioM);

infoPermisoM.setBounds(5,105,250,25);

pModificacion.add(infoPermisoM);

permisoM.setBounds(50,130,150,25);

pModificacion.add(permisoM);

aplicarCambiosM.setBounds(50,230,150,25);

pModificacion.add(aplicarCambiosM);

}

public void panelBorrado(){

pBorrado = new JPanel();

pBorrado.setLayout(null);

infoCiudadB = new JLabel("Ciudad sobre la que se borra el usuario:");

infoUsuarioB = new JLabel("Usuario que se borra:");

ciudadesB = new JComboBox();

ciudadesB.addItem("Ciudades...");

usuarioB = new JComboBox();

usuarioB.addItem("Usuarios...");

aplicarCambiosB = new JButton("Aplicar");

infoCiudadB.setBounds(5,5,250,25);

pBorrado.add(infoCiudadB);

ciudadesB.setBounds(50,30,150,25);

pBorrado.add(ciudadesB);

infoUsuarioB.setBounds(5,55,250,25);

pBorrado.add(infoUsuarioB);

usuarioB.setBounds(50,80,150,25);

pBorrado.add(usuarioB);

aplicarCambiosB.setBounds(50,230,150,25);

pBorrado.add(aplicarCambiosB);

}

// METODOS DE LA INSERCION DE USUARIO

public String dameCiudadI(){

return ciudadesI.getSelectedItem().toString();

}

public String damePermisoI(){

return permisosI.getSelectedItem().toString();

}

public String dameUsuarioI(){

return usuarioI.getText();

}

public String damePasswordI(){

return passwordI.getText();

}

// METODOS DE LA MODIFICACION DE USUARIO

public String dameCiudadM(){

return ciudadesM.getSelectedItem().toString();

}

public String dameUsuarioM(){

return usuarioM.getSelectedItem().toString();

}

public String damePermisoNuevoM(){

return permisoM.getSelectedItem().toString();

}

// METODOS DEL BORRADO DE USUARIO

public String dameCiudadB(){

return ciudadesB.getSelectedItem().toString();

}

public String dameUsuarioB(){

return usuarioB.getSelectedItem().toString();

}

// METODO CONTROLADOR

public void controlador(CtrlGestionUsers ctrl){

// Pestaa Insertar

aplicarCambiosI.addActionListener(ctrl);

aplicarCambiosI.setActionCommand("INSERTAR");

ciudadesI.setActionCommand("CLICK_CIUDAD_I");

ciudadesI.addActionListener(ctrl);

// Pestaa Borrar

aplicarCambiosB.setActionCommand("BORRAR");

aplicarCambiosB.addActionListener(ctrl);

ciudadesB.setActionCommand("CLICK_CIUDAD_B");

ciudadesB.addActionListener(ctrl);

// Pestaa Modificar

if (!constante.equals("1")){

aplicarCambiosM.setActionCommand("MODIFICAR");

aplicarCambiosM.addActionListener(ctrl);

ciudadesM.setActionCommand("CLICK_CIUDAD_M");

ciudadesM.addActionListener(ctrl);

}

cerrarVentana.setActionCommand("CERRAR");

cerrarVentana.addActionListener(ctrl);

}

public void mensajeError(String msg){

//mensaje.setText(msg);

JOptionPane.showMessageDialog(this, msg, "Mensaje de error",

JOptionPane.ERROR_MESSAGE);

}

public void mensajeCorrecto(String msg){

JOptionPane.showMessageDialog(this, msg, "Mensaje",

JOptionPane.INFORMATION_MESSAGE);

}

public String dameConstante(){

return constante ;

}

public static void main(String args[]) {

vallas v = null;

String[] string = {"Algeciras","Malaga","Tarifa"};

GestionUsersGUI general = new GestionUsersGUI("Pestaas");

//CtrlGestionUsers ctrl = new CtrlGestionUsers(general,v,string);

//general.controlador(ctrl);

general.pack();

general.setBounds(5,10,300,500);

general.setVisible(true);

}

}

FondoGUI is the before code.

Thanks

pakkkAttacka at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...
# 7

That is still not a SSCCE.

Your question is about drawing an image. So only the code specifically related to drawing the image is needed. And that code should be compileable.

90% of the code is NOT related to drawing an image.

I don't care about the text fields and the combo boxes and the buttons. They are not related to the problem of drawing the image

Here is an example of a SSCCE:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=599393

Learn to simply your code when you have a problem. Its easier to problem solve on 20 lines of code then it is 200 lines of code.

camickra at 2007-7-28 19:10:10 > top of Java-index,Desktop,Core GUI APIs...