JTextField クラス
- [内容]
- 1 行の文章を表示,編集するためのクラスです
- [コンストラクタ]
- public JTextField()
- public JTextField(Document doc, String text, int columns)
- public JTextField(int columns)
- public JTextField(String text)
- public JTextField(String text, int columns)
- JTextField オブジェクトを生成する
- [主なメソッド]
- public void addActionListener(ActionListener l)
- ActionListener を付け加える(改行キーが押されたときの処理)
- public int getCaretPosition() ( JTextComponent から継承)
- 挿入位置(カーソルの現在位置)を返す
- public int getColumns()
- 現在の列数を返す
- public Document getDocument() ( JTextComponent から継承)
- エディタに関連したモデルを取り出す
- public String getSelectedText() ( JTextComponent から継承)
- 選択されたテキストを返す
- public int getSelectionEnd() ( JTextComponent から継承)
- 選択されたテキストの終了位置(最後の文字の次)を返す
- public int getSelectionStart() ( JTextComponent から継承)
- 選択されたテキストの開始位置を返す
- public String getText(int offs, int len) throws BadLocationException ( JTextComponent から継承)
- public String getText() ( JTextComponent から継承)
- テキストの一部,または,すべての内容を返す
- public void paint(Graphics g) ( JComponent から継承)
- コンポーネントを描画
- public void paintAll(Graphics g) ( Component から継承)
- このコンポーネントおよびそのすべてのサブコンポーネントを描画
- public void paintComponent(Graphics g) ( JComponent から継承)
- コンポーネントを描画
- public void paintComponents(Graphics g) ( Container から継承)
- コンテナ内の各コンポーネントを描画
- public void removeActionListener ( ActionListener l )
- ActionListener を取り除く
- public void repaint() ( Component から継承)
- public void repaint(int x, int y, int width, int height) ( Component から継承)
- public void repaint(long tm) ( Component から継承)
- public void repaint(Rectangle r) ( JComponent から継承)
- public void repaint(long tm, int x, int y, int width, int height) ( JComponent から継承)
- 指定された矩形領域を,tm ミリ秒以内に再描画する
- public void replaceSelection(String content) ( JTextComponent から継承)
- 選択されている文字列を,指定された文字列で置き換える.
- public void select(int selectionStart, int selectionEnd) ( JTextComponent から継承)
- テキストの指定された領域を選択する( selectionEnd の位置は選択されない)
- public void selectAll() ( JTextComponent から継承)
- すべてのテキストを選択する
- public void setBackground(Color c) ( JComponent から継承)
- バックグラウンドカラーを設定する
- public void setCaretPosition(int position) ( JTextComponent から継承)
- カーソルの位置を設定する
- public void setColumns(int columns)
- 列数を設定する
- public void setEditable(boolean b) ( JTextComponent から継承)
- 編集可能か否かを設定する
- public void setEnabled(boolean b) ( JComponent から継承)
- コンポーネントを enable,または,disable にする.disable にするとイベントを受け付けなくなる.
- public void setFont(Font f)
- フォントを設定する
- public void setForeground(Color c) ( JComponent から継承)
- フォアグラウンドカラーを設定する
- public void setHorizontalAlignment(int alignment)
- テキストの水平配置を設定する.alignment は以下に示す値をとることができる.
- JTextField.LEFT
- JTextField.CENTER
- JTextField.RIGHT
- JTextField.LEADING
- JTextField.TRAILING
- public void setText(String t) ( JTextComponent から継承)
- 指定された文字列を設定する
- public void setVisible(boolean b) ( JComponent から継承)
- コンポーネントを表示,非表示にする
- [使用例]
- プログラム例は,JTextArea クラス,JTextField クラス,及び,JPasswordField クラスの各メソッド等の使用例です.上から 2 番目の JPasswordField では,エコー文字として '*' が設定されていますので,入力された文字がすべて * に替わっています.また,テキストエリアの文字列の初期設定は,「 Text Area 」です.それ以外の文字は,様々なメソッドによって生成されたものです.なお,JTextField,及び,JTextArea において JScrollPane を利用しています.

- プログラム例は,JTextArea クラス,及び,JTextField クラスに対するイベント処理の例です.上のテキストフィールドに文字を入力し改行キーを押すと,その内容がテキストエリアに表示されます( ActionListener の利用).また,テキストエリアの内容を変更すると,2 番目のテキストフィールドにメッセージが表示されます( DocumentListener の利用).

- [参照]
- TextField, JTextArea, JPasswordField, JScrollPane, ActionListener, ActionEvent, DocumentListener, DocumentEvent