debut limit de caractere

This commit is contained in:
gallego 2024-04-30 02:12:14 +02:00
parent 8724af4581
commit aa78e6e387
2 changed files with 33 additions and 2 deletions

View File

@ -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);
}
}
}

View File

@ -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){
@ -65,6 +76,8 @@ public class grille extends JComponent{
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);