- Home ›
 - Swing ›
 - JSplitPaneクラス ›
 - HERE
 
仕切線(ディバイダ)の現在の値を取得する
広告
仕切線が配置されている位置を取得する方法を確認します。JSplitPaneクラスで用意されている「getDividerLocation」メソッドを使います。
getDividerLocation public int getDividerLocation()
setDividerLocation に渡された最終値を返します。このメソッドで返された値 は、setDividerLocation に渡された値が現在のサイズより大きい場合は、実際 のディバイダの位置と異なる場合があります。 戻り値: ディバイダの位置を指定する int 値
戻り値としてメソッドが実行された時の仕切線の位置をint型の値として取得できます。ここで取得できる値は仕切線の左側とスプリットペインの左端からの距離となります。
実際の使い方は次のようになります。
JSplitPane splitpane = new JSplitPane(); int pos = splitpane.getDividerLocation();
サンプルプログラム
では簡単なサンプルを作成して試してみます。
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class JSplitPaneTest8 extends JFrame implements ActionListener{
  JSplitPane splitpane;
  JLabel posLabel;
  public static void main(String[] args){
    JSplitPaneTest8 frame = new JSplitPaneTest8();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(10, 10, 300, 200);
    frame.setTitle("タイトル");
    frame.setVisible(true);
  }
  JSplitPaneTest8(){
    splitpane = new JSplitPane();
    JPanel leftPanel = new JPanel();
    JButton leftButton = new JButton("Left");
    leftPanel.add(leftButton);
    JPanel rightPanel = new JPanel();
    JButton rightButton = new JButton("Right");
    rightPanel.add(rightButton);
    splitpane.setLeftComponent(leftPanel);
    splitpane.setRightComponent(rightPanel);
    posLabel = new JLabel("pos:");
    JButton button = new JButton("get");
    button.addActionListener(this);
    JPanel labelPanel = new JPanel();
    labelPanel.add(posLabel);
    labelPanel.add(button);
    getContentPane().add(splitpane, BorderLayout.CENTER);
    getContentPane().add(labelPanel, BorderLayout.PAGE_END);
  }
  public void actionPerformed(ActionEvent e){
    int pos = splitpane.getDividerLocation();
    posLabel.setText("pos:" + pos);
  }
}
			上記をコンパイルした後で実行すると次のように表示されます。
			
			
仕切線を適当な位置に移動させてボタンをクリックして下さい。
			
			
仕切線の位置を取得して表示します。
( Written by Tatsuo Ikura )
				
JavaDrive