//package text;
/*
* TextfeldDemo.java
*/
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextfeldDemo extends JFrame {
public TextfeldDemo() {
super("Textfeld Demo");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400,300);
setLocationRelativeTo(null);
//JComponents erzeugen:
mainPanel = new JPanel();
textField1 = new JTextField(6);
textField2 = new JTextField(3);
//Layout:
mainPanel.add(new JLabel("6 Zeichen: "));
mainPanel.add(textField1);
mainPanel.add(new JLabel(" 3 Zeichen: "));
mainPanel.add(textField2);
getContentPane().add(mainPanel, BorderLayout.CENTER);
textField1.setDocument(new TextFeldHuepft(6, textField2));//maximal 6 Zeichen
textField2.setDocument(new TextFeldHuepft(3, textField1));//maximal 3 Zeichen
}
public static void main(String args[]) {new TextfeldDemo().setVisible(true);}
private JPanel mainPanel;
private JTextField textField1, textField2;
}
class TextFeldHuepft extends DefaultStyledDocument {
public TextFeldHuepft(int maxChars, JTextField next) {
maxCharacters = maxChars;
this.next = next;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
int len = getLength() + str.length();
if(len <= maxCharacters){
super.insertString(offs, str, a);
if(len == maxCharacters){
next.requestFocusInWindow();
}
}else{
Toolkit.getDefaultToolkit().beep();
}
}
private int maxCharacters;
private JTextField next;
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at dvorak.ranking.util.JumpTextField.insertString(JumpTextField.java:36)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.replaceSelection(Unknown Source)
package dvorak.ranking.util;
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
/**
* Jump to next text field
* @version 2007-03-02
*/
public class JumpTextField extends DefaultStyledDocument {
private int maxCharacters;
private JTextField next;
/**
* Jump to next text field if chars are filled
* @param maxChars
* @param next
*/
public JumpTextField(int maxChars, JTextField next) {
maxCharacters = maxChars;
this.next = next;
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
int len = getLength() + str.length();
if(len <= maxCharacters){
super.insertString(offs, str, a);
if(len == maxCharacters){
next.requestFocusInWindow();
}
}else{
Toolkit.getDefaultToolkit().beep();
}
}
}
package dvorak.ranking.windows;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import org.apache.log4j.Logger;
import dvorak.ranking.ctrl.DistanceData;
import dvorak.ranking.ctrl.EventData;
import dvorak.ranking.ctrl.ResultPanelAnalysis;
import dvorak.ranking.ctrl.ListPersonTable;
import dvorak.ranking.model.MyPanel;
import dvorak.ranking.util.JumpTextField;
import dvorak.ranking.util.TextFieldLength;
/**
* Entry records panel
* @author Andreas Dvorak
* @version 2006-12-29
*/
public class EntryResultPanel extends MyPanel
.......
public EntryResultPanel(JFrame parent){
parent_ = parent;
ctrl_ = new ResultPanelAnalysis(this);
...........
hoursTimeTextField = new JTextField();
hoursTimeTextField.setPreferredSize(new java.awt.Dimension(20,20));
//hoursTimeTextField.setDocument(new TextFieldLength(2));
hoursTimeTextField.setDocument(new JumpTextField(2, minutesTimeTextField));//maximal 6 Zeichen
minutesTimeTextField = new JTextField();
minutesTimeTextField.setPreferredSize(new java.awt.Dimension(20,20));
minutesTimeTextField.setDocument(new TextFieldLength(2));
hoursTimeTextField.setDocument(new JumpTextField(2, minutesTimeTextField));//maximal 6 Zeichen
minutesTimeTextField = new JTextField();