int preferredWidth = 0;
int preferredHeight = 0;
Component view = scrollPane.getView();
if (view != null) {
Dimensions preferredViewSize = view.getPreferredSize();
preferredWidth += preferredViewSize.width;
preferredHeight += preferredViewSize.height;
Component rowHeader = scrollPane.getRowHeader();
if (rowHeader != null) {
preferredWidth += rowHeader.getPreferredWidth(-1);
}
Component columnHeader = scrollPane.getColumnHeader();
if (columnHeader != null) {
preferredHeight += columnHeader.getPreferredHeight(-1);
}
if (scrollPane.getHorizontalScrollBarPolicy() == ScrollBarPolicy.ALWAYS) {
preferredHeight += horizontalScrollBar.getPreferredHeight(-1);
}
if (scrollPane.getVerticalScrollBarPolicy() == ScrollBarPolicy.ALWAYS) {
preferredWidth += verticalScrollBar.getPreferredWidth(-1);
}
}
return new Dimensions(preferredWidth, preferredHeight);
}