Example usage for javax.swing JTree addMouseListener

List of usage examples for javax.swing JTree addMouseListener

Introduction

In this page you can find the example usage for javax.swing JTree addMouseListener.

Prototype

public synchronized void addMouseListener(MouseListener l) 

Source Link

Document

Adds the specified mouse listener to receive mouse events from this component.

Usage

From source file:Main.java

public static void main(String[] args) {

    DefaultMutableTreeNode root = new DefaultMutableTreeNode(Boolean.TRUE);
    DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(Boolean.FALSE);
    DefaultMutableTreeNode child2 = new DefaultMutableTreeNode(Boolean.FALSE);
    root.add(child1);//from   w  ww  .  j a va  2  s.  co  m
    root.add(child2);
    DefaultTreeModel model = new DefaultTreeModel(root);

    JTree tree = new JTree(model);
    tree.setCellRenderer(new TreeRenderer());

    tree.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            TreePath pathForLocation = tree.getPathForLocation(e.getX(), e.getY());
            Object lastPathComponent = pathForLocation.getLastPathComponent();
            if (lastPathComponent instanceof DefaultMutableTreeNode) {
                Boolean oldObject = (Boolean) ((DefaultMutableTreeNode) lastPathComponent).getUserObject();
                ((DefaultMutableTreeNode) lastPathComponent).setUserObject(!oldObject);
                model.nodeChanged((DefaultMutableTreeNode) lastPathComponent);
            }
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(tree, BorderLayout.CENTER);
    frame.setSize(800, 600);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    final JTree tree = new JTree(getTreeModel());
    tree.setCellRenderer(new MyTreeCellRenderer());
    HyperlinkMouseListener listener = new HyperlinkMouseListener(tree);
    tree.addMouseListener(listener);
    tree.addMouseMotionListener(listener);
    JFrame f = new JFrame(Main.class.getSimpleName());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(new JScrollPane(tree), BorderLayout.CENTER);
    f.pack();//from   w ww. j a  v a 2s  .  c o  m
    f.setSize(f.getWidth() + 100, f.getHeight() + 100);
    f.setVisible(true);
}

From source file:Main.java

public Main() {
    JTree myTree = new JTree();
    myTree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    myTree.addMouseListener(new MyMouseAdapter());

    add(new JScrollPane(myTree));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();//from w w w.  j  a  v  a2 s . c  o m
    setVisible(true);
}

From source file:org.eevolution.form.VCRPDetail.java

private void handleActionEvent(ActionEvent e) {

    Timestamp df = getDateFrom();
    Timestamp dt = getDateTo();//  w w w.j a  v  a2s . co m
    MResource r = getResource(resource.getValue());

    if (df != null && dt != null && r != null) {

        model = CRPDatasetFactory.get(df, dt, r);

        JFreeChart jfreechart = createChart(model.getDataset(), getChartTitle(),
                getSourceUOM(resource.getValue()));

        chartPanel = new ChartPanel(jfreechart, false);
        contentPanel.setLeftComponent(chartPanel);

        JTree tree = model.getTree();
        tree.addMouseListener(new TreeHandler());
        contentPanel.setRightComponent(new JScrollPane(tree));
        popup = createPopup(tree);
        contentPanel.setDividerLocation(0.70);
        contentPanel.setVisible(true);

        contentPanel.validate();
        contentPanel.repaint();
    }

    SwingTool.setCursorsFromParent(m_form.getWindow(), false);
}

From source file:de.rub.syssec.saaf.gui.editor.FileTree.java

