Wallpaper in a NetBeans RCP application
Posted by Nicklas Löf on February 18, 2011
Yesterday I wrote about my attempts to put an image in the root window of a Netbeans RCP application that is shown when no TopComponents are open.
Today I did finalize the code and added so I could add a logotype at the bottom on the right side and when the application window is resized it will stay at the bottom right side. So here is the complete code.
private ImageIcon bgimage;
@Override
public void restored() {
bgimage = new ImageIcon(ImageUtilities.loadImage("path/to/my/wallpaper.jpg"));
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame main = (JFrame) WindowManager.getDefault().getMainWindow();
JPanel mainPanel = new JPanel(new BorderLayout()) {
@Override
public void add(Component comp, Object constraints) {
super.add(comp, constraints);
if (constraints == BorderLayout.CENTER) {
if (comp instanceof JPanel) {
final JPanel panel = (JPanel) comp;
panel.setBorder(BorderFactory.createEmptyBorder());
panel.add(new BackgroundLabel(bgimage), java.awt.BorderLayout.CENTER);
}
}
}
};
main.setContentPane(mainPanel);
}
});
}
}
private class BackgroundLabel extends JLabel {
private boolean showBgImage = true;
private ComponentAdapter componentAdapter;
private Image image;
public BackgroundLabel(ImageIcon imageIcon) {
super(imageIcon);
image = ((ImageIcon) getIcon()).getImage();
setupAdapter();
TopComponent.getRegistry().addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (TopComponent.getRegistry().getOpened().isEmpty()) {
showBgImage = true;
repaint();
} else {
showBgImage = false;
}
}
});
}
@Override
public void paintComponent(Graphics g) {
if (showBgImage) {
int x = getParent().getSize().width - image.getWidth(this);
int y = getParent().getSize().height - image.getHeight(this);
g.drawImage(image, x, y, image.getWidth(this), image.getHeight(this), this);
}
}
private void setupAdapter() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
componentAdapter = new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
BackgroundLabel.this.setSize(getParent().getSize());
}
};
getParent().addComponentListener(componentAdapter);
}
});
}
}
Any image loaded into bgimage will be put at the bottom right corner of the application and stay there even when the window resizes.
To modify this behaviour just change the x and y parameters in paintComponent(Graphics g). For example setting them to 0,0 would draw the image at the top left corner. To stretch an image the width and height parameters should be modified.
Here is two screenshots showing this in action. The first one is just a quick logo I did created in GIMP since I can’t show my real application here, the other one is a fullscreen netbeans desktop background downloaded from http://netbeans.org/community/teams/evangelism/collateral.html.


Put a wallpaper in the root frame of a Netbeans RCP application « layer.xml said
[...] (RSS) « Place the quicksearch component anywhere you like Wallpaper in a NetBeans RCP application [...]
Geertjan said
Interesting and cool. It kind of relates to this:
-J-Dnetbeans.winsys.imageSource=org/netbeans/module1/farside.jpg
From: http://netbeans.dzone.com/news/advanced-tweaks-netbeans-windo
And: http://bits.netbeans.org/dev/javadoc/org-openide-windows/architecture-summary.html
ramblingbyte said
Hi!
Thanks for the awesome tutorial. I tried to do exact the same thing you describe but I couldn’t make it work. I created a independent module, defined a module installer and a BackgroundLabel class, but nothing happens when I fire up the application and to be honest I have no idea what I’m doing wrong. I’m pretty new to NetBeans RCP so any hint how to make it work will be greatly appreciated. I will check back in case you answer to reply. Thanks again.
Rafa said
And, where put all this code?