本文整理汇总了Java中javax.swing.text.MaskFormatter类的典型用法代码示例。如果您正苦于以下问题:Java MaskFormatter类的具体用法?Java MaskFormatter怎么用?Java MaskFormatter使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MaskFormatter类属于javax.swing.text包,在下文中一共展示了MaskFormatter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFormat
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* Returns format's pattern.
*
* @return format's pattern.
*/
public String getFormat() {
if (format != null) return format;
String fmt = null;
if (formatter instanceof MaskFormatter) {
fmt = ((MaskFormatter)formatter).getMask();
} else if (formatter instanceof InternationalFormatter) {
Format f = ((InternationalFormatter)formatter).getFormat();
if (f instanceof DecimalFormat) {
fmt = ((DecimalFormat)f).toPattern();
} else if (f instanceof SimpleDateFormat) {
fmt = ((SimpleDateFormat)f).toPattern();
}
}
return fmt;
}
示例2: DesktopTimeField
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public DesktopTimeField() {
UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);
digitWidth = getDigitWidth();
timeFormat = Datatypes.getFormatStringsNN(uss.getLocale()).getTimeFormat();
resolution = DateField.Resolution.MIN;
formatter = new MaskFormatter();
formatter.setPlaceholderCharacter('_');
impl = new FlushableFormattedTextField(formatter);
FieldListener listener = new FieldListener();
impl.addFocusListener(listener);
impl.addKeyListener(listener);
setShowSeconds(timeFormat.contains("ss"));
}
示例3: parseString
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public static Object parseString(String value, Object[] pattern) {
MaskFormatter format = new MaskFormatter();
format.setValueContainsLiteralCharacters(false);
Object d = null;
for (int i = 0; i < pattern.length; i++) {
try {
format.setMask(pattern[i].toString());
d = format.stringToValue(value);
if (d != null)
break;
} catch (Exception e) {
continue;
}
} // End of for loop
return d;
}
示例4: validateUsPhoneNumber
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* Instead of using the MaskConverter in the Page, we just validate the value here with a custom validator.
*/
public String validateUsPhoneNumber(final String newValue) {
try {
final MaskFormatter maskFormatter = new MaskFormatter("(###) ###-####");
maskFormatter.setValueClass(String.class);
maskFormatter.setAllowsInvalid(true);
maskFormatter.setValueContainsLiteralCharacters(true);
final Object obj = maskFormatter.stringToValue(newValue);
if (obj != null) {
return null;
}
} catch (final ParseException e) {
//ignore
}
//so we can provide a different message than "is not a valid string"
return "does not match the mask";
}
示例5: AddIPDialog
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public AddIPDialog(JDependCooper frame) throws ParseException {
super();
this.frame = frame;
setSize(250, 100);
this.setLocationRelativeTo(null);// 窗口在屏幕中间显示
this.setLayout(new BorderLayout());
MaskFormatter ipmask = new MaskFormatter("###.###.###.###");
ipmask.setPlaceholderCharacter(' ');
field = new JFormattedTextField(ipmask);
field.setFont(new Font("Courier", Font.PLAIN, 18));
field.setColumns(16);
this.add(BorderLayout.CENTER, field);
JPanel buttonPanel = new JPanel();
buttonPanel.add(this.createOkButton());
buttonPanel.add(this.createCloseButton());
this.add(BorderLayout.SOUTH, buttonPanel);
}
示例6: createGridFields
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
private void createGridFields() {
mGrid = new JFormattedTextField[9][];
for(int i = 0; i < 9; i++) {
mGrid[i] = new JFormattedTextField[9];
for(int j = 0; j < 9; j++) {
try {
mGrid[i][j] = new JFormattedTextField(new MaskFormatter("#"));
}
catch (ParseException ex) {
System.err.println("ParseException while creating grid.");
System.exit(1);
}
mGrid[i][j].setHorizontalAlignment(JFormattedTextField.CENTER);
mGrid[i][j].setForeground(Color.BLACK);
mGrid[i][j].addKeyListener(getGridKeyAdapter(i, j));
mGrid[i][j].addMouseListener(getGridMouseAdapter(mGrid[i][j]));
}
}
}
示例7: Formatacoes
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/** Inicializa os campos */
protected Formatacoes()
{
try {
cpf = new MaskFormatter("###.###.###-##");
cpf.setPlaceholderCharacter('_');
rg = new MaskFormatter("#########");
rg.setPlaceholderCharacter('_');
telefone = new MaskFormatter("####-####");
telefone.setPlaceholderCharacter('_');
telefone2 = new MaskFormatter("####-####");
telefone2.setPlaceholderCharacter('_');
data = new MaskFormatter("##/##/####");
data.setPlaceholderCharacter('_');
cep = new MaskFormatter("#####-###");
cep.setPlaceholderCharacter('_');
tipo = new MaskFormatter("U");
tipo.setPlaceholder(" ");
tipo.setValidCharacters("CFA");
} catch (ParseException e) {
JOptionPane.showMessageDialog(null,"ERRO: N�o foi poss�vel criar as m�scaras dos campos da janela.");
}
}
示例8: ScheduleDurationField
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* @param value the duration to use for the initial value, may not be null
*/
public ScheduleDurationField(final Duration value) {
setInputVerifier(new TimeVerifier());
try {
final MaskFormatter mf = new MaskFormatter(MASKFORMAT);
mf.setPlaceholderCharacter('_');
mf.setValueClass(String.class);
final DefaultFormatterFactory dff = new DefaultFormatterFactory(mf);
setFormatterFactory(dff);
} catch (final ParseException pe) {
throw new FLLInternalException("Invalid format for MaskFormatter", pe);
}
setDuration(value);
}
示例9: createMaskFormatter
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* Creates a {@link MaskFormatter} from a given pattern.
*
* @param format for the formatter
* @return created {@link MaskFormatter}
* @throws ParseException
* @see MaskFormatter
* @since 0.0.1
*/
public static MaskFormatter createMaskFormatter(final String format) throws ParseException {
if (log.isDebugEnabled()) log.debug(HelperLog.methodStart(format));
if (null == format) {
throw new RuntimeExceptionIsNull("format"); //$NON-NLS-1$
}
final MaskFormatter result = new MaskFormatter(format) {
private static final long serialVersionUID = 5212947957420446007L;
{
setCommitsOnValidEdit(true);
setPlaceholderCharacter('_');
}
};
if (log.isDebugEnabled()) log.debug(HelperLog.methodExit(result));
return result;
}
示例10: init
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
private void init() {
//create the CellContraints
cc = new CellConstraints();
// create the Layout for Panel this
String panelColumns = "pref,3dlu,fill:pref:grow";
String panelRows = "pref";
FormLayout panelLayout = new FormLayout(panelColumns, panelRows);
this.setLayout(panelLayout);
this.marker = new JLabel(label+":");
this.valueField = new JFormattedTextField();
if (this.valueField.getBackground().equals(Color.BLACK)) {
this.valueField.setBackground(Color.WHITE);
}
try {
mf = new MaskFormatter ("##");
} catch (java.text.ParseException e) {
}
javax.swing.text.DefaultFormatterFactory dff = new DefaultFormatterFactory(mf);
valueField.setFormatterFactory(dff);
valueField.addFocusListener(listener);
this.add(marker, cc.xy(1, 1));
this.add(valueField, cc.xy(3,1));
}
示例11: formatarString
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public static String formatarString(String texto, String mascara) throws ParseException {
MaskFormatter mf = new MaskFormatter(mascara);
mf.setValueContainsLiteralCharacters(false);
return mf.valueToString(texto);
}
示例12: getHexFormatter
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public static MaskFormatter getHexFormatter(int length) {
try {
return new MaskFormatter(Strings.repeat("H", length));
} catch (ParseException e) {
log.error("Error creating MaskFormatter", e);
return null;
}
}
示例13: ScheduleTimeField
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* @param value the initial value for the widget
*/
public ScheduleTimeField(final LocalTime value) {
try {
final MaskFormatter mf = new MaskFormatter(MASKFORMAT);
mf.setPlaceholderCharacter('_');
mf.setValueClass(String.class);
final DefaultFormatterFactory dff = new DefaultFormatterFactory(mf);
setFormatterFactory(dff);
} catch (final ParseException pe) {
throw new FLLInternalException("Invalid format for MaskFormatter", pe);
}
setTime(value);
}
示例14: initialize
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
/**
* Grafiksel arayuz bilesenlerini olusturup pencereye ekleme isini yapan
* metot
*/
private void initialize() {
this.setSize(395, 110);
this.setTitle("Bir İşlem");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
// Sayilar arayuze ekleniyor
this.getContentPane().add(
createLabel("SAYILAR:", new Rectangle(0, 0, 100, 25)));
try {
// Sadece iki basamak kabul eden bicimlendirici
MaskFormatter ikiBasamak = new MaskFormatter("##");
// Sadece uc basamak kabul eden bicimlendirici
MaskFormatter ucBasamak = new MaskFormatter("###");
// Metin alanlari bicimli metin alani olarak yaratiliyor
// veri giris hatalarini onlemeye calisiyoruz
txtSayi1 = new JFormattedTextField(ikiBasamak);
txtSayi1.setBounds(new Rectangle(100, 0, 30, 25));
this.getContentPane().add(txtSayi1);
txtSayi2 = new JFormattedTextField(ikiBasamak);
txtSayi2.setBounds(new Rectangle(130, 0, 30, 25));
this.getContentPane().add(txtSayi2);
txtSayi3 = new JFormattedTextField(ikiBasamak);
txtSayi3.setBounds(new Rectangle(160, 0, 30, 25));
this.getContentPane().add(txtSayi3);
txtSayi4 = new JFormattedTextField(ikiBasamak);
txtSayi4.setBounds(new Rectangle(190, 0, 30, 25));
this.getContentPane().add(txtSayi4);
txtSayi5 = new JFormattedTextField(ikiBasamak);
txtSayi5.setBounds(new Rectangle(220, 0, 30, 25));
this.getContentPane().add(txtSayi5);
txtSayi6 = new JFormattedTextField(ucBasamak);
txtSayi6.setBounds(new Rectangle(250, 0, 30, 25));
this.getContentPane().add(txtSayi6);
// Hedef Sayi arayuze ekleniyor
this.getContentPane().add(
createLabel("HEDEF SAYI:", new Rectangle(0, 25, 100, 25)));
txtHedefSayi = new JFormattedTextField(ucBasamak);
txtHedefSayi.setBounds(100, 25, 30, 25);
this.getContentPane().add(txtHedefSayi);
// Dugmeler ekleniyor
this.getContentPane().add(getBtnRandomSayi());
this.getContentPane().add(getBtnRandomHedefSayi());
this.getContentPane().add(getBtnHesapla());
// Sonuc ekleniyor
lblCozum = new JLabel();
lblCozum.setBounds(0, 50, 300, 25);
this.getContentPane().add(lblCozum);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例15: increaseId
import javax.swing.text.MaskFormatter; //导入依赖的package包/类
public static String increaseId(String id, String mask) throws ParseException {
LOGGER.info("#### Tools.increaseId id = " + id + " , mask = " + mask);
MaskFormatter formatter = new MaskFormatter(mask);
formatter.setValueContainsLiteralCharacters(false);
String value = formatter.stringToValue(id).toString();
StringBuilder newValue = new StringBuilder();
boolean increase = true;
for (int i = value.length() - 1; i >= 0; i--) {
char c = value.charAt(i);
switch (c) {
case '9':
newValue.append((increase) ? '0' : '9');
break;
case '8':
newValue.append((increase) ? '9' : '8');
increase = false;
break;
case '7':
newValue.append((increase) ? '8' : '7');
increase = false;
break;
case '6':
newValue.append((increase) ? '7' : '6');
increase = false;
break;
case '5':
newValue.append((increase) ? '6' : '5');
increase = false;
break;
case '4':
newValue.append((increase) ? '5' : '4');
increase = false;
break;
case '3':
newValue.append((increase) ? '4' : '3');
increase = false;
break;
case '2':
newValue.append((increase) ? '3' : '2');
increase = false;
break;
case '1':
newValue.append((increase) ? '2' : '1');
increase = false;
break;
case '0':
newValue.append((increase) ? '1' : '0');
increase = false;
break;
default:
newValue.append(c);
break;
}
}
return formatter.valueToString(newValue.reverse().toString());
}