DefaultMutableTreeNode getUserObject仅返回String而不是user对象

时间:2017-10-10 09:02:24

标签: java swing user-interface jtree

我实现了一个Jtree,它的节点由“Event”对象初始化。我使Jtree可编辑,并将ModelListener添加到此树的模型中。但是,当我在此侦听器中更改的节点上使用getUserObject()方法时,该节点仅返回一个String对象而不是“Event”对象。为什么? 我知道getUserObject()返回父节点,所以我使用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) {

    }
  }
}

这是事件类:

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;
 }

}

1 个答案:

答案 0 :(得分:1)

您正在将用户对象设置为root中的字符串。参见:

DefaultMutableTreeNode root = new DefaultMutableTreeNode("test");