title: jTree node selection
body: <p>I have Main <code>JFrame</code> with <code>jTree</code> and displayPanel, also FirstPanel and SecondPanel. By clicking <code>jTree</code> nodes will be selected each <code>jPanels</code>.</p>
  
  <p>Here is code:</p>
  
  <pre><code>package demo;
  
  import java.awt.CardLayout;
  import java.awt.event.MouseEvent;
  import javax.swing.event.TreeSelectionEvent;
  import javax.swing.event.TreeSelectionListener;
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.TreePath;
  import javax.swing.tree.TreeSelectionModel;
  
  public class Main extends javax.swing.JFrame {
  
      /**
       * Creates new form Main
       */
      public Main() {
          initComponents();
      }
  
      /**
       * This method is called from within the constructor to initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is always
       * regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          treePanel = new javax.swing.JPanel();
          jScrollPane1 = new javax.swing.JScrollPane();
          jTree1 = new javax.swing.JTree();
          displayPanel = new javax.swing.JPanel();
          firstPanel = new demo.FirstPanel();
          secondPanel = new demo.SecondPanel();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  
          javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Main");
          javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Unit");
          javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("time");
          treeNode2.add(treeNode3);
          treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("velocity");
          treeNode2.add(treeNode3);
          treeNode1.add(treeNode2);
          jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
          jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
              public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                  jTree1ValueChanged(evt);
              }
          });
          jScrollPane1.setViewportView(jTree1);
  
          treePanel.add(jScrollPane1);
  
          getContentPane().add(treePanel, java.awt.BorderLayout.WEST);
  
          displayPanel.setLayout(new java.awt.CardLayout());
          displayPanel.add(firstPanel, "first");
          displayPanel.add(secondPanel, "second");
  
          getContentPane().add(displayPanel, java.awt.BorderLayout.CENTER);
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {                                    
           String node = evt.getNewLeadSelectionPath().getLastPathComponent().toString();
           if (node == null)
           return;
      if( node.equals("time") ) {
          displayPanel.add(firstPanel);
      } else if( node.equals("velocity") ) {
         displayPanel.add(secondPanel);
      }
  
      }                                   
  
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
          /* Set the Nimbus look and feel */
          //&lt;editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "&gt;
          /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
           * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
           */
          try {
              for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                  if ("Nimbus".equals(info.getName())) {
                      javax.swing.UIManager.setLookAndFeel(info.getClassName());
                      break;
                  }
              }
          } catch (ClassNotFoundException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (InstantiationException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (IllegalAccessException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          } catch (javax.swing.UnsupportedLookAndFeelException ex) {
              java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
          }
          //&lt;/editor-fold&gt;
  
          /* Create and display the form */
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new Main().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private javax.swing.JPanel displayPanel;
      private demo.FirstPanel firstPanel;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JTree jTree1;
      private demo.SecondPanel secondPanel;
      private javax.swing.JPanel treePanel;
      // End of variables declaration                   
  }
  </code></pre>
  
  <p>Problem is there:</p>
  
  <pre><code>private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {                                    
           String node = evt.getNewLeadSelectionPath().getLastPathComponent().toString();
           if (node == null)
           return;
      **if( node.equals("time") ) {
          displayPanel.add(firstPanel);
      } else if( node.equals("velocity") ) {
         displayPanel.add(secondPanel);**
      }
  </code></pre>
  
  <p>When I am clicking <strong>time node</strong> it shows me <strong>secondPanel</strong>, when clicking <strong>velocity node</strong> it shows me <strong>firstPanel</strong>. I don't know how to switch them.
  Question is: how to make that <strong>time node</strong> shows <strong>firstPanel</strong>, <strong>velocity node</strong> shows <strong>secondPanel</strong>?</p>
  
  <p>Thank you in advance.</p>