powered by simpleCommunicator - 2.0.61     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / Java [игнор отключен] [закрыт для гостей] / Маска - MaskFormatter - JFormattedTextField
2 сообщений из 2, страница 1 из 1
Маска - MaskFormatter - JFormattedTextField
    #39024611
SmallTux
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Привет, всем!
Кто знает, как сделать так, чтобы если пользователь вводит не до конца маску, то она сохранялась в JFormattedTextField?
...
Рейтинг: 0 / 0
Маска - MaskFormatter - JFormattedTextField
    #39024684
SmallTux
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
SmallTuxКто знает, как сделать так, чтобы если пользователь вводит не до конца маску, то она сохранялась в JFormattedTextField?

Уже сделал все сам. Фокус + таймер = чудо!

Код: java
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.event.*;
import java.text.ParseException;

public class Test extends JFrame{
    MaskFormatter f1;
    JFormattedTextField t1, t2;
    String str1,str2;
    String strR1 = "",strR2 = "";
    Reader r = new Reader();
    Timer time1 = new Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            str1 = t1.getText();
        }
    });
    Timer time2 = new Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            str2 = t2.getText();
        }
    });
    public Diskriminant(String s){
        super(s);
        try {
            f1 = new MaskFormatter("*#####");
            f1.setValidCharacters("-1234567890");
            t1 = new JFormattedTextField(f1);
            t1.setColumns(5); t1.addFocusListener(new CustomListener1());
            t2 = new JFormattedTextField(f1);
            t2.setColumns(5); t2.addFocusListener(new CustomListener2());
        } catch (ParseException e) {}
        add(t1);add(t2);
    }
    public class Reader implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            try{
                //События...
            }catch(Exception ex){};
        }
    }
    public class CustomListener1 implements FocusListener {
        public void focusGained(FocusEvent e) {
            time1.start();
        }
        public void focusLost(FocusEvent e) {
            time1.stop();
            strR1 = "";
            char[] chstr1 = new char[str1.length()];
            str1.getChars(0, str1.length(), chstr1, 0);
            for (int i = 0; i < str1.length(); i++)
                if (chstr1[i] != ' ')
                    strR1+=chstr1[i];
            t1.setValue(strR1);
        }
    }
    public class CustomListener2 implements FocusListener {
        public void focusGained(FocusEvent e) {
            time2.start();
        }
        public void focusLost(FocusEvent e) {
            time2.stop();
            strR2 = "";
            char[] chstr2 = new char[str2.length()];
            str2.getChars(0, str2.length(), chstr2, 0);
            for (int i = 0; i < str2.length(); i++)
                if (chstr2[i] != ' ')
                    strR2+=chstr2[i];
            t2.setValue(strR2);
        }
    }
}
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / Java [игнор отключен] [закрыт для гостей] / Маска - MaskFormatter - JFormattedTextField
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]