Question

So, I have a grid layout which stores JScrollPane's in each cell. These are also put into an array for other purposes. The "View" extends "JPanel" so it's just a regular panel with image support. The application starts up with cell's filled with scrollPane's that contain the View which doesn't have a image yet.

At that point I see no scrollbar, but that doesn't matter since there is nothing inside the JPanel. As soon as I open an image and use drawImage in the paintComponenet of the JPanel I don't see scrollbar's showing up. Here's how I create the grid and the Scrollpane

private void createContentPane() 
{
        GridLayout gridLay = new GridLayout(GRID_ROWS, GRID_COLUMNS);
        perspectiveTbl = new JScrollPane[NUM_PERSPECTIVE];
        mainPane = new JPanel();
        mainPane.setLayout(gridLay);

        int idx = 0;

        while(idx < perspectiveTbl.length)
        {
            perspectiveTbl[idx] = new JScrollPane(new View(modelImage));
            mainPane.add(perspectiveTbl[idx]);
            idx++;
        }

        this.getContentPane().add(mainPane, BorderLayout.CENTER);
}

I'm not exactly sure why the scrollbar's aren't showing up, should they have been set inside the panel for the image?

Here's an image of the application, as you can see the picture of the shoe does not receive scrollbar's so there is no way to view the rest of the picture:

Picture

Was it helpful?

Solution 2

Thanks for the hint Stanislav, I actually figured it out and got it working an hour ago, but you did give the right path to fix it with the preferredSize attribute. I ended up re-implemented getPreferredSize with the size of the image inside the panel, added revalidate to the paint event so that the bars show up as soon as the image is loaded.

public void paintComponent(Graphics g) 
{
    super.paintComponent(g);
    g.drawImage(image, 0, 0, null);
    this.getViewScrollPane().revalidate();
}

public Dimension getPreferredSize() 
{
    if(image != null)
        return new Dimension(image.getWidth(),image.getHeight());
    else
        return super.getPreferredSize();
}

OTHER TIPS

You can either user not JPanel with image but usual JLabel with the image

or

call setPreferredSize() for the panels to reflect the image's size.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top