My question here is how can I display an image into a JPanel? Other topics here asking something similar aren't clear to my about how can I do that.
I have a directory in my project folder that have image files Project Folder/GUI/img
, specifically gray.png
and green.png
which I want to display in a JPanel.
I tried with the following code using ImageIcon
and JLabel
that I found in other post:
ImageIcon image = new ImageIcon("GUI/img/gray.png");
JLabel label = new JLabel(image);
//JPanel panel is already initialized by the IDE
panel.add(label)
But doesn't work... The JPanel
remain empty without displaying any image. How can I do that?
Additional to this, I want that the image inside the JPanel
change (gray.png
for green.png
) when some action is performed, for example pressing a button. I can archive that by the same method for display the image in the JPanel
right?
Thanks in advance!
EDIT: Here's an example of a test code for get this. The initialization is done automatically by the IDE.
import java.awt.Image;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Sample extends javax.swing.JFrame {
public Sample() {
initComponents();
}
//Initialization
private void initComponents() {
PanelImage = new javax.swing.JPanel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
javax.swing.GroupLayout PanelImageLayout = new javax.swing.GroupLayout(PanelImage);
PanelImage.setLayout(PanelImageLayout);
PanelImageLayout.setHorizontalGroup(
PanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
PanelImageLayout.setVerticalGroup(
PanelImageLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
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(61, 61, 61)
.addComponent(PanelImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(239, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(45, 45, 45)
.addComponent(PanelImage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(155, Short.MAX_VALUE))
);
pack();
}
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
DisplayImage(PanelImage, "/GUI/img/gray.png");
} catch (Exception ex) {
Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
}
}
//For display the url image in a JPanel
private void DisplayImage(JPanel jp, String url) throws IOException, Exception {
try {
Image image=ImageIO.read(this.getClass().getResource(url));
ImageIcon imageicon=new ImageIcon(image);
JLabel label=new JLabel(imageicon);
jp.add(label);
} catch (IOException ex) {
throw new IOException();
} catch (Exception ex) {
throw new Exception();
}
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Sample.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Sample().setVisible(true);
}
});
}
// Variables declaration
private javax.swing.JPanel PanelImage;
}
The private void DisplayImage(JPanel jp, String url)
is what I need to work for display the image, from the url String url
in the JPanel jp