How to scroll a table with a overlaid component



My question is related to a typical example in a spread*** where I
can have an arbitrary component ontop of a scrollable table, which
should scroll along with the table.
The position of the overlaid component is arbitrary. A typical example
would be a
spread*** table with a graphical bar.

The only way I managed to achive it with some success is by creating a
scrollable table first,
then adding the scrollable table and my additional component to the
contentPane, which
has no layout manager, as in the code below:
----------------------------------------------------------------------------
// (1) Get the content pane from a JFrame
Container pane = myFrame.getContentPane();

// (2) Create a custom table with a header column
JTable jt = JTableCustom;
JTable jh = HeaderColumn;

// (3) Create a JScrollPane pane for this table
JScrollPane scrollPane = new JScrollPane();

// (4) Add the table to the scroll pane
JViewport jv = new JViewport();
jv.setView(jh);
jv.setPreferredSize(jh.getMaximumSize());
scrollPane.setViewportView(jt);
scrollPane.setRowHeader(jv); // Now I have a properly
scrollable table

// (5) Create an arbitrary JComponent and put it somehere ontop of the
table
JLabel label = new JLabel("A Label");
label.setBounds(200,200,200,200);

// (6) Add the scrollable table an my label to the pane
pane.add(label);
pane.add(scrollPane);
------------------------------------------------------------------------------------------------

This solution has only one problem - when I scroll pas the position of
my label (the label
scrolls nicely with the table),and then scroll back to where the label
was, the label does not repaint itself automatically - I have to
slightly change the window size to get the label to
reappear.

An obvious solution is to add a mouse listener and detect when I need
to repaint the label
but it is not a clean way to do it.

Do you know a propper way to do it? (I have tried various methods,
including a LayeredPane,
or adding a customized JPanel with the table and the label to the
viewPort, but this is the only
one that works reasonably well)

Thanks,
Gregory Miecznik

.


Quantcast