src/de/uapcore/sudoku/SudokuTextField.java

changeset 1
f1d7de36b01e
child 2
5179eff8a9b6
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/uapcore/sudoku/SudokuTextField.java	Sat Jan 26 15:48:59 2013 +0100
@@ -0,0 +1,71 @@
+package de.uapcore.sudoku;
+
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import javax.swing.JTextField;
+
+/**
+ *
+ * @author mike
+ */
+public class SudokuTextField extends JTextField {
+    
+    public SudokuTextField() {
+        setBorder(null);
+        setBackground(Color.WHITE);
+        
+        setFont(new Font("Dialog", Font.PLAIN, 18));
+        setHorizontalAlignment(JTextField.CENTER);
+        
+        Dimension dim = new Dimension(40,40);
+        setPreferredSize(dim);
+        setMinimumSize(dim);
+        setMaximumSize(dim);
+        
+        addKeyListener(new KeyAdapter() {
+            private void handle(KeyEvent e) {
+                if (getText().length() > 0) {
+                    int c = e.getKeyCode();
+                    if (c != KeyEvent.VK_DELETE &&
+                            c != KeyEvent.VK_BACK_SPACE &&
+                            !e.isActionKey()) {
+                        e.consume();
+                    }
+                } else {
+                    char c = e.getKeyChar();
+                    if (c < '0' || c > '9') {
+                        e.consume();
+                    }
+                }
+            }
+            
+            @Override
+            public void keyPressed(KeyEvent e) {
+                handle(e);
+            }
+
+            @Override
+            public void keyTyped(KeyEvent e) {
+                handle(e);
+            }
+
+            @Override
+            public void keyReleased(KeyEvent e) {
+                handle(e);
+            }
+            
+        });
+        addFocusListener(new FocusAdapter() {
+            @Override
+            public void focusGained(FocusEvent e) {
+                selectAll();
+            }
+        });
+    }
+    
+}

mercurial