From aa78e6e3871a6fe926c125f3c33ecf5477f24f35 Mon Sep 17 00:00:00 2001 From: gallego Date: Tue, 30 Apr 2024 02:12:14 +0200 Subject: [PATCH] debut limit de caractere --- Sudoku/JTextFieldCharLimit.java | 18 ++++++++++++++++++ Sudoku/grille.java | 17 +++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 Sudoku/JTextFieldCharLimit.java diff --git a/Sudoku/JTextFieldCharLimit.java b/Sudoku/JTextFieldCharLimit.java new file mode 100644 index 0000000..fc35118 --- /dev/null +++ b/Sudoku/JTextFieldCharLimit.java @@ -0,0 +1,18 @@ +import javax.swing.text.PlainDocument; +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; + +public class JTextFieldCharLimit extends PlainDocument{ + private int limit; + + public JTextFieldCharLimit(int limit){ + this.limit = limit; + } + public void insertString(int offset, String str, AttributeSet set) throws BadLocationException{ + if (str == null){ + return; + } else if ((getLength() + str.length() - getLength()) <= limit){ + super.insertString(offset, str, set); + } + } +} \ No newline at end of file diff --git a/Sudoku/grille.java b/Sudoku/grille.java index a0e2a0d..0c8d884 100644 --- a/Sudoku/grille.java +++ b/Sudoku/grille.java @@ -4,7 +4,11 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*; import javax.swing.border.Border; -import javax.swing.text.*; +import javax.swing.text.PlainDocument; +import javax.swing.text.AttributeSet; +import javax.swing.text.BadLocationException; + + public class grille extends JComponent{ private static JLabel etat_exportation = new JLabel(); @@ -33,6 +37,13 @@ public class grille extends JComponent{ if(editable){ JTextField[][] case_editable = null; case_editable = new JTextField[9][9]; + for (int ligne = 0; ligne < 9; ligne++) { + for (int col = 0; col < 9; col++) { + case_editable[ligne][col] = new JTextField(); + case_editable[ligne][col].setDocument(new JTextFieldCharLimit(4)); + } + } + for (int ligne = 0; ligne < 9; ligne++) { for (int col = 0; col < 9; col++) { if (grille[ligne][col] == 0){ @@ -61,10 +72,12 @@ public class grille extends JComponent{ JLabel[][] case_depart = null; case_depart = new JLabel[9][9]; case_modifiable = new JTextField[9][9]; - + for (int ligne = 0; ligne < 9; ligne++) { for (int col = 0; col < 9; col++) { if ((grid_values[ligne][col]) == 0) { + + case_modifiable[ligne][col].setDocument(new JTextFieldCharLimit(4)); case_modifiable[ligne][col] = new JTextField("", 1); case_modifiable[ligne][col].setFont(new Font("Arial", Font.PLAIN, 30)); case_modifiable[ligne][col].setHorizontalAlignment(JTextField.CENTER);