Not sure what your objective is. If you just want to be able to put an image up and be able to scroll it, then you simply set the Icon of your Label to the desired image - all is well. The following some sample code that will do that:
BufferedImage image = ImageIO.read(new File("/home/dlpinto/Desktop/Screenshot.png"));
JLabel label = new JLabel(new ImageIcon(image));
JScrollPane sp = new JScrollPane(label);
If you want to be able to put components up in the ScrollPane, with a background image - that is a different story - most like the use of a JLayeredPane is what you need. Let me know if that is indeed the objective.
Hi there;
No; i want to set a separete backgroung image for this label in the JScrollPane. This label is my main component, other than that
i have this image
Image watermarkImg = Toolkit.getDefaultToolkit().getImage("C:\temp\watermark.jpg");
I want to make this image as the background image to this JScrollPane(or to this label) and should be shown as a watermark(background image) behind this label. (Some thing similar to behing text in microsoft work).
My JScrollPane constructor is as follows:
JLabel imageLabel;
BufferedImage pageImage = renderer.getLastRenderedPage();
imageLabel.setIcon(new ImageIcon(tempBufferedImage));
JScrollPane jsp;
jsp = new JScrollPane(imageLabel);
thanks
madumm
Just in case it was the "other" objective - here is some code to help you do that...
//Load Image
BufferedImage image = ImageIO.read(new File("/home/dlpinto/Desktop/Screenshot.png"));
//Create Image Label
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(0, 0, image.getWidth(), image.getHeight());
//Create Layered Pane
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
//Create Desired Components
JLabel messageLabel = new JLabel("Hello World");
messageLabel.setOpaque(true);
messageLabel.setBounds(50, 50, 100, 100);
//Populate Layered Pane
layeredPane.add(label, JLayeredPane.DEFAULT_LAYER-1);
layeredPane.add(messageLabel, JLayeredPane.DEFAULT_LAYER);
//Create ScrollPane
JScrollPane sp = new JScrollPane(layeredPane);
Just read your reply... You don't need the JScrollPane to do what you want - but I suspect you can modify the code I posted to achieve what you want... Just create a LayeredPane, add the imageLabel, and then add the Label on top of that (set the opacity as you see fit)...
You can then place the LayeredPane where-ever you like...
Hope that helps...
Hi;
Thank alot for ur ideas; I tried out it in this way; but it didn't dispay anything. i Think there is something wrong in my code. Pls can u kindly go through it and tell me y is that?
JLabel imageLabel = null;
BufferedImage pageImage = renderer.getLastRenderedPage();
imageLabel.setIcon(new ImageIcon(pageImage));
imageLabel.setOpaque(true);
BufferedImage backgroundImage = processBackgroundImage_1();
JLabel backgroundLabel = new JLabel(new ImageIcon(backgroundImage));
backgroundLabel.setBounds(0, 0, backgroundImage.getWidth(), backgroundImage.getHeight());
private JLayeredPane layeredPane =null;
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(imageLabel.getWidth(), imageLabel.getHeight()));
layeredPane.add(backgroundLabel, JLayeredPane.DEFAULT_LAYER.intValue()-1);
layeredPane.add(imageLabel, JLayeredPane.DEFAULT_LAYER.intValue()-1);
layeredPane.validate();
JScrollPane jsp = new JScrollPane(layeredPane);
jsp.setVisible(true);
thanks
hi all;
i tried to solve my problem in this way too; but still it didn't work. can some one help me!!
BufferedImage pageImage = renderer.getLastRenderedPage();
Jlabel imageLabel.setIcon(new ImageIcon(pageImage));
imageLabel.setOpaque(false);
File filename = new File("D:/Sparx Work/pictures/watermark.jpg");
JScrollPane sp = new JScrollPane(pageImage);
sp.getViewport().setOpaque(false);
sp.setViewportBorder(new CentredBackgroundBorder(ImageIO.read(filename.toURL())));
JFrame f = new JFrame("BackgroundBorderExample");
f.getContentPane().add(sp);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600,400);
f.setLocationRelativeTo(null);
f.setVisible(true);
thanx
I included a complete code listing of a functional example. The main problem with wha tI shared earlier was that I did it quickly in Groovy, where everything is an object. When I called layeredPane.add(xxx, 4) - the number 4 was an object - whereas in Java it is not - this called the wrong function so that the label would not appear... ok - here is the code... run this with your own image and then modify it to do what you need...
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
public class BackgroundImageExample {
public static void main(String[] args) {
try {
//Load Image
String filename = "/home/dlpinto/Desktop/Screenshot.png";
BufferedImage image = ImageIO.read(new File(filename));
//Create Image Label
JLabel label = new JLabel(new ImageIcon(image));
label.setBounds(0, 0, image.getWidth(), image.getHeight());
//Create Layered Pane
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayout(null);
layeredPane.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
//Create Desired Components
JLabel messageLabel = new JLabel("Hello World");
messageLabel.setOpaque(true);
messageLabel.setBounds(50, 50, 100, 100);
//Populate Layered Pane
layeredPane.add(label, new Integer(JLayeredPane.DEFAULT_LAYER-1));
layeredPane.add(messageLabel, JLayeredPane.DEFAULT_LAYER);
//Create ScrollPane
JScrollPane sp = new JScrollPane(layeredPane);
//Create & Display Frame
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.add(sp);
frame.setVisible(true);
System.out.println(layeredPane.getLayer(messageLabel));
System.out.println(layeredPane.getLayer(label));
} catch (Exception e) {
e.printStackTrace();
}
}
}
> i tried to solve my problem in this way too; but still it didn't work. can some one help me!!
Did you try the link to my code in reply #6? It really does work and I use it
all the time. That particular example has a stale image URL, but you can
substitute this for it:
http://blogs.sun.com/jag/resource/JagHeadshot.jpg
Hi equilibric and DrLaszloJamf
Thanks alot again
Yes tried out both examples but the problem in my case is The foreground labe which i'm using is not just a label where u can separate text.
In equilibric; u have used a label
JLabel messageLabel = new JLabel("Hello World");
in DrLaszloJamf have use a area; there also u can separete text
JTextArea area = new JTextArea();
area.read(new FileReader("CentredBackgroundBorder.java"), null);
in my case:
private JLabel imageLabel = new JLabel();
BufferedImage pageImage = renderer.getLastRenderedPage();
imageLabel.setIcon(new ImageIcon(tempBufferedImage));
I have set a buffered image to label's icon, so it also acting as a image. So when i set my imageLabel in front of my backgroung image, it doesn't visible the backimage because of the foreground image.
thanx
Regards
madumm
Can u give some solution to my prob