1

This is the screenshot.

enter image description here

This is my code to build and set the header.

private void buildHeader() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    
    panel.add(Box.createHorizontalStrut(10));
    panel.add(new JLabel("Documents"));
    panel.add(Box.createHorizontalGlue());
    panel.add(new JButton("View"));
    panel.add(new JButton("Print"));
    
    panel.setBorder(BorderFactory.createLineBorder(new Color(210, 210, 210)));
    panel.setBackground(new Color(245, 245, 245));      
    panel.setPreferredSize(new Dimension(panel.getPreferredSize().width, 51));
    
    setColumnHeaderView(panel);
}

But, I am not able to make the header to take its whole space. It is leaving the space above vertical scroll bar.

So, How can I make this column header to take up its whole width.

Community
  • 1
  • 1
Akshat
  • 720
  • 9
  • 24
  • as your header doesn't need to be aligned with the viewportView, you might be better off keeping it outside of the scrollPane's control - with a suitable LayoutManager it will be placed above the scrollPane and sized to the full width of the scrollPane (f.i. a BorderLayout and the "header" added to the north) – kleopatra Oct 14 '13 at 09:55

2 Answers2

3

But, I am not able to make the header to take its whole space. It is leaving the space above vertical scroll bar.

  • by placing the JComponent to the Corner (there yould be an issue with Borders)

  • or by moving JPanel contians JButtons out of the JScrollPane

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • But the link also shows header taking up its full space, above vertical scroll bar. – Akshat Oct 14 '13 at 05:39
  • 3
    @Akshat: [Don't use `setPreferredSize()`](http://stackoverflow.com/q/7229226/230513). – trashgod Oct 14 '13 at 09:17
  • 2
    @Akshat _shows header taking up its full space, above vertical scroll bar_ no it doesn't - look carefully and don't forget to _read_ the tutorial :-) – kleopatra Oct 14 '13 at 09:50
2

This is how headers work, they are designed to provide header information to the viewable area, so they have to be constrained to the width of the viewable area.

Instead of setting the column header view, try adding the panel to the NORTH position of the parent container and the scroll pane to the CENTER position.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366