2013-07-30 3 views
3

Je teste le SwingX AutoCompleteDecorator, le code J'utilise est le suivant:SwingX AutoCompleteDecorator JTextField erreurs

import javax.swing.text.JTextComponent; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.JPanel; 
import javax.swing.JList; 

import org.jdesktop.swingx.autocomplete.*; 

public class Test extends JFrame 
{ 
    public Test() 
    { 
     this.setTitle("Testing"); 
     JPanel pan = new JPanel(); 
     JTextComponent test = new JTextField(); 
     ((JTextField)test).setColumns(20); 
     String[] data = {"one", "two", "three", "four"}; 
     JList dataList = new JList(data); 
     AutoCompleteDecorator.decorate(dataList, test); 
     pan.add(test); 
     this.setContentPane(pan); 
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     this.setVisible(true); 
     this.setBounds(280,150,500,200); 

    } 
    public static void main(String[] args) 
    { 
     Test t = new Test(); 
    } 
}  

je le vent jusqu'à obtenir un message d'erreur d'exécution énorme que je ne veux pas vraiment poster la trace de la pile pour environ une exception dans le thread "AWT-EventQueue-0" java.lang.NullPointerException:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at org.jdesktop.swingx.autocomplete.ListAdaptor.valueChanged(ListAdaptor.java:77) 
    at javax.swing.JList.fireSelectionValueChanged(JList.java:1798) 
    at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812) 
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184) 
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164) 
    at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211) 
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405) 
    at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415) 
    at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:459) 
    at javax.swing.JList.setSelectedIndex(JList.java:2212) 
    at javax.swing.JList.setSelectedValue(JList.java:2362) 
    at org.jdesktop.swingx.autocomplete.ListAdaptor.setSelectedItem(ListAdaptor.java:99) 
    at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.setSelectedItem(AutoCompleteDocument.java:355) 
    at org.jdesktop.swingx.autocomplete.AutoCompleteDocument.insertString(AutoCompleteDocument.java:303) 
    at javax.swing.text.JTextComponent.replaceSelection(JTextComponent.java:1386) 
    at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(DefaultEditorKit.java:884) 
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664) 
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2879) 
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2926) 
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2842) 
    at java.awt.Component.processEvent(Component.java:6282) 
    at java.awt.Container.processEvent(Container.java:2229) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895) 
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762) 
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027) 
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899) 
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727) 
    at java.awt.Component.dispatchEventImpl(Component.java:4731) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:688) 
    at java.awt.EventQueue$3.run(EventQueue.java:686) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:702) 
    at java.awt.EventQueue$4.run(EventQueue.java:700) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

Qu'ai-je manqué?

+0

J'ai lu http://stackoverflow.com/questions/11928999/jtextfield-autocompletion-error?rq=1, et la réponse ne résout pas le problème que j'ai actuellement. – JustAnotherSoul

+0

hmm .. peut être un bug, peut le reproduire avec votre exemple et swinglabs-demos - vous pourriez envisager de le signaler dans le tracker de swingx afin que nous n'oublions pas de le regarder (faites-en la plus haute priorité pour nous lancer réellement J'y pense :-) – kleopatra

+0

Je pense que je vais m'asseoir dessus pendant un jour ou deux, juste au cas où quelqu'un verrait quelque chose de subtil que j'ai réussi à briser. – JustAnotherSoul

Répondre

4

Il y a un bug: le coupable est un ObjectToStringConverter nul dans la décoration d'un textComponent avec les éléments d'une JList, en utilisant la méthode à deux paramètres:

public static void decorate(JList list, JTextComponent textComponent) { 
    decorate(list, textComponent, null); 
} 

Une solution rapide est d'utiliser la méthode à trois paramètres et passer dans le convertisseur par défaut:

JTextComponent test = new JTextField(20); 
String[] data = {"one", "two", "three", "four"}; 
JList dataList = new JList(data); 
AutoCompleteDecorator.decorate(dataList, test, ObjectToStringConverter.DEFAULT_IMPLEMENTATION); 

Filed Issue #1570 - fixe comme la révision # 4305


Matin Rêveries (peut être ignoré en toute sécurité :-)

La raison technique est incorrecte constructeur Enchaînement: insérer la valeur par défaut doit être manipulé par le do-it-all constructeur (sinon il faut jeter un NPE)

public ListAdaptor(JList list, JTextComponent textComponent) { 
    this(list, textComponent, ObjectToStringConverter.DEFAULT_IMPLEMENTATION); 
} 

public ListAdaptor(JList list, JTextComponent textComponent, ObjectToStringConverter stringConverter) { 
    this.list = list; 
    this.textComponent = textComponent; 
    this.stringConverter = stringConverter; 
    // when a new item is selected set and mark the text 
    list.addListSelectionListener(this); 
} 

La raison sous-jacente est un changement subtil dans la propriété du convertisseur: son propriétaire habituel est le document personnalisé qui gère la saisie semi-automatique, ce document se protège contre une valeur nulle. Avec une variante JList, c'est le ListAdaptor qui n'est pas habitué à ce fardeau ... Le décalage n'est pas incorrect (en fait, le chemin exact à suivre), introduit juste une incohérence si légère qui est facile à négliger.

0
AutoCompleteDecorator.decorate(dataList, test, false) 

ajouter simplement les paramètres faux

ou tout simplement comme ceux-ci

boolean strictMatching = false; 
AutoCompleteDecorator.decorate(UName,items,strictMatching);