Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a jpg image on a JPanel

Tags:

java

image

jpanel

What would be the most appropriate image type to display a jpg image (loaded from a local folder) on a JPanel?

Cheers.

like image 286
burntsugar Avatar asked Aug 07 '09 02:08

burntsugar


People also ask

How do I add an image to a panel?

To add an image to JPanel, the Java Swing framework provides built-in classes such as ImageIO and ImageIcon that you can use to fetch an image. Here, we will add an image to JLabel which will be added to JPanel. It is quite easy, let's see the example.

How do I add an image to a JFrame?

If you want to add an image, choose the JPictureBox, after that go to Properties and find "icon" property and select an image.

How do you add an image to a JLabel?

You have to supply to the JLabel an Icon implementation (i.e ImageIcon ). You can do it trough the setIcon method, as in your question, or through the JLabel constructor: Image image=GenerateImage. toImage(true); //this generates an image file ImageIcon icon = new ImageIcon(image); JLabel thumb = new JLabel(); thumb.


2 Answers

ImageIcon image = new ImageIcon("image/pic1.jpg");
JLabel label = new JLabel("", image, JLabel.CENTER);
JPanel panel = new JPanel(new BorderLayout());
panel.add( label, BorderLayout.CENTER );
like image 166
Eugene Ryzhikov Avatar answered Oct 15 '22 10:10

Eugene Ryzhikov


You could use a javax.swing.ImageIcon and add it to a JLabel using setIcon() method, then add the JLabel to the JPanel.

like image 42
Sean A.O. Harney Avatar answered Oct 15 '22 09:10

Sean A.O. Harney