Nachdem man mich ein wenig über das JFormattedTextField aufgeklärt hat und ich mich damit beschäftigt habe, stehe ich jetzt vor dem Problem, wie ich ein Feld erstelle in welches nur Zahlen eingegeben werden dürfen. Die Variante mit dem MaskFormatter (siehe unten) schreibt ja automatisch auch vor, dass genau so viele Nummern eingegeben werden müssen, wie im Formatter stehen. Beim zweiten Eingabefeld unten (Gewicht) kann aber ja die Anzahl der Nummern variieren. Dort habe ich jetzt das NumberFormat im Einsatz. Was ich aber eigentlich möchte ist, dass bei einer Fehleingabe des Benutzers das Feld gar nicht erst verlassen werden kann. Ich hatte dort wieder auf den PropertyChangeListener zurückgegriffen, weiß aber nicht ob der Lösungsweg gut ist oder wie ich das lösen kann.
Code:
import javax.swing.SwingUtilities;
import java.awt.GridBagConstraints;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
public class TextFieldTest extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JFormattedTextField nummer;
private JFormattedTextField gewicht;
private JLabel gewichtLabel = null;
private JLabel nummerLabel = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TextFieldTest thisClass = new TextFieldTest();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public TextFieldTest() {
super();
initialize();
}
private void initialize() {
this.setSize(300, 200);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 1;
gridBagConstraints2.gridy = 0;
nummerLabel = new JLabel();
nummerLabel.setText("Nummer");
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.gridx = 1;
gridBagConstraints11.gridy = 1;
gewichtLabel = new JLabel();
gewichtLabel.setText("Gewicht");
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.fill = GridBagConstraints.BOTH;
gridBagConstraints1.gridy = 1;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.gridx = 2;
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.fill = GridBagConstraints.BOTH;
gridBagConstraints.gridy = 0;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.gridx = 2;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.add(nummerTextField(), gridBagConstraints);
jContentPane.add(gewichtTextField(), gridBagConstraints1);
jContentPane.add(gewichtLabel, gridBagConstraints11);
jContentPane.add(nummerLabel, gridBagConstraints2);
}
return jContentPane;
}
private JFormattedTextField nummerTextField() {
try {
nummer = new JFormattedTextField(new MaskFormatter("#####"));
nummer.setValue(null);
nummer.addPropertyChangeListener(new Nummer());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return nummer;
}
private JFormattedTextField gewichtTextField() {
NumberFormat nummerneingabe=NumberFormat.getNumberInstance();
nummerneingabe.setGroupingUsed(false);
nummerneingabe.setMinimumIntegerDigits(1);
nummerneingabe.setMaximumIntegerDigits(5);
gewicht = new JFormattedTextField(nummerneingabe);
gewicht.setValue(null);
gewicht.addPropertyChangeListener(new Gewicht());
return gewicht;
}
class Gewicht implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
System.out.println("Value: "+gewicht.getValue());
System.out.println("Text: "+gewicht.getText());
if(gewicht.getValue()!=null)
{
System.out.println("ein wert wurde eingegeben");
//Prüfung, ob Eingabe i.O., sonst zurück ins Feld springen.
}
else
{
//Mit requestFocus komme ich nicht mehr ohne Eingabe aus dem Feld raus.
//gewicht.requestFocus();
//gewicht.selectAll();
}
}
}
class Nummer implements PropertyChangeListener
{
public void propertyChange(PropertyChangeEvent e)
{
System.out.println("Value: "+nummer.getValue());
System.out.println("Text: "+nummer.getText());
if(nummer.getValue()!=null)
{
System.out.println("Ein Wert wurde eingegeben");
}
}
}
}