I am relative new to Swing development, so please be patient...

I have created a scrollable table and would like to add (at runtime) a JComponent
(for example a chart linked to the table) that would sit on top of the table and would also
scroll aong with the table. The folowing code works fine

// Create a layered pane
JLayeredPane panel = new JLayeredPane();
panel.setLayout(new BorderLayout()); // Use this layout so that I don't have to mess
// with absolute positions. If I setLayout to null,
// I cannot get the table to display properly

// Create a main table and a header table
JTable mainTable = new JTable(); // I am using a more customized form in my code
JTable headTable = new JTable(); // I am using a more customized form in my code

// Create an additional component that will scroll with the table
JLabel topComponent = new JLabel("myLabel");

// Now add the main table and the aditional component to the layered pane

// Create a JScrollPane pane
JScrollPane scrollPane = new JScrollPane();

// and the layered pane to it, keeping the column header and row header in sync with
// the table - this is a standard way.
JViewport jv = new JViewport();


// Make the scroll pane the main pane

This code work just fine, but i think it will be difficult to add new components (like the
label above) at runtime, without the need of repainting the entire scrollable area.
Thus every time I will add a new component, I will have to revalidate the entire scroll

I am wondering if there is a more inteligent way to do it, so that I reproduce a
typical spreadsheet with multiple components being added at runtime ontop of the
scrollable table.( I tried adding the additional components to a glasPane, but they
obviously do not scoll with the table)

Thank you
Gregory Miecznik

I would follow the outline of a good book.

There some good online tutorials too, but they're never the same as a well coordinated book.

This article has been dead for over six months. Start a new discussion instead.