Placeholder image in JViewport of JScrollPane for empty JTable

41 Views Asked by At

I have a JTable and want my user to drag&drop entries into that JTable.

To make that as obvious as possible, I want to display a placeholder image like "Please drag stuff HERE using your mouse!" in the background of the table, as long as the table is still empty.

Therefore, I've set up a custom JViewport using createViewport() of JScrollPane. See the following snippet from my JScrollPane:

@Override
protected JViewport createViewport() {
    return new JViewport() {
        @Override
        protected void paintComponent( Graphics g ) {
            super.paintComponent( g );
            // XXX Draw background image here XXX
        }
    };
}

To simplify the snippet, I've replaced the "draw a BufferedImage centered" stuff with a comment as it's not relevant to the question.

So far, the approach seems to work fine.

But of course, the background image is permanent, even if I add entries to the JTable.

How would you make it disappear as soon as the first entry is added to the JTable?

  • Add a TableModelListener that somehow triggers the viewport to paintComponent again?
  • Or is there a way for the JViewport to detect changes in "its" displayed component (its JTable) and redraw the Viewport then? (Would avoid coupling the data model and the Viewport.)
0

There are 0 best solutions below