3
votes

What part of JScrollPane code is responsible for displaying the column headers of JTable?

If you just add a JTable to a JPanel, it won't show the headers by default. You should either pass the table to JScrollPane's constructor or call JScrollPane setViewportView with the table as the argument. So actually, what makes the column headers visible? Is it part of the internal rendering of the JScrollPane (updateUI and getUI methods)?

Initially I thought JScrollPane uses its setColumnHeaderView to accomplish this, but it doesn't (pass null to this method, the table will still display the headers).

1

1 Answers

2
votes

The JTable is responsible for doing this.

The addNotify() method of JTable is overridden. Basically this method is invoked when a component is added to a visible container.

So the JTable implementation checks if the parent of the table is a JViewport. If so it adds the table header to the scroll pane using the setColumnHeaderView(...) method.