Image as a background in JScrollPane

2.1k views Asked by At

How to add an image as a background in JScrollPane? I tried this but the image doesn't display:

BufferedImage img = null;
    try {
        img = ImageIO.read(new File("C:\\Users\\Suraj\\Documents\\NetBeansProjects\\JavaApplication2\\src\\javaapplication2\\images\\2.jpg"));
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
    Image imag = img.getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH);
    ImageIcon imageBack = new ImageIcon(imag);
    FlowLayout fl = new FlowLayout();
    frame.getContentPane().setLayout(fl);
    fl.addLayoutComponent(null, new JLabel(imageBack));

EDIT : I would like to add jlabels and jbuttons on the JScrollPane with the background

1

There are 1 answers

0
Hovercraft Full Of Eels On BEST ANSWER

If your goal is to simply show an image in a JScrollPane without showing other components (such as a JTable) in the JScrollPane, then you should:

  • Make an ImageIcon out of your image via new ImageIcon(myImage)
  • Add that Icon to a JLabel
  • Place the JLabel into the JScrollPane's viewport, something that can be done by passing the JLabel into the JScrollPane's constructor.

And your done.

If you need to do something else, then describe your problem in greater detail.

For example,

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

@SuppressWarnings("serial")
public class ImageInScrollPane extends JPanel {
   public static final String IMAGE_PATH = "http://image.desk7.net/"
         + "Space%20Wallpapers/1422_1280x800.jpg";
   private static final int PREF_W = 500;
   private static final int PREF_H = 400;


   public ImageInScrollPane() throws IOException {
      URL imageUrl = new URL(IMAGE_PATH);
      BufferedImage img = ImageIO.read(imageUrl);
      Icon icon = new ImageIcon(img);
      JLabel label = new JLabel(icon);
      JScrollPane scrollPane = new JScrollPane(label);

      setLayout(new BorderLayout());
      add(scrollPane);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      ImageInScrollPane mainPanel = null;
      try {
         mainPanel = new ImageInScrollPane();
      } catch (IOException e) {
         e.printStackTrace();
         System.exit(-1);
      }

      JFrame frame = new JFrame("ImageInScrollPane");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

You ask in comment:

what if I have to add other objects like buttons?....How do I do it?

In this situation, I'd create a class that extends JPanel and display the image in this JPanel by drawing it within the paintComponent method override (if you search on this, you'll find many examples, some by me), then add the buttons/components to this image-drawing JPanel, and then adding this JPanel into the JScrollPane's viewport as I do with the JLabel above.