| | |
Adding components at runtime ontop of a scrollable table
![]() |
•
•
Join Date: Nov 2007
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- pagination and search (PHP)
- Java GUI JOptionPane.showMessageDialog (Java)
- need help with tables (VB.NET)
- Subpanel problem (Java)
- How to position buttons/boxes in a JTabbed Pane? (Java)
- Adding components to JApplet (Java)
Other Threads in the Java Forum
- Previous Thread: Username & Password Verification
- Next Thread: Help on a reading from a file programming
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






