Sat, 26 Jan 2013 17:42:07 +0100
check functions
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 final class SudokuTextField extends JTextField { private boolean modified; 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 && getSelectedText() == null) { 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(); } else { setModified(true); } } } @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(); } }); } public int getValue() { if (getText().length() > 0) { return Integer.valueOf(getText()); } else { return 0; } } public void setValue(int v) { if (v == 0) { setText(""); } else if (v < 10) { setText(String.valueOf(v)); } else { throw new IllegalArgumentException( "Sudoku numbers must be in range 0-9 (0 means 'not set')"); } } public void setModified(boolean modified) { this.modified = modified; setForeground(modified?Color.BLUE:Color.BLACK); } public boolean isModified() { return modified; } }