Ja bym zrobił to na zasadzie edytora:
package test;
import java.awt.BorderLayout;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;
/**
* Klasa testująca tabele.
*
* @author jacobus2k
* @version 0.1.0
*/
public final class TableTester extends JFrame {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = 2177419577626910109L;
/**
* Tabela.
*/
private JTable table;
/**
* Konstruktor.
*/
public TableTester() {
super("Test");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 400);
this.initComponents();
this.createLayout();
}
/**
* Inicjuje komponenty.
*/
private final void initComponents() {
this.table = new JTable(5, 2);
// tworze edytor pierwszej kolumny
final DefaultCellEditor column1Editor = new DefaultCellEditor(
new JTextField());
// wyciągam komponent edytora (rzutuje do tekst komponentu i ustawiam
// formatowany dokumen
((JTextComponent) column1Editor.getComponent()).setDocument(
new FormattedDocument("[^01]", false, 5));
this.table.getColumnModel().getColumn(0).setCellEditor(column1Editor);
// tworze edytor drugiej kolumny
final DefaultCellEditor column2Editor = new DefaultCellEditor(
new JTextField());
// wyciągam komponent edytora (rzutuje do tekst komponentu i ustawiam
// formatowany dokument
((JTextComponent) column2Editor.getComponent()).setDocument(
new FormattedDocument("[^01?]", false, 2));
this.table.getColumnModel().getColumn(1).setCellEditor(column2Editor);
}
/**
* Tworzy layout.
*/
private final void createLayout() {
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(new JScrollPane(this.table),
BorderLayout.CENTER);
}
/**
* Main.
*
* @param args
*/
public static final void main(final String[] args) {
final TableTester tester = new TableTester();
tester.setVisible(true);
}
}
class FormattedDocument extends PlainDocument {
/**
* serialVersionUID.
*/
private static final long serialVersionUID = 2911052591988857807L;
/**
* Wyrażenie regularne.
*/
private String regex = null;
/**
* Określa czy wpisany tekst ma pasować do wyrażania regularnego.
*/
private boolean match = false;
/**
* Maksymalna długość tekstu.
*/
private int maxLength = Integer.MAX_VALUE;
/**
* Konstruktor.
*/
public FormattedDocument() {
super();
}
/**
* Konstruktor.
*
* @param expr wyrażenie regularne
* @param m true - jeżeli tekst ma pasować do wyrażenia regularnego, false
* w przeciwnym wypadku
* @param max maksymalna dozwolona długość tekstu
*/
public FormattedDocument(final String expr, final boolean m, final int max) {
super();
this.regex = expr;
this.match = m;
this.maxLength = max;
}
/**
* Konstruktor.
*
* @param max maksymalna dozwolona długość tekstu
*/
public FormattedDocument(final int max) {
this(null, false, max);
}
/**
* Konstruktor.
*
* @param expr wyrażenie regularne
* @param m true - jeżeli tekst ma pasować do wyrażenia regularnego, false
* w przeciwnym wypadku
*/
public FormattedDocument(final String expr, final boolean m) {
this(expr, m, Integer.MAX_VALUE);
}
/**
* {@inheritDoc}
*/
@Override
public void insertString(final int offs, final String str,
final AttributeSet a) throws BadLocationException {
if (str == null) {
return;
}
if (this.regex == null) {
// jeżeli nie podano wyrażenia regularnego sprawdzam długość
if (this.getLength() + str.length() <= this.maxLength) {
super.insertString(offs, str, a);
}
} else {
final Matcher m = Pattern.compile(this.regex).matcher(str);
if ((m.find() == !match) || this.getLength() + str.length() >
this.maxLength) {
return;
}
super.insertString(offs, str, a);
}
}
}
Formatowanie można by bardziej rozbudować, w zależności od potrzeb.