I'm working with a JDesktopPane
with drag-and-drop functionality and everything's going well so far, but I want to add a JLabel
at the center of the window that says something along the lines of "drag and drop file here" purely for decorative purposes. If possible, I want it to always be in the background even if there are JInternalFrame
s in front of them.
I haven't really found anything in here that solves my problem yet and I'm on the verge of giving up on this idea and leaving my program as it is. If there already is a solution to this but I haven't found it yet, please tell me so I can delete this question at once.
Override the paintComponent(...)
method of your JDesktopPane
.
The code might be something like:
JDesktopPane desktop = new JDesktopPane()
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
String text = "drag and drop here";
FontMetrics fm = g.getFontMetrics();
int stringWidth = fm.stringWidth( text );
int x = (getWidth() - stringWidth) / 2;
int y = getHeight() / 2;
g.drawString(text, x, y);
};
}
Read the section from the Swing tutorial on A Closer Look at the Paint Mechanism for more information on custom painting and to understand why this works.