From 9e223843ffceb3f98b560a3b4cc04f3611fb281e Mon Sep 17 00:00:00 2001 From: brigitte Date: Tue, 30 Apr 2024 19:33:40 +0200 Subject: [PATCH] =?UTF-8?q?limite=20de=20characteres=20de=201=20=C3=A0=209?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sudoku/JTextFieldCharLimit.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sudoku/JTextFieldCharLimit.java b/Sudoku/JTextFieldCharLimit.java index e938395..370db80 100644 --- a/Sudoku/JTextFieldCharLimit.java +++ b/Sudoku/JTextFieldCharLimit.java @@ -10,9 +10,15 @@ public class JTextFieldCharLimit extends PlainDocument this.max = max; } public void insertString(int offset, String text, AttributeSet attr) throws BadLocationException { - if (text == null) + if (text == null){ return; - if ((getLength() + text.length()) <= max) { + } + StringBuilder sb = new StringBuilder(); + sb.append(getText(0, getLength())); + sb.insert(offset, text); + + /* Vérifier si le texte ne contient que des chiffres de 1 à 9 et si il ne depasse pas 4 caractères */ + if (sb.length() <= max && sb.toString().matches("[1-9]*")) { super.insertString(offset, text, attr); } }