JTableを配置したJScrollPaneの右上コーナー区画に配置されるコンポーネントを取得する
Total: 1068, Today: 1, Yesterday: 2
Posted by aterai at
Last-modified:
Summary
JTableを配置したJScrollPaneの右上コーナー区画に配置されるコンポーネントを取得し、JTableの幅よりJViewportの幅が大きくなる場合はそれを非表示に切り替えます。
Screenshot

Advertisement
Source Code Examples
// System.out.println(UIManager.get("Table.scrollPaneCornerComponent"));
JTable table = new JTable(15, 3) {
@Override public boolean getScrollableTracksViewportWidth() {
Container c = SwingUtilities.getAncestorOfClass(JScrollPane.class, this);
if (c instanceof JScrollPane) {
JScrollPane scroll = (JScrollPane) c;
Component ur = scroll.getCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER);
if (ur != null) {
ur.setVisible(
getPreferredSize().width >= scroll.getViewport().getWidth());
}
}
return super.getScrollableTracksViewportWidth();
}
};
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
View in GitHub: Java, KotlinDescription
- 上:
NimbusLookAndFeelのデフォルトではJTableを配置したJScrollPaneの右上コーナー区画にUIManager.put("Table.scrollPaneCornerComponent", c)でコンポーネントが配置されているMetalLookAndFeelやWindowsLookAndFeelのデフォルトはnull
- 下:
JTable#getScrollableTracksViewportWidth()メソッドをオーバーライドしてJTableの幅(すべての列幅の合計)よりJViewportの幅が大きくなる場合はscroll.getCorner(ScrollPaneConstants.UPPER_RIGHT_CORNER)で取得した右上コーナーコンポーネントを非表示に切り替えMetalLookAndFeelやWindowsLookAndFeelのデフォルト右上コーナーコンポーネントはnullなので常に非表示状態