1 Reply Latest reply: Jan 22, 2013 8:56 PM by EJP RSS

    Serialization/Deserialization of JTree Object

    986028
      hello friends. i made source code that serialization/deserialization of oracle JTree object example.

      But it has some problem...

      I made that serialize and deserialize method of JTree object.

      these methods are used to make serialize file/read file and deserialize.

      When click 'save' button, serialize method is excute

      and program is start, excute deserialize method.


      problem : if i do click 'save', make file well. but deserializing is not well.

      add node and leaf, not load JTree's status.

      upload part of source code. plz give me an answer.


      p.s :
      1. not use xml encoding/decoding. this is file serialization/deserialization problem.
      2. not occurred error.(exception message).
      3. another data class's serialization/deserialization was operated same code.




      ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

      JTree source code : http://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#DynamicTreeDemo

      Add node, leaf code
      public void actionPerformed(ActionEvent e) {
          String command = e.getActionCommand();
              
          if (ComConstant.ADD_COMMAND.equals(command)) {
                  //Add button clicked
              if (treePanel.getNodeLevel() == 0){
                  String rtuNo = JOptionPane.showInputDialog(null,"please insert additional number");
                  String newName = JOptionPane.showInputDialog(null,"please insert additional name");
                  if (newName != null && no != null){
                      treePanel.addObject(no+". "+newName);
                      DefaultMutableTreeNode subNode = treePanel.rootNode.getLastLeaf();
                      treePanel.addObject(subNode, "leaf1");
                      treePanel.addObject(subNode, "leaf2");
                      treePanel.addObject(subNode, "leaf3");
                      treePanel.addObject(subNode, "leaf4");
                      treePanel.addObject(subNode, "leaf5");
                      treePanel.addObject(subNode, "leaf6");
                      treePanel.addObject(subNode, "leaf7");
                      treePanel.addObject(subNode, "leaf8");
                  }
              }
          } else if (ComConstant.REMOVE_COMMAND.equals(command)) {
                  //Remove button clicked
              treePanel.removeCurrentNode();
          } else if (ComConstant.CLEAR_COMMAND.equals(command)) {
                  //Clear button clicked.
              treePanel.clear();
          }
      }
      Serialization/Deserialization code
      public static void saveDataToObjectFile(DynamicTreeDemo treeInfo){       //serialization code
          treeData = new File(OSConfig.getPath(DskConstant.OBJ), DskConstant.TREE_OBJECT_FILE);    //file path
      
          try {
              FileOutputStream fos = new FileOutputStream(treeData);
              ObjectOutputStream oos = new ObjectOutputStream(fos);
              oos.writeObject(treeInfo);
              oos.close();
              System.out.println("treeInfo.object file saved.");
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
      
      public static DynamicTreeDemo loadTreeDataFromObjectFile(){          //deserialization code
          treeData = new File(OSConfig.getPath(DskConstant.OBJ), DskConstant.TREE_OBJECT_FILE);    //file path
          DymTree treeInfo = new DymTree();
          if(treeData.exists()){
              try {
                  FileInputStream fis = new FileInputStream(treeData);
                  ObjectInputStream ois = new ObjectInputStream(fis);
                  treeInfo = (DymTree)ois.readObject();
                  ois.close();
              } catch (FileNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
          }
          return treeInfo;
      }
      Edited by: 983025 on 2013. 1. 21 오후 4:24

      Edited by: 983025 on 2013. 1. 21 오후 4:25