0

I'm trying to add an image to my jframe using jscroll pane.I tried fewthings but the image appears only in the background.Something like this,the one below image editor.

enter image description here

here is my code:

   private void initComponents(){
     jScrollPane1 = new javax.swing.JScrollPane();
     ImageImplement panel = new ImageImplement(new ImageIcon(mean.get(0)).getImage());
     jScrollPane1.add(panel); setVisible(true); setSize(400,400); setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
   class ImageImplement extends JScrollPane { 
     private Image img; 
     public ImageImplement(Image img) { this.img = img;
     Dimension size = new Dimension(img.getWidth(null), img.getHeight(null)); 
     setPreferredSize(size);
     setMinimumSize(size);
     setMaximumSize(size); 
     setSize(size); 
     setLayout(null); } 
     public void paintComponent(Graphics g) { 
     g.drawImage(img, 0, 0, null); } 
     } 
ani
  • 181
  • 1
  • 2
  • 14
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with .. – Andrew Thompson May 22 '17 at 18:42
  • .. layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) `g.drawImage(img, 0, 0, null);` Since **every** `JComponent` is an `ImageObserver`, that would best be `g.drawImage(img, 0, 0, this);` – Andrew Thompson May 22 '17 at 18:43

1 Answers1

2

Don't extend JScrollPane and don't do custom painting.

Instead you just create a JLabel with an ImageIcon. Then you add the label to the scroll pane and the scroll pane to the frame.

So the basic logic is:

ImageIcon icon = new ImageIcon(...);
JLabel label = new JLabel( icon );
JScrollPane scrollPane = new JScrollPane( label );
frame.add(scrollPane, BorderLayout.CENTER);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • But then how do i set the dimension of the image – ani May 22 '17 at 18:28
  • @ani, you don't. The label will determine its preferred size automatically based on the dimensions of the image. – camickr May 22 '17 at 19:30
  • Can you tell me how can i replace the current image in JScrollPane with a new image – ani May 22 '17 at 21:20
  • @ani, Replace the Icon in the label. Read the JLabel API for the appropriate "setter" method. – camickr May 22 '17 at 21:49
  • https://stackoverflow.com/questions/44120528/unable-to-change-image-in-jscrollpane-upon-clicking-next?noredirect=1#comment75267543_44120528...I have asked this question here and given the code...But it doesnt seem to work – ani May 23 '17 at 06:25
  • @ani, `and given the code...` - no you haven't given the code. You have been asked multiple times to post a proper [mcve]. If you want help, then make an effort to ask a proper question. – camickr May 23 '17 at 19:28