DefaultMutableTreeNode 仅 getUserObject return 字符串而不是用户对象
DefaultMutableTreeNode getUserObject only return String instead of user object
我实现了一个Jtree,它的节点由"Event"对象初始化。我使 Jtree 可编辑,并为这棵树的模型添加了一个 ModelListener。但是,当我在此侦听器中的已更改节点上使用 getUserObject() 方法时,该节点仅 return 我是一个 String 对象而不是 "Event" 对象。为什么?
我知道 getUserObject() return 是父节点,所以我使用 parent.getChildAt(0) 来获取真正更改的节点,但它没有用。
这是我的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class TreeObject extends JFrame {
private JPanel contentPane;
private JTree tree;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeObject frame = new TreeObject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TreeObject() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 659, 496);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane);
tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("test");
DefaultTreeModel model = new DefaultTreeModel(root);
tree.setModel(model);
model.addTreeModelListener(new MyModeListener());
tree.setEditable(true);
createEvent(model);
scrollPane.setViewportView(tree);
}
public void createEvent(DefaultTreeModel model){
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
int i = root.getChildCount();
final Event event = new Event("Event");
DefaultMutableTreeNode secondLevel = new DefaultMutableTreeNode(event);
model.insertNodeInto(secondLevel, root, root.getChildCount());
tree.scrollPathToVisible(new TreePath(secondLevel.getPath()));
System.out.println(secondLevel.getUserObject().getClass());
}
public class MyModeListener implements TreeModelListener{
@Override
public void treeNodesChanged(TreeModelEvent arg0) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(arg0.getTreePath().getLastPathComponent());
System.out.println(((DefaultMutableTreeNode) node.getChildAt(0)).getUserObject().getClass());//"WHY??"
}
@Override
public void treeNodesInserted(TreeModelEvent arg0) {
}
@Override
public void treeNodesRemoved(TreeModelEvent arg0) {
}
@Override
public void treeStructureChanged(TreeModelEvent arg0) {
}
}
}
这是活动 class:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Event {
private long ID;
private String name;
private String lawType;
private Map<String,String> values = new HashMap<>();
public Event(String name){
this.ID = System.currentTimeMillis();
this.name = name;
addValue("Lambda", "1E-6");
}
@Override
public String toString() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getLawType() {
return lawType;
}
public void setLawType(String lawType) {
this.lawType = lawType;
}
public void addValue(String valueName,String value){
this.values.put(valueName, value);
}
public Map<String, String> getValues() {
return values;
}
public String getName() {
return name;
}
public long getID() {
return ID;
}
}
您正在将用户对象设置为根目录中的字符串。参见:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("test");
我实现了一个Jtree,它的节点由"Event"对象初始化。我使 Jtree 可编辑,并为这棵树的模型添加了一个 ModelListener。但是,当我在此侦听器中的已更改节点上使用 getUserObject() 方法时,该节点仅 return 我是一个 String 对象而不是 "Event" 对象。为什么? 我知道 getUserObject() return 是父节点,所以我使用 parent.getChildAt(0) 来获取真正更改的节点,但它没有用。 这是我的代码:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeModelEvent;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JScrollPane;
import javax.swing.JTree;
public class TreeObject extends JFrame {
private JPanel contentPane;
private JTree tree;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TreeObject frame = new TreeObject();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TreeObject() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 659, 496);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new GridLayout(1, 0, 0, 0));
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane);
tree = new JTree();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("test");
DefaultTreeModel model = new DefaultTreeModel(root);
tree.setModel(model);
model.addTreeModelListener(new MyModeListener());
tree.setEditable(true);
createEvent(model);
scrollPane.setViewportView(tree);
}
public void createEvent(DefaultTreeModel model){
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
int i = root.getChildCount();
final Event event = new Event("Event");
DefaultMutableTreeNode secondLevel = new DefaultMutableTreeNode(event);
model.insertNodeInto(secondLevel, root, root.getChildCount());
tree.scrollPathToVisible(new TreePath(secondLevel.getPath()));
System.out.println(secondLevel.getUserObject().getClass());
}
public class MyModeListener implements TreeModelListener{
@Override
public void treeNodesChanged(TreeModelEvent arg0) {
DefaultMutableTreeNode node;
node = (DefaultMutableTreeNode)(arg0.getTreePath().getLastPathComponent());
System.out.println(((DefaultMutableTreeNode) node.getChildAt(0)).getUserObject().getClass());//"WHY??"
}
@Override
public void treeNodesInserted(TreeModelEvent arg0) {
}
@Override
public void treeNodesRemoved(TreeModelEvent arg0) {
}
@Override
public void treeStructureChanged(TreeModelEvent arg0) {
}
}
}
这是活动 class:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Event {
private long ID;
private String name;
private String lawType;
private Map<String,String> values = new HashMap<>();
public Event(String name){
this.ID = System.currentTimeMillis();
this.name = name;
addValue("Lambda", "1E-6");
}
@Override
public String toString() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getLawType() {
return lawType;
}
public void setLawType(String lawType) {
this.lawType = lawType;
}
public void addValue(String valueName,String value){
this.values.put(valueName, value);
}
public Map<String, String> getValues() {
return values;
}
public String getName() {
return name;
}
public long getID() {
return ID;
}
}
您正在将用户对象设置为根目录中的字符串。参见:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("test");