Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Java Programming > Java GUI สอน Java GUI เขียนโปรแกรม GUI ด้วย Java(Swing/AWT/JFC) > Java Formatted TextField (JFormattedTextField) - Swing Example



Clound SSD Virtual Server

Java Formatted TextField (JFormattedTextField) - Swing Example

Java Formatted TextField (JFormattedTextField) - Swing Example สำหรับ Formatted TextField หรือ JFormattedTextField (javax.swing.JFormattedTextField) จัดอยู่ในกลุ่มของ Component โดยวัตถุประสงค์คือใช้จัดรูปแบบ Format ของ Input ต่าง ๆ เช่น Input แบบ Date Format , Number Format , Telephone Format และอื่น ๆ

Java Formatted TextField (JFormattedTextField)

Java Formatted TextField (JFormattedTextField) - Swing Example


Syntax
MaskFormatter mask = new MaskFormatter("##########");
JFormattedTextField fmField = new JFormattedTextField(mask);

Controls Icon Tools

Java Formatted TextField (JFormattedTextField)

Example 1 การสร้าง Formatted TextField ด้วย JFormattedTextField แบบรันค่าตัวเลข Number เท่านั้น

MyForm.java
package com.java.myapp;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class MyForm extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyForm frame = new MyForm();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 362, 249);
		setTitle("ThaiCreate.Com Java GUI Tutorial");
		getContentPane().setLayout(null);
		
		// Format
    	MaskFormatter mask = null;
		try {
			mask = new MaskFormatter("##########");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Formatted TextField
		JFormattedTextField fmField = new JFormattedTextField(mask);
		fmField.setBounds(125, 87, 90, 20);
		getContentPane().add(fmField);

	}
}

Output

Java Formatted TextField (JFormattedTextField)

แสดง Input ข้อมูล

Java Formatted TextField (JFormattedTextField)

สามารถกรอกได้เฉพาะตัวเลข Number 0-9 เท่านั้น








Example 2 การสร้าง Formatted TextField ด้วย JFormattedTextField แบบ รูปแบบหมายเลขโทรศัพท์ (Telephone Number)

MyForm.java
package com.java.myapp;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class MyForm extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyForm frame = new MyForm();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 362, 249);
		setTitle("ThaiCreate.Com Java GUI Tutorial");
		getContentPane().setLayout(null);
		
		// Format
    	MaskFormatter mask = null;
		try {
			mask = new MaskFormatter("###-###-####");
			mask.setPlaceholderCharacter('_');
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Formatted TextField
		JFormattedTextField fmField = new JFormattedTextField(mask);
		fmField.setBounds(125, 87, 90, 20);
		getContentPane().add(fmField);

	}
}

Output

Java Formatted TextField (JFormattedTextField)

แสดง Format แบบหมายเลขโทรศัพท์

Java Formatted TextField (JFormattedTextField)

สามารถกรอกได้เฉพาะรูปแบบ Format ของหมายเลขโทรศัพท์


Example 3 การสร้าง Formatted TextField ด้วย JFormattedTextField แบบ รูปแบบรหัสประจำตัวประชาชน

MyForm.java
package com.java.myapp;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;

public class MyForm extends JFrame {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				MyForm frame = new MyForm();
				frame.setVisible(true);
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyForm() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 362, 249);
		setTitle("ThaiCreate.Com Java GUI Tutorial");
		getContentPane().setLayout(null);
		
		// Format
    	MaskFormatter mask = null;
		try {
			mask = new MaskFormatter("#-####-##-###-##-#");
			mask.setPlaceholderCharacter('_');
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Formatted TextField
		JFormattedTextField fmField = new JFormattedTextField(mask);
		fmField.setBounds(101, 83, 131, 20);
		getContentPane().add(fmField);

	}
}









Output

Java Formatted TextField (JFormattedTextField)

แสดงรูปแบบรหัสประจำตัวประชาชน

Java Formatted TextField (JFormattedTextField)

สามารถกรอก Format ที่เป็นบัตรประจำตัวประชาชนได้เท่านั้น

   
Share

Property & Method (Others Related)

Java GUI Swing Controls (Component)
Java Label (JLabel) - Swing Example
Java Button (JButton) - Swing Example
Java Toggle Button (JToggleButton) - Swing Example
Java Check Box (JCheckBox) - Swing Example
Java Radio Button (JRadioButton) - Swing Example
Java Button Group (ButtonGroup) - Swing Example
Java ComboBox (JComboBox) - Swing Example
Java List (JList) - Swing Example
Java Text Field (JTextField) - Swing Example
Java Text Area (JTextArea) - Swing Example
Java Scroll Bar (JScrollBar) - Swing Example
Java Slider (JSlider) - Swing Example
Java Progress Bar (JProgressBar) - Swing Example
Java Password Field (JPasswordField) - Swing Example
Java Spinner (JSpinner) - Swing Example
Java Separator (JSeparator) - Swing Example
Java Text Pane (JTextPane) - Swing Example
Java Editor Pane (JEditorPane) - Swing Example
Java Tree (JTree) - Swing Example
Java Table (JTable) - Swing Example

ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-09-03 22:05:07 / 2017-03-27 21:03:33
  Download : No files
 Sponsored Links / Related

 
Java GUI Swing Controls (Component)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่