I want to add a Image into a JPanel, the JPanel is inside JInternalFrame, and the JInternalFrame has a parent, the JDesktopPane.
I've tried with another class to add a Image from a path, and then, jPanel1 = new Imagen(<String FilePath>);
, the class will receive the path in String, in the window constructor, but it does not work. and I have tried by customizing code from the initComponents()
, the line jPanel1 = javax.swing.JPanel();
and replacing this line by jPanel1 = new Imagen();, the parameter that the constructor will receive, is the path of the image file, and this method works fine, but it only run one time (inside the initComponents() method), and I can't (or I don't known) how to replace the image from the JPanel. :(
The project starts first with a JDesptopPane, an then one JInternalFrame (1), from this internalframe, it shows other JInternalFrame(2), from this internalFrame, it shows other JInternalFrame(3), in the internalFrame 3, I want to find with a button, and JFileChooser, an image, and the path received by the filechooser, I want to set as parameter in the Imagen() class. and set the image into a JPanel that is builded into the JInternalFrame(3).
EDIT 1: Thanks for your comments. Here the code of JInternalFrame(3), the window constructor:
public AgregarContacto() {
JFileChooser jf = new JFileChooser();
jf.setDialogTitle("Elija el archivo de Imagen");
int i = jf.showOpenDialog(null);
if (i == JFileChooser.APPROVE_OPTION) {
default1 = jf.getSelectedFile().getAbsolutePath();
}
initComponents();
jPanel1= new Imagen(default1);
for (String llenarMese : p.llenarMeses()) {
Mes.addItem(llenarMese);
}
for (String llenarDia : p.llenarDias31()) {
Dia.addItem(llenarDia);
}
for (String llenarAnios : p.Anios()) {
Anio.addItem(llenarAnios);
}
}
And here the code of the class Imagen() that will receive in the Constructor a string:
public class Imagen extends JPanel {
String ruta;
private BufferedImage imag;
public Imagen(String path){
ruta = path;
try{
imag = ImageIO.read(new File("unknown.png"));
}catch(IOException ex){
JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
}
}
@Override
public void paint(Graphics g){
super.paint(g);
Image Scaledimage = imag.getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_SMOOTH);
g.drawImage(Scaledimage, 0, 0, null);
}
}