473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

how to add an image to Jpanel

momotaro
357 100+
Hi everyone,

I have my Jpanel created using netbeans wizard (drag and drop) and I have my class ImageLoader which prepare the image but am stuck at how I make the image to show on my panel...
here is the code for ImageLoader:
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package myphotoshop.logic;
  6.  
  7. import java.awt.Graphics;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import javax.imageio.ImageIO;
  12. import javax.swing.JPanel;
  13.  
  14. /**
  15.  *
  16.  * @author AYACH MOHAMED
  17.  */
  18. public class ImageLoader extends JPanel{
  19.  
  20.     private BufferedImage image;
  21.  
  22.     public ImageLoader(String imageName) {
  23.        try {
  24.             image = ImageIO.read(new File(imageName));
  25.  
  26.  
  27.        } catch (IOException ex) {
  28.             // handle exception...
  29.            System.out.println("lol");
  30.        }
  31.     }
  32.  
  33.     @Override
  34.     public void paintComponent(Graphics g) {
  35.         g.drawImage(image, 0, 0, null); // see javadoc for more info on the parameters
  36.  
  37.     }
  38. }
  39.  
Here is the code to chose the file :
Expand|Select|Wrap|Line Numbers
  1. private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
  2.         // TODO add your handling code here:
  3.  
  4.         FileDialog fileChoser = new FileDialog(this);
  5.         fileChoser.setVisible(true);
  6.  
  7.         ImageLoader loadimage = new ImageLoader(fileChoser.getDirectory() 
  8.                                                 + fileChoser.getFile());  
  9.     }
And here is the generated code for the Jpanel by netbeans:
Expand|Select|Wrap|Line Numbers
  1. Panel = new javax.swing.JPanel();
  2.  
  3. Panel.setBackground(new java.awt.Color(255, 255, 255));
  4.  
  5. // Code of sub-components - not shown here
  6.  
  7. // Layout setup code - not shown here
  8.  
  9. // Code adding the component to the parent container - not shown here
please help thank's in advance.
Jan 19 '12 #1
0 1910

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Moth | last post by:
I have a PlotPanel class that extends JPanel which I have used previously to display charts in swing guis. I now want to display a graph in a jsp page so I was going to generate the graph and then...
1
by: M.Kamermans | last post by:
I have a rudimentary gui with a class that extends JPanel, which sets its content to a jpg image using: if (im != null) { g.drawImage(im,0,0,null); } in the paint() method. However, when I...
4
epots9
by: epots9 | last post by:
I have no clue how to print, but i have this: PrinterJob print = PrinterJob.getPrinterJob(); boolean okToPrint = print.printDialog(); if(okToPrint) { ...
5
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
1
by: chanshaw | last post by:
Alright so basically I have a logo named "DirSync.jpg" and I want to display it on the top portion of my application window in a container (either JLabel or JPanel which ever is normally recommended)...
1
by: chanshaw | last post by:
How would I go about using an image as a background inside of a JPanel? I want to be able to place components on top of my background image.
7
by: KiddoGuy | last post by:
Hello, I'm trying to add an icon to a JPanel in the event where a button is pressed. I have the following code: private void button1ActionPerformed(java.awt.event.ActionEvent evt) { ...
2
by: chanshaw | last post by:
Alright I'm trying to place the textarea on top of the image but right now it displays the textarea below the image, how do i go about doing this. import java.awt.Color; import...
1
by: Rudiger Diezman | last post by:
I am using a tabbed pane with four tabs - three of which work perfectly - the fourth one is supposed to display an image but does NOT ever call the paint method. When I use the same ImagePanel code...
0
by: phpuser123 | last post by:
I have a class test2 containing a button.When I press this button, I want to instantiate an object from Test3 and display the image in a panel on the newly created object.. test2 class ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.