Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8896243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:59:24+00:00 2026-06-14T23:59:24+00:00

I have a JScrollPanel , I add a JPanel containing an image to the

  • 0

I have a JScrollPanel, I add a JPanel containing an image to the JScrollPanel and it works, but if the image within the JPanel is larger that the JScrollPanel either in width, height, or both the panel is then resized to about 20×20. Why is it doing that? I am using the GridBagLayout on the viewport of the JScrollPanel. What I am expecting is that if the JPanel is larger than the JScrollPanel it will add scroll bars, but that isn’t what is happening. Any suggestions?

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    WebFileChooser wfc = null;
    if(wfc == null){
        wfc = new WebFileChooser(this, "Open an Image");
        wfc.setSelectionMode(SelectionMode.SINGLE_SELECTION);
        wfc.setAvailableFilter(GlobalConstants.IMAGES_AND_FOLDERS_FILTER);
        wfc.setChooseFilter(GlobalConstants.IMAGES_FILTER);
        wfc.setCurrentDirectory("/Users/Ryan/Desktop");
    }
    wfc.setVisible(true);

    if(wfc.getResult() == StyleConstants.OK_OPTION){
        String file = wfc.getSelectedFile().getPath();
        try{
            imagePane.remove(canvas);
        }catch(Exception e){
        }
        canvas = new Canvas();
        canvas.setVisible(true);
        canvas.setImage(file);
        //imagePane.getViewport().setLayout(new GridBagLayout());
        canvas.setSizeFromLoaded();
        imagePane.getViewport().add(canvas);
        imagePane.repaint();
        imagePane.revalidate();
    }
}

Here is the Canvas Class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pocketshop;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import pocketshop.util.ImageSync;

/**
 *
 * @author Ryan
 */
public class Canvas extends CanvasShadow{

    public static BufferedImage image = null, preview = null;
    public static int width, height;

    public void setImage(String filename){
        try{
            Canvas.image = ImageIO.read(new File(filename));
            Canvas.width = image.getWidth();
            Canvas.height = image.getHeight();
            ImageSync.originalPixels = new int[width * height];
            ImageSync.previewPixels = new int[width * height];
            Canvas.image.getRGB(0, 0, width, height, ImageSync.originalPixels, 0, width);
        }catch(IOException e){
        }
    }

    public static void setImage(BufferedImage image){
        Canvas.image = image;
        Canvas.width = image.getWidth();
        Canvas.height = image.getHeight();
        ImageSync.originalPixels = new int[width * height];
        ImageSync.previewPixels = new int[width * height];
        Canvas.image.getRGB(0, 0, width, height, ImageSync.originalPixels, 0, width);
    }

    public static void setPreview(BufferedImage img, Container parent){
        preview = img;
        parent.repaint();
    }

    public static BufferedImage deepCopy(){
        ColorModel cm = Canvas.image.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = Canvas.image.copyData(null);
        return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
    }

    public void setSizeFromLoaded(){
        try{
            this.setPreferredSize(new Dimension(Canvas.width + 10, Canvas.height + 10));
            this.setSize(Canvas.width + 10, Canvas.height + 10);
        }catch(Exception e){
            e.getMessage();
        }
    }

    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        if(Canvas.preview != null){
            g.drawImage(Canvas.preview, 5, 5, width, height, Color.black, null);
        }else{
            g.drawImage(Canvas.image, 5, 5, width, height, Color.black, null);
        }
    }
}

And here is the CanvasShadow Class:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pocketshop;

import java.awt.Color;
import org.jdesktop.swingx.JXPanel;
import org.jdesktop.swingx.border.DropShadowBorder;

public class CanvasShadow extends JXPanel{

    public CanvasShadow(){
        DropShadowBorder shadow = new DropShadowBorder();
        shadow.setShadowColor(Color.BLACK);
        shadow.setShowLeftShadow(true);
        shadow.setShowRightShadow(true);
        shadow.setShowBottomShadow(true);
        shadow.setShowTopShadow(true);
        this.setBorder(shadow);

        float[] hsb = new float[3];

        Color.RGBtoHSB(255, 255, 255, hsb);

        this.setBackground(Color.getHSBColor(hsb[0], hsb[1], hsb[2]));
    }
}

enter image description here

Here is the actual image: http://images2.fanpop.com/images/photos/4800000/Beach-beaches-4843817-1280-800.jpg

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:59:27+00:00Added an answer on June 14, 2026 at 11:59 pm

    I already told you get rid of that GridBagLayout. Setting the layout on the JScrollPane will resize the panel so it’s never bigger than the container. You’re panel has to lay itself out so that it’s the size of the the image in order for the JScrollPane to see it’s too big to display. It’s only then does JScrollPane add the scrollbars. So if your image is 2000×2000 then your panel needs to set it’s preferred size to 2000×2000 and act like it can be that big. The JScrollPane will do all of the heavy lifting to crop and make sure the panel only renders the portion visible based on the scroll bars. If your panel is always the same size as the viewport of the JScrollPane then the scroll pane thinks it fits and there is no need for scroll bars.

    Update: Quick google search revealed the answer: http://chaosinmotion.com/blog/?p=168

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTextArea in a JPanel that I would like to use a
I have a java class that extends from JPanel. The basic purpose of extending
I have a JScrollPane with a JPanel that implements Scrollable as its viewport view.
I have developed a desktop application. Now in that app I want to add
I have setup a JTable with paging - which works very well, but I
I have this code, where Canvas is a class I made that extends JPanel
I have a JPanel that contains a JScrollPane that contains a JTable. Inside my
I have a JPanel that holds a JScrollPane that holds a JPanel as such:
I have a popup menu that displays dynamically created custom JPanel objects in a
I have a JPanel where I add a JScrollPane (see class below). I added

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.