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);
}
}
明白了。感謝MadProgrammer。已經做出必要的改變並理解行爲。 – 2015-04-02 14:26:20
很高興它可以幫助 – MadProgrammer 2015-04-02 20:37:54