Hi,
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
layeredPane.add(topComponent);
layeredPane.add(mainTable);
// 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();
jv.setView(jh);
jv.setPreferredSize(jh.getMaximumSize());
scrollPane.setViewportView(layeredPane);
scrollPane.setRowHeader(jv);
scrollPane.setColumnHeaderView(mainTable.getTableHeader());
// Make the scroll pane the main pane
myFrame.setContentPane(scrollPane);
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
pane.
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