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
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:
new ImageIcon(myImage)
And your done.
If you need to do something else, then describe your problem in greater detail.
For example,
You ask in comment:
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.