vorrei inserire una regex in JFormattedTextField in modo che l'isbn sia giusto.
ho provato a fare così:
diciamo che ha un malfunzionamento generale.codice:private Pattern pattern = Pattern.compile("ISBN\\x20(?=.{13}$)\\d{1,5}([- ])\\d{1,7}\\1\\d{1,6}\\1(\\d|X)$"); textIsbn = new javax.swing.JFormattedTextField( new RegexPatternFormatter(pattern) ); ....... private class RegexPatternFormatter extends DefaultFormatter { private Matcher matcher; public RegexPatternFormatter(Pattern regex) { setOverwriteMode(false); matcher = regex.matcher(""); } public Object stringToValue(String string) throws ParseException { if (string == null) { JOptionPane.showMessageDialog(panelContainer, "Missing element ISBN"); return null; } matcher.reset(string); if (!matcher.matches()) { // JOptionPane.showMessageDialog(panelContainer, "Wrong ISBN"); throw new ParseException("does not match regex", 0); } return super.stringToValue(string); } }
quando clicco sul bottone che dovrebbe inviare i dati a un db semplicemente il campo si svuota e nn succede nulla.
se invece decommento quel JOptiopane mi esce un errore ad ogni carattere digitato.
probabilmente ho fatto un pò di casino.

Rispondi quotando
