Select to view content in your preferred language

Right mouse click on a layer in a JLegend to bring up a popup menu

2692
1
05-14-2013 10:56 AM
dw2
by
Deactivated User
Not sure how to bring up a popup menu on right mouse clicking of a layer in the JLegend.  There are a few event handlers that could be extended.  Not sure which one and the logic to check which layer is selected.
0 Kudos
1 Reply
CarlosColón-Maldonado
Occasional Contributor III
I'm not sure what your requirements are, but you must modify the JLegend class found in the toolkit to get it to do what you want it to. You have access to its source code if you associate the source path to its referenced jar on your project via properties on eclipse.

the code you're interest in is in the internal LegendMouseListener class that gets added to the internal JTree that the JLegend uses:
 /**
  * This class is used to handle mouse click events in the JLegend control.
  * The mouse click event handler looks to see if the mouse click is over the
  * check box of a tree node and updates the visibility of the corresponding
  * layer as appropriate. Note that the actual drawing of the check box and
  * displaying of its state is handled by {@link LegendTreeCellRenderer}.
  */
 protected class LegendMouseListener extends MouseAdapter {
  int _hotspot = new JCheckBox().getPreferredSize().width;

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
   */
  @Override
  public void mousePressed(MouseEvent e) {
   TreePath selPath = _tree.getPathForLocation(e.getX(), e.getY());
   if (selPath != null && SwingUtilities.isRightMouseButton(e)) {
    _tree.setSelectionPath(selPath);
    Object userObject = ((DefaultMutableTreeNode) selPath
      .getLastPathComponent()).getUserObject();

    if (userObject instanceof Layer) {
     Layer layer = (Layer) userObject;
     JOptionPane.showMessageDialog(getParent(),
       "You right-clicked on Layer " + layer.getName());
    } else if (userObject instanceof LayerInfo) {
     LayerInfo info = (LayerInfo) userObject;
     JOptionPane.showMessageDialog(getParent(),
       "You right-clicked on LayerInfo " + info.getName());
    } else {
     // What else were you expecting?
    }
   } else {
    // Not a right-click
   }
   super.mousePressed(e);
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
   */
  @Override
  public void mouseClicked(MouseEvent e) {
   TreePath path = _tree.getPathForLocation(e.getX(), e.getY());

   if (path != null
     && e.getX() < _tree.getPathBounds(path).x + _hotspot) {
    Object userObject = ((DefaultMutableTreeNode) path
      .getLastPathComponent()).getUserObject();

    if (userObject instanceof Layer) {
     Layer layer = (Layer) userObject;
     if (SwingUtilities.isRightMouseButton(e)) {
      // handle right-click on Layer
      JOptionPane
        .showMessageDialog(
          getParent(),
          "You right-clicked on Layer "
            + layer.getName());
     } else {
      layer.setVisible(!layer.isVisible());
     }
    } else if (userObject instanceof LayerInfo) {
     LayerInfo info = (LayerInfo) userObject;
     if (SwingUtilities.isRightMouseButton(e)) {
      // handle right-click on LayerInfo
      JOptionPane.showMessageDialog(
        getParent(),
        "You right-clicked on LayerInfo "
          + info.getName());
     } else {
      info.setVisible(!info.isVisible());
     }
    }
    _tree.repaint();
   }
  }
 }


I simply added the mousePressed overriden method check for right-clicks. the source code is attached.
[ATTACH=CONFIG]24469[/ATTACH]

Good luck!
0 Kudos