2 Replies Latest reply on Dec 30, 2011 6:54 PM by 827294

    Right Clicking JList Elements

      I must preface this thread with the following: I have looked far and wide for answers and have come up dry, despite the Java Tutorials, other threads I looked through, threads that exist about this very issue that I apparently cannot locate, and all applicable APIs.

      My issue is regarding the JList and right clicking within it in order to bring up a JPopupMenu; in turn, its JMenuItems should be able to alter the contents of the JList as defined in the subsequent, applicable code (whether that be deleting, changing properties of the item in question, or replacing it with another from some other source). I have come across the getComponent() method (<I>not</I> a getJComponent() method, which doesn't exist, except for in an Interface JBOField...whatever that is...). And though JList is a type of component, it would seem that its elements, on which I'd like to be able to right click, are not actual components; rather, they would be handled by a ListSelectionModel, or so I think. That said, the ListSelectionModel may aid in changing list contents, but will not help in the actual selection of those by right clicking.

      Nevertheless, here is the code in question (with the appropriate and hopefully helpful annotations):
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;
      public class IngredientsListPopupMenu implements ActionListener {
           // constants for JPopupMenu
           private static final String MENU_NAME = "Selected Ingredients List";
           private static final String ITEM_1 = "Edit Quantity";
           private static final String ITEM_2 = "Delete Ingredient";
           private static final String ITEM_3 = "Replace Ingredient";
           private JPopupMenu listPopupMenu;
           /* all of this "hubbub" below is part of the formatting I learned for 
                  classes with multiple constructors perhaps it's considered too cumbersome?  */
           public IngredientsListPopupMenu() {
                this(new JPopupMenu(MENU_NAME));
           public IngredientsListPopupMenu(JPopupMenu jpm) {
           public void createPopupMenu(JPopupMenu jpm) {
                this.listPopupMenu = jpm;
              private JMenuItem createMenuItem(String s) {
                JMenuItem jmi = new JMenuItem(s);
                return jmi;
           /* listens for mouse activity and shows menu if not already open
               otherwise detects mouse activity for selecting menu items 
                  AND here is where it gets "hairy"! */
           private Component selectedComponent; /* not JComponent due to method call below. 
              I'm led to believe that there's nothing wrong with this variable type in and of itself, 
              whether or not I'm dealing with "Components" v. "JComponents" */
           private MouseListener mouseListener = new MouseAdapterTest();          
           class MouseAdapterTest extends MouseAdapter {     
                public void mousePressed(MouseEvent e) {
                public void mouseClicked(MouseEvent e) {
                public void mouseReleased(MouseEvent e) {
                private void checkPopup(MouseEvent e) {
                     if (e.isPopupTrigger()) {
                          selectedComponent = e.getComponent(); /* gets the type of (in this
                                      case) JComponent, which is rather general (and probably meant more
                                      for when one is dealing with more than one type of component?), as I 
                                      do not know how to refer to an element in a JList here with this particular
                                      method, which is probably not the way to go... */
                          listPopupMenu.show(e.getComponent(), e.getX(), e.getY()); 
           public void actionPerformed(ActionEvent e) { // applies action on selected JComponent from checkPopup method above
                String selection = e.getActionCommand(); // assigns JPopupMenu's selected text to a String
                /* empty conditional statements due to them not being completely necessary 
                          at this point, as I'd like to make sure the JPopupMenu displays properly before
                          writing the code for them */
                      if (selection.equals(ITEM_1)) {
                     // open up dialog to edit quantity
                } else if (selection.equals(ITEM_2)) {
                     // delete ingredient from JList
                } else if (selection.equals(ITEM_3)) {
                     // open up dialog to replace ingredient with another
           public JPopupMenu getMenu() { // for a JScrollPane class
                return this.listPopupMenu;
      In closing, I guess my question is: how do I take what I've got here and get that JPopupMenu to work with JList elements?