Add JScrollPane to the panel displaying the image

I have this little program to display images on apanel .. but I can’t add scroll to it .. and this is my code

a class that extends jpanel to display an image:

public class ShowPanel extends JPanel{

public ShowPanel(BufferedImage image, int height, int width) {
  this.image = image;
  this.height = height;
  this.width = width;
  //this.setBounds(width, width, width, height);

}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(image, height, width, null);
}

public void setImage(BufferedImage image, int height, int width) {
    this.image = image;
    this.height = height;
    this.width = width;

}

private BufferedImage image;
private int height;
private int width;

}

and a sample main frame with another panel called imageContainer to hold the display panel:

public class MainFrame extends javax.swing.JFrame {

/** Creates new form MainFrame */
public MainFrame() {
    initComponents();
    image = null;
    path = "PantherOnLadder.gif";
    image = Actions.loadImage(path);
    showPanel = new ShowPanel(image, 0, 0);
    spY = toolBar.getY() + toolBar.getHeight();
    showPanel.setBounds(0, spY, image.getWidth(), image.getHeight());
    showPanel.repaint();
    imageContainer.add(showPanel);
    JScrollPane scroller = new JScrollPane(imageContainer);
    scroller.setAutoscrolls(true);


}

so i made a mistake here?

+3
source share
2 answers

, , scrollpane, . , . , .

, , , . . ImageIcon BufferedImage, Icon JLable, scrollpane, . - - .

+6

"imagecontainer" - , , , , . "showPanel" .

+1

Source: https://habr.com/ru/post/1722146/


All Articles