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.

PlainDocument

Empfohlene Antworten

Veröffentlicht

Guten Morgen!

Also ich hab folgendes Problem ich muss ein Textfeld so sperren das der Benutzer nur Zahlen eingeben kann dazu hab ich im Internet folgendes gefunden...


import javax.swing.JTextField;


import javax.swing.text.DefaultStyledDocument;


import javax.swing.text.AttributeSet;


import javax.swing.JFrame;


import javax.swing.text.BadLocationException;


public class Main extends JFrame{


    public Main() {

      JTextField field = new JTextField();

      field.setColumns(1);

      field.setDocument(new Main.Customdoc(1));

      getContentPane().add(field);

      setSize(50,50);

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public class Customdoc extends DefaultStyledDocument{

        private String mask = "0123456789";

        private int maxLength = 0;

        public Customdoc(int max){

            this.maxLength = max;

        }



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

            if(str.length()+getLength() > maxLength)

                return;

            if(mask.indexOf(str)==-1)

                return;

            super.insertString(off,str,a);    

        }

    }





    public static void main(String[] args){

        Main m = new Main();

        m.setVisible(true);

    }

}

ich könnte das zwar selbst noch nicht schreiben aber verstehe eigentlich wie es funktioniert ist ja auch eigentlich nicht schwer... ich hab in meinem Programm 56 TextFelder die auch noch über Arrays erzeugt wurden und würde meine TextFelder nun mit diesem Dokument gern gegenüber Eingaben begrenzen (nur eine Zahl pro TextFeld + Zahl von 1 - 9) kann mir jemand vllt sagen wie ich des am besten mache, weil ich schon alles probiert habe :( also ich habs so gemacht das ich "public class Customdoc extends DefaultStyledDocument" und "public void insertString(int off, String str, AttributeSet a) throws BadLocationException" so übernommen hab wie es drin steht und meine Textfelder nun mit
tf[1].setColumns(1);

tf[1].setDocument(new Customdoc(1));

begrenzen will nur krieg ich das nicht wirklich hin :( und da ich es für die Arbeit brauche stehe ich auch unter Zeitdruck...

Ich bin dankbar für jeden tip :)

Ich habs ;)

import javax.swing.text.PlainDocument;

import javax.swing.text.BadLocationException;

import javax.swing.text.AttributeSet;


public class TextFeldBegrenzung extends PlainDocument

{

    private int limit;


    public TextFeldBegrenzung(int newLimit)

    {

        super();

        if (limit < 0)

        {

            limit = 0;

        }

        else

        {

            limit = newLimit;

        }

    }



    private String mask = "123456";

    private int maxLength = 1;


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

    {

        if(str.length()+getLength() > maxLength)

            return;

        if(mask.indexOf(str)==-1)

            return;

        super.insertString(off,str,a);    

    }

}

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.