• title: JScrollBarをJScrollPaneの左と上に配置 tags: [JScrollBar, JScrollPane, BorderLayout] author: aterai pubdate: 2007-01-08T14:26:45+09:00 description: JScrollBarの配置位置を、JScrollPaneの左側、上側に変更します。

概要

JScrollBarの配置位置を、JScrollPaneの左側、上側に変更します。

サンプルコード

scroll.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
JPanel panel = new JPanel(new BorderLayout());
int w = scroll.getVerticalScrollBar().getPreferredSize().width;
panel.add(Box.createHorizontalStrut(w), BorderLayout.WEST);
panel.add(scroll.getHorizontalScrollBar(), BorderLayout.CENTER);
add(panel,  BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
View in GitHub: Java, Kotlin

解説

  • 水平スクロールバーを右から左に
    • パネルのレイアウトを、BorderLayoutにして、JScrollPaneをそのパネルの中央(BorderLayout.CENTER)に追加し、JScrollPane#setComponentOrientationメソッドで、ComponentOrientation.RIGHT_TO_LEFTを設定しています。
  • 垂直スクロールバーを下から上に
    • JScrollPane#getHorizontalScrollBar()メソッドでスクロールバーを取得し、パネルレイアウトを使ってJScrollPaneの上部(BorderLayout.NORTH)に配置されているように見せかけています。
    • 左上隅の余白は、Box.createHorizontalStrut(縦スクロールバーの幅)です。

参考リンク

コメント