タイトル
TOPJavaSwingJFormattedTextField → This Page

JFormattedTextField@Swing サンプル05

概要

Java - Swing - JFormattedTextField のサンプルです。
・その他[MaskFormatter]

解説

入力した文字列を様々なフォーマットに整形して表示します。

パターン

フォーマットには以下のパターンを組み合わせて設定します。
パターン説明
# 数値
? 文字
A 数値または文字
* 全て
U 大文字にマップされた小文字を持つ文字
L 小文字にマップされた大文字を持つ文字
H 16進数字(A-F、a-f、0-9)
' 別のマスク文字のエスケープに使う

サンプルイメージ

サンプル画像


サンプルソース

import java.awt.Dimension;
import java.awt.Font;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;

/**
 * JFormattedTextField サンプル05
 * ・その他[MaskFormatter]
 * 
 * @author みっちー
 */
public class JFormattedTextField05 extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * 開始メソッド
	 * 
	 * @param args	パラメータ
	 * @exception ParseException
	 */
	public static void main(String[] args) throws ParseException {
		JFormattedTextField05 frame = new JFormattedTextField05();
		
		// 閉じるボタンをクリックされた場合の動作を設定
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// ウインドウのタイトルを設定
		frame.setTitle("JFormattedTextField サンプル05");
		
		// フレームの X座標、Y座標、幅、高さを設定
		frame.setBounds(100, 200, 250, 140);
		
		// フレームを表示(これをしないと透明のフレームが立ち上がってしまう)
		frame.setVisible(true);
	}
	
	/**
	 * コンストラクタ
	 * 
	 * @exception ParseException
	 */
	public JFormattedTextField05() throws ParseException {
		// パネルを作成
		JPanel panelBase = new JPanel();
		
		// フォーマットを設定
		// 面倒なので ParseException はスルーしてます
		MaskFormatter mf1 = new MaskFormatter("(####)-###-####");
		mf1.setPlaceholderCharacter('_');
		MaskFormatter mf2 = new MaskFormatter("〒###-####");
		mf2.setPlaceholderCharacter('_');
		MaskFormatter mf3 = new MaskFormatter("####/##/##");
		mf3.setPlaceholderCharacter('_');
		MaskFormatter mf4 = new MaskFormatter("AAAAA@AAAAA.AAAAA");
		mf4.setPlaceholderCharacter('_');
		
		// テキストを作成
		JFormattedTextField text1 = new JFormattedTextField(mf1);
		JFormattedTextField text2 = new JFormattedTextField(mf2);
		JFormattedTextField text3 = new JFormattedTextField(mf3);
		JFormattedTextField text4 = new JFormattedTextField(mf4);
		
		// 初期値を設定
		// setText() だと初期表示時にフォーマットが評価されないので setValue() を使う
		text1.setValue("");
		text2.setValue("");
		text3.setValue("");
		text4.setValue("");
		
		// サイズを設定
		text1.setPreferredSize(new Dimension(220, 20));
		text2.setPreferredSize(new Dimension(220, 20));
		text3.setPreferredSize(new Dimension(220, 20));
		text4.setPreferredSize(new Dimension(220, 20));
		
		// フォントの設定
		text1.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text2.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text3.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		text4.setFont(new Font("MS ゴシック", Font.PLAIN, 16));
		
		// テキストを追加
		panelBase.add(text1);
		panelBase.add(text2);
		panelBase.add(text3);
		panelBase.add(text4);
		
		// パネルを追加
		getContentPane().add(panelBase);
	}
}

サンプルソースのダウンロード

ソースのダウンロード(Eclipse用のプロジェクトファイルも同梱)

更新履歴

2016/05/13 Windows 8.1 + Java 7 環境で全体を見直し
2007/12/27 新規作成


TOPJavaSwingJFormattedTextField → This Page