Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

numerisch prüfen

Empfohlene Antworten

Veröffentlicht

Guten morgen,

ich bastel gerade ein bißchen an einer GUI herum und habe ein Textfeld, in das nur Zahlen eingegeben werden sollen. Gibt es eine Möglichkeit ein Textfeld auf eine numerische Eingabe hin zu überprüfen???

Gruß Trickydelphin

Hi,

einen DocumentListener auf das Document des Textfeldes registrieren und über changedUpdate oder insertUpdate Änderungen überprüfen.

Numerisch kannst du mit Double oder Integer.parseInt(String s) überprüfen, ist es keine Zahl, gibts ne Exception.

Gruß Jaraz

In der Klasse Character gibt's die Methode "isDigit(char ch)".

Da kannst du ganz bequem abfragen ob es ne Zahl ist oder nicht... ;)

Moin,

oder eine eigene Klasse von PlainDocument ableiten und die insertString- Methode überschreiben. Innerhalb der Methode dann wie Jaraz oder Peeter beschrieben haben, prüfen ob es ne Zahl ist.

Gruß, Peter

Danke für die guten Antworten!

Sowas z.b.:


/**

 * A simple document extension, that acceppts only numbers as entries

 */

class IntegerNumberDocument extends PlainDocument {

  public void insertString (int off, String s, AttributeSet a) throws BadLocationException {

    StringBuffer buffer = new StringBuffer();

    for(int i=0; i < s.length(); i++) {

      if(Character.isDigit(s.charAt(i))) {

        buffer.append(s.charAt(i));

      }

    }

    super.insertString(off, buffer.toString(), a);

  }

}


JTextField textField = new JTextField("testText");

textField.setDocument(new IntegerNumberDocument());

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.