JScrollPane – Column Header


Set Column Header Size (i.e. Extent Size)

Dimension extentDim = jScrollPane1.getColumnHeader().getExtentSize();
System.out.println("Column Header Extent Size: (" + (int) extentDim.getWidth() + "w, " + (int) extentDim.getHeight() + "h)");

Set Column Header to JPanel

JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.BLUE, 4));
panel.setPreferredSize(new Dimension(2000, 200));
jScrollPane1.setColumnHeaderView(panel);

Set Column Header to JTableHeader

Object view = jScrollPane1.getViewport().getView();
if (view != null && view instanceof JTable) {
    jScrollPane1.setColumnHeaderView(((JTable) view).getTableHeader());
}
Note:
    When the JScollPane View is a JTable, the Column Header is set (or can be reset)
    to the JTable's JTableHeader

Get Column Header Position

if(jScrollPane1.getColumnHeader() != null){
    Point position = jScrollPane1.getColumnHeader().getViewPosition();
    System.out.println("Row Header Component Position: (" + position.x + ", " + position.y + ")");
}

Set Column Header Position

if (jScrollPane1.getColumnHeader() != null && jScrollPane1.getColumnHeader().getView() != null) {
    jScrollPane1.getColumnHeader().setViewPosition(new Point(100, 100));
}


Get Column Header Component (i.e. View) Preferred Size

if (jScrollPane1.getColumnHeader() != null) {
    Point position = jScrollPane1.getColumnHeader().getViewPosition();
    System.out.println("Column Header Component Position: (" + position.x + ", " + position.y + ")");
}

Get Column Header Component (i.e. View) Size

if (jScrollPane1.getColumnHeader() != null) {
    Dimension compDim = jScrollPane1.getColumnHeader().getViewSize();
    System.out.println("Column Header Component Size: (" + (int) compDim.getWidth() + "w, " + (int) compDim.getHeight() + "h)");
}
Note:
    To ensure proper automatic scrolling, set the width of the Column Header's View
    to match jScrollPane1.getViewport().getViewSize().getWidth()

Set Column Header Component (i.e. View) Size

if (jScrollPane1.getColumnHeader() != null && jScrollPane1.getColumnHeader().getView() != null) {
    jScrollPane1.getColumnHeader().getView().setSize(new Dimension(3000, 3000));
    jScrollPane1.getColumnHeader().getView().setPreferredSize(new Dimension(3000, 3000));
}
Note:
    To ensure proper automatic scrolling, set the width of the Column Header's View
    to match jScrollPane1.getViewport().getViewSize().getWidth()

Scroll the Column Header to a Desired Rectangle

if (jScrollPane1.getColumnHeader() != null) {
    Rectangle rect = new Rectangle(200,200,500,500);
    jScrollPane1.getColumnHeader().scrollRectToVisible(rect);
}
Description:
    With the current possition of the Column Header's upper left corner being considered
    (0,0), scroll to make the provided Rectangle as visible as possible.

Set the Column Header Scroll Mode

if (jScrollPane1.getColumnHeader() != null) {
    jScrollPane1.getColumnHeader().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
}
Description:
    Used to set the drawing mode used during scrolling the Column Header

    BLIT_SCROLL_MODE (default): A fast method of drawing (Bit Block Transfer) that uses Graphics.copyArea
    BACKINGSTORE_SCROLL_MODE: Painting will start at this JViewport (not an ancestor)
    SIMPLE_SCROLL_MODE: Redraw from stratch everytime