Question

I have a JSplitPane. i want to add background image to the left side of the JSplitPane.

mySplitPane.getLeftComponent 

this returs the left side component. and then i want to add an image background to the left side. i think i can use the Paint() to set background image to the JSplitPane. but how can i set on the only left side component.

I have a JTable on left side of my JSplitPane. i want to have JTable transparent. and then showing the background image.

Right now i have set background to my JTable. i dont want the background to be scrolled with JTable Scroll. Thats why i want to add background image to the Split Pane not the Table.

Was it helpful?

Solution

Could this be what you're looking for?

class ImagePanel extends JPanel {
    private Image image;
    public ImagePanel(Image image) {
        this.image = image;
    }
    @Override
    protected void paintComponent(Graphics g) {
       g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
    }
}

Usage:

BufferedImage myImage = ImageIO.load(...);
JPanel leftPanel = new ImagePanel(myImage);

//Add panel to splitpanel
JSplitPane mySplitPane= new JSplitPane(JSplitPane.HORIZONTAL_SPLIT)
mySplitPane.setLeftComponent(leftPanel);

Above I have created a subclass of JComponent. Override the paintComponent(Graphics g) method to paint the image that I want to display. I then set the content pane of the JPanel and then finally pass the panel to the left of the split pane

For more on Watermark background, see here for examples and code samples. Edited

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top