Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: painting Image on JPanel inside a JScrollPane

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Thanked 0 Times in 0 Posts

    Default painting Image on JPanel inside a JScrollPane

    I am trying to build an application that displays an image in a JScrollPane. I have written a class that extends JPanel of which I override the paintComponent() function to paint the image on the JPanel. This is then added to the JScrollPane. The problem is this, the JPanel is displayed as the same size as the JScrollPane and no scrollbars appear, and only a portion of the image is displayed. I'll post some code excerpts below so you can get an idea of what I'm doing.

    The JPanel subclass
    public class xImagePanel extends JPanel {
        BufferedImage image;
        public xImagePanel(){
            image = null;
        public void setImage(String path) throws IOException{
            File f = new File(path);
            image = ImageIO.read(f);        
        public void paintComponent(Graphics g)
               Graphics2D g2 = (Graphics2D)g;
                g2.drawImage(image, 0,0, this);

    And the main class where it is added to the scrollpane. I didn't add the entire class because the scrollpane is part of a much larger GUI.

     try {
               JScrollPane jScrollPane1 = new JScrollPane();
               xImagePanel imagePanel = new xImagePanel();
                imagePanel.setSize(2560, 1600);
                imagePanel.setImage("...\testIMG.jpg");   //directory path excluded
            } catch (IOException ex) {
                Logger.getLogger(xbsMainFrame.class.getName()).log(Level.SEVERE, null, ex);

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: painting Image on JPanel inside a JScrollPane

    Try setting the preferred size of the JPanel:
    imagePanel.setPreferredSize(2560, 1600);//or whatever dimensions you wish

Similar Threads

  1. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  2. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  3. Drawing image on JPanel from another frame
    By jeryslo in forum AWT / Java Swing
    Replies: 3
    Last Post: December 8th, 2009, 04:01 PM
  4. count components inside a JPanel
    By dewboy3d in forum AWT / Java Swing
    Replies: 1
    Last Post: August 2nd, 2009, 03:17 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM