2015-04-01 55 views
1

我想要做的事很簡單,但遇到一些問題。我有一個類,我用一些值覆蓋toString()。JTree,setUserObject(),toString()

public class AClass { 

@Override 
public String toString() { 
    int rand = 5 + (int) (Math.random() * 25); 
    return Integer.toString(rand); 
    } 
} 

我有一個JTree,我通過使用setUserObject()將類對象添加到節點。節點上的顯示現在具有來自toString()的值。

  DefaultMutableTreeNode node = new DefaultMutableTreeNode(); 
      node.setUserObject(new AClass()); 
      fileTreeModel.insertNodeInto(node, (DefaultMutableTreeNode) rootNode, 0); 
      tree.setSelectionPath(new TreePath(fileTreeModel.getPathToRoot(node))); 
      tree.expandPath(new TreePath(rootNode.getPath())); 

但是,當我點擊節點或嘗試添加新節點時,toString()值正在改變。

這是怎麼回事?任何幫助是極大的讚賞。 以下是一些示例代碼。

Main.java

import java.awt.Color; 
import java.awt.EventQueue; 

import javax.swing.JDesktopPane; 
import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.JSplitPane; 

import java.awt.BorderLayout; 

import javax.swing.JTree; 
import javax.swing.tree.DefaultMutableTreeNode; 
import javax.swing.tree.DefaultTreeModel; 
import javax.swing.tree.TreePath; 
import javax.swing.tree.TreeSelectionModel; 

import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

import javax.swing.JScrollPane; 


public class Main { 

private JFrame frame; 
private JTree tree; 
private DefaultMutableTreeNode rootNode ; 
private DefaultTreeModel fileTreeModel; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Main window = new Main(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the application. 
*/ 
public Main() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JSplitPane splitPane = new JSplitPane(); 
    splitPane.setResizeWeight(0.2); 
    frame.getContentPane().add(splitPane, BorderLayout.CENTER); 

    JScrollPane scrollPane = new JScrollPane(); 
    splitPane.setLeftComponent(scrollPane); 

    tree = new JTree(); 
    tree.setBackground(new Color(255, 235, 205)); 
    scrollPane.setViewportView(tree); 
    rootNode = new DefaultMutableTreeNode("Files"); 
    fileTreeModel = new DefaultTreeModel(rootNode); 
    tree.setModel(fileTreeModel); 
    tree.getSelectionModel().setSelectionMode(
      TreeSelectionModel.SINGLE_TREE_SELECTION); 
    tree.setRootVisible(true); 

    JDesktopPane desktopPane = new JDesktopPane(); 
    desktopPane.setBackground(new Color(255, 255, 0)); 
    splitPane.setRightComponent(desktopPane); 

    JButton btnNewButton = new JButton("New button"); 
    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 

      DefaultMutableTreeNode node = new DefaultMutableTreeNode(); 
      node.setUserObject(new AClass()); 
      fileTreeModel.insertNodeInto(node, (DefaultMutableTreeNode) rootNode, 0); 
      tree.setSelectionPath(new TreePath(fileTreeModel.getPathToRoot(node))); 
      tree.expandPath(new TreePath(rootNode.getPath())); 
     } 
    }); 
    btnNewButton.setBounds(122, 115, 89, 23); 
    desktopPane.add(btnNewButton); 

    } 

} 

AClass.java

public class AClass { 

@Override 
public String toString() { 
    int rand = 5 + (int) (Math.random() * 25); 
    return Integer.toString(rand); 
    } 

} 

回答

4

他們正在改變,因爲每次toString是叫你正在計算新的隨機數...

public String toString() { 
    int rand = 5 + (int) (Math.random() * 25); 
    return Integer.toString(rand); 
} 

所以,每次表更新時,我們假設您點擊它們或修改TreeModel以某種方式,樹問每個節點它的值,它返回一個新的隨機數。

嘗試預先計算值。

node.setUserObject(new AClass(5 + (int) (Math.random() * 25))); 

然後簡單地返回該值...

public String toString() { 
    return Integer.toString(rand); 
} 
+0

明白了。感謝MadProgrammer。已經做出必要的改變並理解行爲。 – 2015-04-02 14:26:20

+0

很高興它可以幫助 – MadProgrammer 2015-04-02 20:37:54