Java 添加JPanel时,JTable columns标头将消失

Java 添加JPanel时,JTable columns标头将消失,java,swing,jtable,jpanel,jscrollpane,Java,Swing,Jtable,Jpanel,Jscrollpane,将我的JTable(带有列标题的表)添加到JScrollPane中,两者都显示了良好的 如果相同的JTable+一些JPanel…都添加到主JPanel(BorderLayout),然后将此主JPanel添加到JScrollPane…当表显示时,表的列标题停止显示 知道为什么以及如何解决这个问题吗 somePanel=new JPanel (new FlowLayout ()); somePanel.setPreferredSize (new Dimension (600,50)); someP

将我的JTable(带有列标题的表)添加到JScrollPane中,两者都显示了良好的

如果相同的JTable+一些JPanel…都添加到主JPanel(BorderLayout),然后将此主JPanel添加到JScrollPane…当表显示时,表的列标题停止显示

知道为什么以及如何解决这个问题吗

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 */
仍然不知道为什么标题会消失

将JTable添加到JScrollPane时,JTable的JTableHeader将添加到滚动窗格的列标题中。这是JTable的特殊逻辑

如果将JTable直接添加到JPanel,则负责在面板上显示标题。比如:

JPanel panel = new JPanel( new BorderLayout() );
panel.add(table, BorderLayout.CENTER);
panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
panel.add(anotherPanel, BorderLayout.PAGE_END);
或者您可以自己将标题添加到滚动窗格>

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

你能发布你的代码吗?@Chris Phillips…嗨,我在问题中添加了代码…实际上是正常的代码…仍然不知道为什么标题会消失!!