简体   繁体   English

添加JPanel时JTable列标题消失

[英]JTable columns header dissapear when adding JPanel

adding my JTable(table with columns header) to a JScrollPane ,both shows fine... 将我的JTable(带有列标题的表)添加到JScrollPane,两者都很好...

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 ?! 如果相同的JTable +一些JPanel ...都添加到主JPanel(BorderLayout)中,则将此主JPanel添加到JScrollPane ....表的列标题停止显示,而表显示?!

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 */

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. 将JTable添加到JScrollPane时,JTable的JTableHeader会添加到滚动窗格的列标题中。 This is special logic of the JTable. 这是JTable的特殊逻辑。

If you add a JTable directly to a JPanel, then you are responsible for displaying the header on the panel. 如果将JTable直接添加到JPanel,则负责在面板上显示标题。 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> 或者您可以自己将标题添加到scrollPane>

JPanel panel = new JPanel(...);
panel.add(table, ...);
panel.add(anotherPanel, ...);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setColumnHeaderView(table.getTableHeader());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM