触发先前选择的JTree节点的鼠标侦听器

时间:2016-10-06 11:33:36

标签: java swing jtree jinternalframe

我正在处理的应用程序使用一个树,其子节点打开JInternalFrames。关闭相应的内部框架后单击同一节点不会启动新实例。

单击另一个节点(也会打开相应的框架)并单击后退工作。

打开内部框架的代码。

public class MAdapter extends MouseAdapter{
    public void onClick(MouseEvent e){
        if(e.getSource()==jTree1){              
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();

            if(node == null)return;

            //Object nodeInfo = node.getUserObject();
            String child = node.getUserObject().toString();
            System.out.println("Child: "+child);

            //Object nodeParent =  node.getParent();
            String parent = node.getParent().toString();
            System.out.println("Parent: "+parent);              




            if(parent.equalsIgnoreCase("Client")){
                if(child.equalsIgnoreCase("Create")){
                    UIClient client = new UIClient();                       
                    openInternalFrame("Client Create",client);

                }else if(child.equalsIgnoreCase("Display")){
                    ClientView view = new ClientView();
                    openInternalFrame("Client View",view);
                }else if(child.equalsIgnoreCase("Change")){
                    EditClient edit = new EditClient();
                    openInternalFrame("Edit Client",edit);
                }
            }else if(parent.equalsIgnoreCase("Requirement")){
                if(child.equalsIgnoreCase("Create")){
                    openInternalFrame("Create Requirement",new Requirement());
                }else if(child.equalsIgnoreCase("Display")){
                    ViewRequirement view =  new ViewRequirement(null);
                    openInternalFrame("View Requirement",view);
                }else if(child.equalsIgnoreCase("Change")){
                    openInternalFrame("Edit Requirement",new EditRequirement());
                }
            }               
        }
    }
}

openInternalFrame方法

 private void openInternalFrame(String name,Component com){

    JInternalFrame iframe = new JInternalFrame(name);
    iframe.addInternalFrameListener(this);
    iframe.setVisible(true);
    iframe.setMaximizable(true);
    iframe.setIconifiable(true);
    iframe.setClosable(true);
    iframe.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
    iframe.setIconifiable(true);
    iframe.setLocation(300, 400);
    iframe.add(com);        
    iframe.requestFocus();
    iframe.moveToFront();



    JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(thisFrame);
    topFrame.getContentPane().add(iframe);

    //topFrame.getContentPane().paint(getGraphics());
}

以下示例适用于我的预期。它没有问题。这里的区别是我使用JFrame而不是JInternalFrame。我猜测其中一个用于创建内部框架的类中存在错误。没关系,因为我不再工作了。 mods应该删除这个问题:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class Example {

    static JTree tree;
    static JFrame mainWindow;
    static JScrollPane pane;

    public static void createTree(JScrollPane pane){
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
        DefaultMutableTreeNode parent;
        DefaultMutableTreeNode child;
        parent = new DefaultMutableTreeNode("Parent1");
        root.add(parent);
        child = new DefaultMutableTreeNode("Child1");
        parent.add(child);
        child = new DefaultMutableTreeNode("Child2");
        parent.add(child);
        parent = new DefaultMutableTreeNode("Parent2");
        root.add(parent);
        child = new DefaultMutableTreeNode("Child1");
        parent.add(child);
        child = new DefaultMutableTreeNode("Child2");
        parent.add(child);

        tree = new JTree(new DefaultTreeModel(root));
        tree.addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent evt){
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                if(node == null) return;
                else{
                    String child = node.getUserObject().toString();
                    String parent = node.getParent().toString();
                    System.out.println(parent);
                    System.out.println(child);

                    createSecondFrame(parent,child);
                }
            }
        });
        pane.setViewportView(tree);
    }

    public static void createSecondFrame(String parent, String child){
        JFrame secondary = new JFrame(parent+", "+child);
        secondary.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        secondary.setVisible(true);     
    }

    public static void main(String[] args) {
        mainWindow = new JFrame("Example");
        pane = new JScrollPane();
        createTree(pane);
        mainWindow.setContentPane(pane);
        mainWindow.setSize(200,300);
        mainWindow.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        mainWindow.setVisible(true);
    }

 }

0 个答案:

没有答案