public FileTree(final ApplicationInterface app, File dir, OpenAnalysis open) {
    super();/*from   w w  w .  j  a  v  a 2 s . c o m*/
    this.openAna = open;
    history = new Vector<Vector<String>>();
    linkEditorKit = new LinkEditorKit(history, app.getUnpackedDataDir(), this);

    directory = dir;
    //userful to debug layout issues
    //setBackground(Color.MAGENTA);
    setLayout(new GridBagLayout());

    this.model = new EditorModel(app);

    // we want to be notified if the file changes so we can reflect that in
    // the tree
    model.addPropertyChangeListener(this);

    // the tree that lists the files (top left)
    JTree tree = new JTree(addNodes(null, dir));
    tree.addMouseListener(ma);
    tree.addTreeSelectionListener(new SelectionListener());
    tree.setCellRenderer(new FileCellRenderer());
    fileTree = tree;

    GridBagConstraints treeConstraints = new GridBagConstraints();
    treeConstraints.fill = GridBagConstraints.BOTH;
    treeConstraints.gridheight = 1;
    treeConstraints.gridwidth = 1;
    treeConstraints.gridx = 0;
    treeConstraints.gridy = 0;
    treeConstraints.weightx = 0.20;
    treeConstraints.weighty = 1.0;
    treeConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
    this.add(new JScrollPane(tree), treeConstraints);

    // the list of components (bottom left)
    EntryPointsView entrypoints = new EntryPointsView(model);
    model.addPropertyChangeListener(entrypoints);

    JScrollPane entryPointsScroller = new JScrollPane(entrypoints);
    GridBagConstraints entrypointConstraints = new GridBagConstraints();
    entrypointConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
    entrypointConstraints.fill = GridBagConstraints.BOTH;
    entrypointConstraints.gridheight = 1;
    entrypointConstraints.gridwidth = 1;
    entrypointConstraints.gridx = 0;
    entrypointConstraints.gridy = 1;
    entrypointConstraints.weightx = 0.15;
    entrypointConstraints.weighty = 1.0;

    this.add(entryPointsScroller, entrypointConstraints);

    // the editor (contains the textview and the list of methods)
    this.editor = new EditorView(model, this);
    this.model.addPropertyChangeListener(this.editor);

    GridBagConstraints editorConstraints = new GridBagConstraints();
    editorConstraints.anchor = GridBagConstraints.NORTHWEST;
    editorConstraints.fill = GridBagConstraints.BOTH;
    editorConstraints.gridheight = 2;
    editorConstraints.gridwidth = 1;
    editorConstraints.gridx = 1;
    editorConstraints.gridy = 0;
    editorConstraints.weightx = 0.70;
    editorConstraints.weighty = 1.0;
    this.add(editor, editorConstraints);

    this.outlineTree = new OutlineView(this.model);
    model.addPropertyChangeListener("currentClass", outlineTree);

    GridBagConstraints outlineConstraints = new GridBagConstraints();
    outlineConstraints.anchor = GridBagConstraints.NORTHWEST;
    outlineConstraints.fill = GridBagConstraints.BOTH;
    outlineConstraints.gridwidth = 1;
    outlineConstraints.gridheight = 2;
    outlineConstraints.gridx = 2;
    outlineConstraints.gridy = 0;
    outlineConstraints.weightx = 0.15;
    outlineConstraints.weighty = 1.0;
    this.add(outlineTree, outlineConstraints);

    String shortpath = model.getCurrentFile().getAbsolutePath()
            .replace(app.getApplicationDirectory().getAbsolutePath(), "");
    this.setTitle("Editor - " + shortpath);
    searchNode(shortpath, null);
}

From source file:edu.ku.brc.specify.config.DebugLoggerDialog.java

@Override
public void createUI() {
    super.createUI();

    configureLoggers();//from ww w  .  j av a2 s  . c o  m

    root = new LoggerNode(null, "Root");
    for (String loggerName : sortedNames) {
        buildTree(loggers.get(loggerName), root, StringUtils.split(loggerName, '.'), 0);
    }

    JTree tree = new JTree(root);
    tree.setCellRenderer(new CheckRenderer());
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.putClientProperty("JTree.lineStyle", "Angled");
    tree.addMouseListener(new NodeSelectionListener(tree));

    contentPanel = new JScrollPane(tree);

    mainPanel.add(contentPanel, BorderLayout.CENTER);

    setSize(500, 500);
}

From source file:MainClass.java

public MainClass() {
    final JTree tree;
    final JTextField jtf;

    DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");

    DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
    top.add(a);// w w  w. j a va 2  s .  c  o m
    DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
    a.add(a1);
    DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
    a.add(a2);

    DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
    top.add(b);
    DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
    b.add(b1);
    DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
    b.add(b2);
    DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
    b.add(b3);

    tree = new JTree(top);

    JScrollPane jsp = new JScrollPane(tree);

    add(jsp, BorderLayout.CENTER);

    jtf = new JTextField("", 20);
    add(jtf, BorderLayout.SOUTH);

    tree.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
            if (tp != null)
                jtf.setText(tp.toString());
            else
                jtf.setText("");
        }
    });

}

From source file:ca.sqlpower.architect.swingui.enterprise.SecurityPanel.java

