ورود

View Full Version : سوال: شخصي سازي MaskFormatter و اشكال در همزمان سازي آپديت اطلاعات در JFormattedTextField



kobari
یک شنبه 03 بهمن 1389, 02:20 صبح
سلام
براي تبديل اطلاعات در فيلد JFormattedTextField ،‌از يك MaskFormatter‌ استفاده كردم كه در متد stringToValue آن عمل تبديل انجام مي گيرد. ولي همزمان اطلاعات در JFormattedTextField بروز نمي شه و فقط بعداز خروج و ورود مجدد ماوس در فيلد ،‌اين اطلاعات بروز مي شود. براي اينكه اين همزماني اتفاق بيفته چكار بايد كرد؟




import javax.swing.*;
import javax.swing.text.MaskFormatter;
public class JFormattedTF extends JFrame {
JFormattedTextField textField ;
public JFormattedTF() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Text :");
try {


MyMaskFormatter formatter = new MyMaskFormatter("********************************");
textField = new JFormattedTextField(formatter);
textField.setColumns(40);
panel.add(label);


panel.add(textField);
getContentPane().add(panel, java.awt.BorderLayout.NORTH);
pack();
} catch(Exception e) {e.getMessage();}
}


public static void main(String[] args) {
JFormattedTF tfe = new JFormattedTF();
tfe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
tfe.setVisible(true);
}


public class MyMaskFormatter extends MaskFormatter {


public MyMaskFormatter(String mask) throws java.text.ParseException{
super(mask);
setCommitsOnValidEdit(true);
}


@Override public Object stringToValue(String value) throws java.text.ParseException {
String str = value;
if(str.indexOf("," ) >= 0)
str = str.replaceAll(",", ";");


return super.stringToValue(str);


}



}


}