Reputation: 191
adding my JTable(table with columns header) to a JScrollPane ,both shows fine...
If the same JTable + some JPanel...both add to a main JPanel(BorderLayout) then add this main JPanel to JScrollPane ....the columns header of the table stops showing while the table shows ?!
any idea why and how to solve this ..
somePanel=new JPanel (new FlowLayout ());
somePanel.setPreferredSize (new Dimension (600,50));
somePanel.setBackground (Color.lightGray);
mainPane=new JPanel (new BorderLayout ());
mainPane.setPreferredSize (new Dimension (600,550));
mainPane.setBackground (Color.lightGray);
mainPane.add (somePanel,BorderLayout.NORTH);
mainPane.add (table,BorderLayout.CENTER);
scrollBar=new JscrollBar();
scrollBar.setVisible (true);
scrollBar.setVisibleAmount (10);
scrollBar.setEnabled (true);
scrollBar.setOrientation (Adjustable.VERTICAL);
scrollPane=new JScrollPane ();
scrollPane.setVerticalScrollBar (scrollBar);
scrollPane.setVerticalScrollBarPolicy
(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize (new Dimension (600,650));
scrollPane.setViewportView (mainPane);
/*the createExamsSchedualTable() is inside inner class in the Jframe
and called by class constructor*/
private void createExamsSchedualTable(){
table=new JTable (new TheTableModel ());
table.setPreferredScrollableViewportSize (new Dimension (600,570));
table.setFillsViewportHeight (true);
table.setAutoResizeMode (JTable.AUTO_RESIZE_ALL_COLUMNS);
table.setAutoCreateRowSorter (true);
table.setSelectionBackground (Color.LIGHT_GRAY);
table.setDragEnabled (true);
table.setGridColor (Color.LIGHT_GRAY);
table.setIntercellSpacing (new Dimension (3,3));
table.setRowSelectionAllowed (true);
table.setColumnSelectionAllowed (true);
table.setCellSelectionEnabled (true);
table.setShowGrid (true);
table.setShowHorizontalLines (true);
table.setVerifyInputWhenFocusTarget (true);
table.setToolTipText ("Exams Scheduals Times Table");
}
/*TheTableModel class extends AbstractTableModel */
Upvotes: 1
Views: 305
Reputation: 324197
still don't know why the header disappear though !!
When you add a JTable to a JScrollPane the JTableHeader of the JTable is added to the column header of the scroll pane. This is special logic of the JTable.
If you add a JTable directly to a JPanel, then you are responsible for displaying the header on the panel. Something like:
JPanel panel = new JPanel( new BorderLayout() );
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
panel.add(anotherPanel, BorderLayout.PAGE_END);
Or you can add the header to the scrollPane yourself>
JPanel panel = new JPanel(...);
panel.add(table, ...);
panel.add(anotherPanel, ...);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setColumnHeaderView(table.getTableHeader());
Upvotes: 2