Java - DefaultTreeNode中的多个对象类型。如何在不进行投射的情况下检索正确

时间:2015-03-31 20:57:42

标签: java polymorphism jtree

这是我的第一个问题,我希望我已将其发布在正确的区域。

我有JTree,我添加了具有不同用户对象的节点。 例如:

 AClass A = new AClass();
 DefaultMutableTreeNode cNodeA = new DefaultMutableTreeNode();
 cNodeA.setUserObject(A);
 //add to tree code here

 BClass B = new BClass();
 DefaultMutableTreeNode cNodeB = new DefaultMutableTreeNode();
 cNodeB.setUserObject(B);
 //add to tree code here

当我检索这些节点时,如何获取与节点关联的正确对象而无需转换它们。我认为应该有更好的方法来实现这一点,而不必在if:else语句或switch case语句中使用getClass()或instanceOf()。

最后,我想使用这样的东西,在与节点关联的正确类中调用doSomething()方法。

cnode.getUserObject().doSomething();

这是一个简单的模型代码。

非常感谢任何帮助。

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

import javax.swing.JFrame;
import javax.swing.JButton;

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

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

public class Main {

private JFrame frame;
private JTree tree = null;
private DefaultTreeModel treeModel = null;

/**
 * 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);

    JPanel panel = new JPanel();
    panel.setBounds(141, 11, 151, 175);
    frame.getContentPane().add(panel);

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

    panel.add(tree);

    JButton btnBObjButton = new JButton("B");
    btnBObjButton.setBounds(219, 239, 89, 23);
    btnBObjButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            BClass B = new BClass();
            DefaultMutableTreeNode cNode = new DefaultMutableTreeNode();
            cNode.setUserObject(B);
            treeModel.insertNodeInto(cNode, (MutableTreeNode) rootNode, 0);
            tree.setSelectionPath(new TreePath(treeModel
                    .getPathToRoot(cNode)));
            tree.expandPath(new TreePath(rootNode.getPath()));
        }
    });
    frame.getContentPane().setLayout(null);
    frame.getContentPane().add(btnBObjButton);

    JButton btnAObjButton = new JButton("A");
    btnAObjButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            AClass A = new AClass();
            DefaultMutableTreeNode cNode = new DefaultMutableTreeNode();
            cNode.setUserObject(A);
            treeModel.insertNodeInto(cNode, (MutableTreeNode) rootNode, 0);
            tree.setSelectionPath(new TreePath(treeModel
                    .getPathToRoot(cNode)));
            tree.expandPath(new TreePath(rootNode.getPath()));

        }
    });
    btnAObjButton.setBounds(120, 239, 89, 23);
    frame.getContentPane().add(btnAObjButton);

    JButton btndosomethingButton = new JButton("Do Something");
    btndosomethingButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if (rootNode.getChildCount() > 0) {
                DefaultMutableTreeNode cnode = (DefaultMutableTreeNode) tree
                        .getLastSelectedPathComponent();
                System.out.println(cnode.getUserObject());

                /*
                 * if(cnode is of Type ClassA){ //Do Something;
                 * ((ClassA)(cnode.getUserObject()).doSomething(); } else
                 * if(cnode is of Type ClassB){
                 * ((ClassB)(cnode.getUserObject()).doSomething(); }
                 */
            }
        }

    });
    btndosomethingButton.setBounds(153, 212, 127, 23);
    frame.getContentPane().add(btndosomethingButton);

   }
}

ClassA.java

public class AClass implements MethodInterface {

@Override
public void doSomething() {
    // TODO Auto-generated method stub

}
}

BClass.java

public class BClass implements MethodInterface{

@Override
public void doSomething() {
    // TODO Auto-generated method stub

  }
}

MethodInterface.java

public interface MethodInterface {

void doSomething();
}

0 个答案:

没有答案