• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JScrollBarをJScrollPaneの左と上に配置
#navi(../)
RIGHT:Posted by [[aterai]] at 2007-01-08
*JScrollBarをJScrollPaneの左と上に配置 [#l79e30ba]
JScrollBarの配置位置を、JScrollPaneの左側、上側に変更します。
---
category: swing
folder: LeftScrollBar
title: JScrollBarをJScrollPaneの左と上に配置
tags: [JScrollBar, JScrollPane, BorderLayout]
author: aterai
pubdate: 2007-01-08T14:26:45+09:00
description: JScrollBarの配置位置を、JScrollPaneの左側、上側に変更します。
image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTPG13yZbI/AAAAAAAAAdc/1a4aTgyblRo/s800/LeftScrollBar.png
---
* 概要 [#summary]
`JScrollBar`の配置位置を、`JScrollPane`の左側、上側に変更します。

-&jnlp;
-&jar;
-&zip;
#download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTPG13yZbI/AAAAAAAAAdc/1a4aTgyblRo/s800/LeftScrollBar.png)

//#screenshot
#ref(http://lh6.ggpht.com/_9Z4BYR88imo/TQTPG13yZbI/AAAAAAAAAdc/1a4aTgyblRo/s800/LeftScrollBar.png)

**サンプルコード [#c9e13a36]
#code{{
* サンプルコード [#sourcecode]
#code(link){{
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(panel, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
}}

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

- 垂直スクロールバーを下から上に
-- JScrollPane#getHorizontalScrollBar()メソッドでスクロールバーを取得し、パネルレイアウトを使ってJScrollPaneの上部(BorderLayout.NORTH)に配置されているように見せかけています。
--左上隅の余白は、Box.createHorizontalStrut(縦スクロールバーの幅)です。
* 参考リンク [#reference]
- [https://community.oracle.com/thread/1375964 Swing - JScrollPane with scroll bar on the left]
- [http://sozai-free.com/ 2000ピクセル以上のフリー写真素材集]
- [[JScrollBarのButtonの位置を変更>Swing/ScrollBarButtonLayout]]

**参考リンク [#u3788d1b]
-[http://forums.sun.com/thread.jspa?threadID=600107 Swing - JScrollPane with scroll bar on the left]
-[http://sozai-free.com/ 2000ピクセル以上のフリー写真素材集]
* コメント [#comment]
#comment
- グッド -- &user(a1); &new{2008-12-26 (金) 13:52:59};
-- どうもです。 -- [[aterai]]

**コメント [#ybd1b696]
- グッド -- [[a1]] &new{2008-12-26 (金) 13:52:59};

#comment