Question

I setup a JPopupMenu that will show if the user right-clicks on a cell within a JList, but right now that cell has to first be selected in order for the JPopupMenu to show. I am wondering how I can have that cell be selected upon right-click then show the JPopupMenu like it is now. I also show a menu when no item is selected and the mouse is not over any items in the list.

Code:

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.TransferHandler;

public class CardZone extends JList implements MouseListener{
    private static final long serialVersionUID = 1L;
    private DefaultListModel model;
    private TransferHandler handler;
    private JPopupMenu popupMenu = null;
    private Planeswalker owner;
    private String name;
    private CardImageCellRenderer cellRenderer;

    public CardZone(Planeswalker owner, String name) {
        this.owner = owner;
        this.name = name;

        cellRenderer = new CardImageCellRenderer(150);

        model = new DefaultListModel();
        setModel(model);
        setCellRenderer(cellRenderer);

        setDropMode(DropMode.INSERT);
        setDragEnabled(true);
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        handler = new CardZoneTransferHandler();
        setTransferHandler(handler);

        addMouseListener(this);

        ActionMap map = getActionMap();
        AbstractAction dummy = new AbstractAction() {
            private static final long serialVersionUID = 1L;

            @Override public void actionPerformed(ActionEvent e) {}
        };
        map.put(TransferHandler.getCutAction().getValue(Action.NAME),   dummy);
        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),  dummy);
        map.put(TransferHandler.getPasteAction().getValue(Action.NAME), dummy);

        setBorder(BorderFactory.createTitledBorder(name));

        setFixedCellWidth(150);
        setFixedCellHeight(150);
    }

    public void setPopupMenu(JPopupMenu menu){
        this.popupMenu = menu;
    }

// Mouse Listener Stuff
    public void check(MouseEvent e) {
        if (e.isPopupTrigger()) { //if the event shows the menu
            //Only show the list's popup menu if an item is not selected and positioned where the mouse was right-clicked
            int closestIndexToClick = locationToIndex(e.getPoint());
            Rectangle cellBounds = getCellBounds(closestIndexToClick, closestIndexToClick); 
            if( cellBounds != null && !cellBounds.contains(e.getPoint()) ){
                if( popupMenu != null )
                    popupMenu.show(this, e.getX(), e.getY()); //and show the menu
            }else{
                // If the mouse was clicked over a selected card then show the card's popup menu
                Card selectedCard = (Card)getSelectedValue();               
                JPopupMenu cardsMenu = null;
                if( selectedCard != null ){
                    cardsMenu = selectedCard.getPopupMenu();
                    if( cardsMenu != null )
                        cardsMenu.show(this, e.getX(), e.getY());
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent me) {       
    }

    @Override
    public void mouseEntered(MouseEvent me) {       
    }

    @Override
    public void mouseExited(MouseEvent me) {        
    }

    @Override
    public void mousePressed(MouseEvent me) {
        check(me);
    }

    @Override
    public void mouseReleased(MouseEvent me) {
        check(me);
    }
// End Mouse Listener Stuff
}


import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class Card extends JPanel{
    private static final long serialVersionUID = 1L;
    private String name;
    private String type;
    private String subType;
    private String manaCost;
    private String manaItProvides;
    private String flippedName;
    private Boolean isFlipped;
    private Boolean isTapped;
    private Vector<String> canFetch = null;
    private JPopupMenu popupMenu = null;
    private CardZone zone;

    public Card(String name, String type, String subType, String manaCost, String manaItProvided, Vector<String> canFetch) {
        this.name = name;
        this.type = type;
        this.subType = subType;
        this.manaCost = manaCost;
        this.manaItProvides = manaItProvided;
        this.flippedName = "";
        this.isFlipped = false;
        this.isTapped = false;

        this.canFetch = new Vector<String>();

        if( canFetch != null )
            this.canFetch.addAll(canFetch);

        addMouseListener(new PopClickListener(popupMenu));
    }       

    public String getManaItProvides(){
        return manaItProvides;
    }

    public int getManaCost(char color){
        int cost = 0;
        for( int i = 0; i < manaCost.length(); i++ ) {
            if( manaCost.charAt(i) == color )
                cost++;
        }
        return cost;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }

    public String getSubType() {
        return subType;
    }

    public Boolean isFlipped(){
        return isFlipped;
    }

    public Boolean isTapped(){
        return isTapped;
    }

    public String getFlippedName(){
        return flippedName;
    }

    public void unFlip(){
        isFlipped = false;
    }

    public void flip(String flippedName){
        this.flippedName = flippedName;
        isFlipped = true;
        zone.repaint();
    }

    public void tap(){
        isTapped = true;
    }

    public void untap(){
        isTapped = false;
    }

    public void swapTapped(){
        if(isTapped){
            untap();
        }else{
            tap();
        }
    }

    public Vector<String> getFetchables(){
        return canFetch;
    }

    public void setPopupMenu(JPopupMenu menu){
        this.popupMenu = menu;
    }

    public JPopupMenu getPopupMenu(){
        return popupMenu;
    }

    public void setCurrentCardZone(CardZone zone){
        this.zone = zone;
    }

    public CardZone getCurrentCardZone(){
        return this.zone;
    }
}

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

import javax.swing.JPopupMenu;

    class PopClickListener extends MouseAdapter {
        private JPopupMenu popupMenu;

        public PopClickListener(JPopupMenu popupMenu){
            this.popupMenu = popupMenu;
        }

        public void mousePressed(MouseEvent e){
            if (e.isPopupTrigger())
                doPop(e);
        }

        public void mouseReleased(MouseEvent e){
            if (e.isPopupTrigger())
                doPop(e);
        }

        private void doPop(MouseEvent e){
             if( popupMenu != null )
                 popupMenu.show(e.getComponent(), e.getX(), e.getY());
        }
    }
Was it helpful?

Solution

Add a MouseListener to the JList. Something like:

list.addMouseListener( new MouseAdapter()
{
    public void mousePressed(MouseEvent e)
    {
        if ( SwingUtilities.isRightMouseButton(e) )
        {
            JList list = (JList)e.getSource();
            int row = list.locationToIndex(e.getPoint());
            list.setSelectedIndex(row);
        }
    }

});

OTHER TIPS

paste this code in Mouse/Mouse Click Listener Method

byte index = (byte)jList.getSelectedIndex();
System.out.println(index);
if(index > -1){
      BTremoveImage.setEnabled(true);`
      BTshowImage.setEnabled(true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top