public SecurityPanel(SPServerInfo serverInfo, Action closeAction, Dialog d, ArchitectSession session) {
    this.closeAction = closeAction;

    splitpane = new JSplitPane();
    panel = new JPanel();

    ArchitectClientSideSession clientSideSession = ArchitectClientSideSession.getSecuritySessions()
            .get(serverInfo.getServerAddress());

    //Displaying an indeterminate progress bar in place of the split pane
    //until the security session has loaded fully.
    if (clientSideSession.getUpdater().getRevision() <= 0) {
        JLabel messageLabel = new JLabel("Opening");
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);

        DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout("pref:grow, 5dlu, pref"));
        builder.setDefaultDialogBorder();
        builder.append(messageLabel, 3);
        builder.nextLine();/*from ww  w.  j  ava2  s  .  co m*/
        builder.append(progressBar, 3);

        UpdateListener l = new UpdateListener() {

            @Override
            public void workspaceDeleted() {
                //do nothing
            }

            @Override
            public boolean updatePerformed(AbstractNetworkConflictResolver resolver) {
                panel.removeAll();
                panel.add(splitpane);
                dialog.pack();
                refreshTree();
                return true;
            }

            @Override
            public boolean updateException(AbstractNetworkConflictResolver resolver, Throwable t) {
                //do nothing, the error will be handled elsewhere
                return true;
            }

            @Override
            public void preUpdatePerformed(AbstractNetworkConflictResolver resolver) {
                //do nothing
            }
        };

        clientSideSession.getUpdater().addListener(l);
        panel.add(builder.getPanel());
    }
    this.securityWorkspace = clientSideSession.getWorkspace();
    this.username = serverInfo.getUsername();
    this.dialog = d;
    this.session = session;

    try {
        digester = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    rootNode.add(usersNode);
    rootNode.add(groupsNode);

    rightSidePanel = new JPanel();

    tree = new JTree(rootNode);
    tree.addTreeSelectionListener(treeListener);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.setCellRenderer(new DefaultTreeCellRenderer() {
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
            Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
            if (userObject instanceof User) {
                setIcon(USER_ICON);
            } else if (userObject instanceof Group) {
                setIcon(GROUP_ICON);
            }
            return this;
        }
    });

    treePane = new JScrollPane(tree);
    treePane.setPreferredSize(new Dimension(200, treePane.getPreferredSize().height));

    tree.addMouseListener(popupListener);

    splitpane.setRightComponent(rightSidePanel);
    splitpane.setLeftComponent(treePane);

    if (clientSideSession.getUpdater().getRevision() > 0) {
        panel.removeAll();
        panel.add(splitpane);
    }

    refreshTree();

    try {
        tree.setSelectionPath(new TreePath(usersNode.getFirstChild()));
    } catch (NoSuchElementException e) {
    } // This just means that the node has no children, so we cannot expand the path.
}

From source file:edu.ku.brc.specify.ui.containers.ContainerTreePanel.java

/**
 * @param actionTree//from  ww w . j a v a  2  s .  c  om
 */
private void registerPrintContextMenu(final JTree actionTree) {
    actionTree.addMouseListener(new MouseAdapter() {
        private void displayMenu(MouseEvent e) {
            if (e.isPopupTrigger()) {
                JPopupMenu menu = new JPopupMenu();
                JMenuItem printMenu = new JMenuItem(UIRegistry.getResourceString("Print"));
                menu.add(printMenu);
                menu.show(e.getComponent(), e.getX(), e.getY());
                printMenu.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent ev) {
                        print(actionTree);
                    }
                });
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            displayMenu(e);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            displayMenu(e);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            super.mouseReleased(e);
            displayMenu(e);
        }

    });
}

From source file:eu.europa.ec.markt.dss.applet.view.validationpolicy.EditView.java

private void registerMouseListener(final JTree tree) {

    MouseListener mouseAdapter = new MouseAdapter() {
        public void mousePressed(MouseEvent mouseEvent) {
            if (mouseEvent.getButton() == MouseEvent.BUTTON3) {
                final int selectedRow = tree.getRowForLocation(mouseEvent.getX(), mouseEvent.getY());
                final TreePath selectedPath = tree.getPathForLocation(mouseEvent.getX(), mouseEvent.getY());
                if (selectedRow != -1) {
                    final XmlDomAdapterNode clickedItem = (XmlDomAdapterNode) selectedPath
                            .getLastPathComponent();
                    final boolean isLeaf = tree.getModel().isLeaf(selectedPath.getLastPathComponent());
                    // Do nothing on root element
                    if (selectedPath.getPathCount() > 1) {
                        // find the allowed actions, to know if a popup menu should be displayed and the content of the popup menu + action handlers
                        if (clickedItem.node instanceof Element) {
                            nodeActionAdd(mouseEvent, selectedRow, selectedPath, clickedItem, tree);
                        } else if (isLeaf) {
                            valueLeafActionEdit(mouseEvent, selectedPath, clickedItem, tree);
                        }/*w ww. j a v a 2s.co  m*/
                    }
                }
            }
        }
    };
    tree.addMouseListener(mouseAdapter);
}