Index: trunk/src/org/openstreetmap/josm/gui/dialogs/validator/ValidatorTreePanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/dialogs/validator/ValidatorTreePanel.java	(revision 14856)
+++ trunk/src/org/openstreetmap/josm/gui/dialogs/validator/ValidatorTreePanel.java	(revision 14857)
@@ -8,5 +8,4 @@
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Enumeration;
 import java.util.HashSet;
@@ -72,6 +71,6 @@
     protected DefaultTreeModel valTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode());
 
-    /** The list of errors shown in the tree */
-    private transient List<TestError> errors = new ArrayList<>();
+    /** The list of errors shown in the tree, normally identical to field validationErrors in current edit layer*/
+    private transient List<TestError> errors;
 
     /**
@@ -91,4 +90,5 @@
      */
     public ValidatorTreePanel(List<TestError> errors) {
+        setErrorList(errors);
         ToolTipManager.sharedInstance().registerComponent(this);
         this.setModel(valTreeModel);
@@ -99,5 +99,4 @@
         this.setCellRenderer(new ValidatorTreeRenderer());
         this.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
-        setErrorList(errors);
         for (KeyListener keyListener : getKeyListeners()) {
             // Fix #3596 - Remove default keyListener to avoid conflicts with JOSM commands
@@ -159,6 +158,4 @@
             return;
         }
-        // Sort validation errors - #8517
-        sortErrors(errors);
 
         // Remember first selected tree row
@@ -264,4 +261,5 @@
                     }
 
+                 // add the matching errors to the current node
                     errors.stream().map(DefaultMutableTreeNode::new).forEach(messageNode::add);
                 });
@@ -283,10 +281,11 @@
 
     /**
-     * Sort list or errors in place.
-     * @param errors error list to be sorted
-     */
-    static void sortErrors(List<TestError> errors) {
+     * Sort list of errors in place (#8517).
+     */
+    void sortErrors() {
+        if (errors.isEmpty())
+            return;
         // Calculate the string to sort only once for each element
-        // Avoids to call TestError.compare() which costly
+        // Avoids to call TestError.compare() which is costly
         List<Pair<String, TestError>> toSort = new ArrayList<>();
         for (int i = 0; i < errors.size(); i++) {
@@ -325,5 +324,6 @@
      */
     public final void setErrorList(List<TestError> errors) {
-        this.errors = errors;
+        this.errors = errors != null ? errors : new ArrayList<>();
+        sortErrors();
         if (isVisible()) {
             buildTree();
@@ -336,7 +336,5 @@
      */
     public void setErrors(List<TestError> newerrors) {
-        if (errors == null)
-            return;
-        clearErrors();
+        errors.clear();
         for (TestError error : newerrors) {
             if (!error.isIgnored()) {
@@ -344,4 +342,5 @@
             }
         }
+        sortErrors();
         if (isVisible()) {
             buildTree();
@@ -354,5 +353,5 @@
      */
     public List<TestError> getErrors() {
-        return errors != null ? errors : Collections.<TestError>emptyList();
+        return errors;
     }
 
@@ -436,15 +435,9 @@
     }
 
-    private void clearErrors() {
-        if (errors != null) {
-            errors.clear();
-        }
-    }
-
     @Override
     public void destroy() {
         DatasetEventManager.getInstance().removeDatasetListener(this);
         ToolTipManager.sharedInstance().unregisterComponent(this);
-        clearErrors();
+        errors.clear();
     }
 
@@ -546,5 +539,5 @@
      */
     private boolean filterRemovedPrimitives() {
-        return errors != null && errors.removeIf(
+        return errors.removeIf(
                 error -> error.getPrimitives().stream().anyMatch(p -> p.isDeleted() || p.getDataSet() == null));
     }
