import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
/******************************
* Created by Paul C........... *
* P.C. der Name ist Programm *
******************************/
public class TextEingrenzen extends BaseFrame implements ActionListener {
JFormattedTextField field;
JLabel result;
TextEingrenzen() throws ParseException{
JLabel header = new JLabel("Bitte bestätigen Sie Ihre Nummer!", SwingConstants.CENTER);
result = new JLabel("Ihr Eingabe:",SwingConstants.CENTER);
MaskFormatter mf = new MaskFormatter("#.###");
mf.setPlaceholderCharacter('#');
field = new JFormattedTextField(mf);
field.setFocusLostBehavior(JFormattedTextField.PERSIST);
field.addActionListener(this);
JButton button = new JButton("Eingabe");
button.addActionListener(this);
JPanel center = new JPanel(new GridLayout(1, 2));
center.add(field);
center.add(button);
JPanel content = new JPanel(new GridLayout(3, 1));
content.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
content.add(header);
content.add(center);
content.add(result);
this.add(content);
this.pack();
}
public void actionPerformed(ActionEvent e) {
boolean isValid = field.isEditValid();
if(isValid) {
try {
field.commitEdit();
} catch (ParseException ignored) {
isValid = false;
}
}
if(isValid) {
result.setText(String.format("Sie haben %s eingegeben:", field.getText()));
} else {
result.setText("Bitte geben Sie eine Nummer ein!");
}
}
public static void main(String[] args) throws ParseException {
new TextEingrenzen();
}
}