- Home ›
- Swing ›
- テキストエリアの作成(JTextAreaクラス) ›
- HERE
入力された値の取得
テキストエリアに入力された値を取得するには、JTextAreaクラスの親クラスであるJTextComponentで用意されているgetTextメソッドを使います。
getText public String getText()
この TextComponent に格納されたテキストを返します。基本となるドキュメントが null の場合は、 NullPointerException を返します。 テキストはバウンドプロパティーではないため、それが変更さ れても PropertyChangeEvent はトリガーされません。テキストの変更を待機するには、 DocumentListener を使用してください。 戻り値: テキスト パラメータ: position - 位置 例外: NullPointerException - ドキュメントが null の場合
メソッドを実行するとテキストエリアに入力されているテキストをStringクラスのオブジェクトとして取得します。
実際の使い方は次のようになります。
JTextArea area = new JTextArea("こんにちは");
String str = area.getText();
指定した位置の値を取得する
テキストエリアに入力された全ての値ではなく、指定した位置から指定しただけの文字を取得することもできます。先ほどと同名のメソッドですが引数が異なるgetTextメソッドを使います。
getText public String getText(int offs, int len) throws BadLocationException
コンポーネントが表すテキストの一部を取り出します。長さが 0 の場合は、空の文字列を返します。 パラメータ: offs - オフセット >= 0 len - 長さ >= 0 戻り値: テキスト 例外: BadLocationException - オフセットまたは長さが無効な場合
引数には取得する文字列の開始位置と取得する文字の長さを指定します。指定は文字数で指定し先頭は0から開始されます。戻り値としてテキストエリアに入力された文字列の中で指定した位置から指定した長さだけのString型の値として取得できます。入力された値の中に改行文字が含まれている場合は改行文字も1文字として数えます。
このメソッドを使用する場合には例外として「javax.swing.text.BadLocationException」が発生する可能性がありますので、その処理を記述して下さい。
実際の使い方は次のようになります。
JTextArea area = new JTextArea("初期値");
try{
String str = area.getText(0, 5);
}catch(BadLocationException e){
System.out.println("Bad Location Error!");
}
サンプルプログラム
では実際に試してみます。
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import java.awt.Container;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.text.BadLocationException;
class SSample11_1 extends JFrame{
JTextArea area;
JTextArea disparea;
public static void main(String args[]){
SSample11_1 frame = new SSample11_1("タイトル");
frame.setVisible(true);
}
SSample11_1(String title){
setTitle(title);
setBounds(100, 100, 300, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p = new JPanel();
area = new JTextArea();
area.setLineWrap(true);
JScrollPane scrollpane1 = new JScrollPane(area);
scrollpane1.setPreferredSize(new Dimension(120, 120));
disparea = new JTextArea();
disparea.setLineWrap(true);
disparea.setEditable(false);
disparea.setBackground(Color.LIGHT_GRAY);
JScrollPane scrollpane2 = new JScrollPane(disparea);
scrollpane2.setPreferredSize(new Dimension(120, 120));
JButton button1 = new JButton("全て取得");
button1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
disparea.setText(area.getText());
}
}
);
JButton button2 = new JButton("20文字取得");
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
try{
disparea.setText(area.getText(0, 20));
}catch(BadLocationException e){
System.out.println("Bad Location Error!");
}
}
}
);
p.add(scrollpane1);
p.add(scrollpane2);
p.add(button1);
p.add(button2);
Container contentPane = getContentPane();
contentPane.add(p, BorderLayout.CENTER);
}
}
ではコンパイルを行った上で実行してみます。
左側のテキストエリアに文字列を入力し「全て取得」ボタンを押すと、入力された値を全て取得して右側のテキストエリアに表示します。
「20文字取得」ボタンを押すと、入力された値の先頭から20文字分だけ取得して右側のテキストエリアに表示します。
( Written by Tatsuo Ikura )
JavaDrive