当前位置: 首页 > 知识库问答 >
问题:

我如何右键单击JavaJList选择该单元格就像左键单击一样?

益炜
2023-03-14

我设置了一个JPopupMenu,它将显示用户是否右键单击JList中的某个单元格,但现在必须先选择该单元格,才能显示JPopupMenu。我想知道如何在右键单击后选择该单元格,然后像现在一样显示JPopupMenu。当没有选择任何项目并且鼠标不在列表中的任何项目上时,我也会显示一个菜单。

代码:

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());
        }
    }

共有2个答案

薛坚
2023-03-14

粘贴此代码在鼠标/鼠标单击监听器方法

byte index = (byte)jList.getSelectedIndex();
System.out.println(index);
if(index > -1){
      BTremoveImage.setEnabled(true);`
      BTshowImage.setEnabled(true);
}
夹谷辰沛
2023-03-14

在JList中添加鼠标侦听器。比如:

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);
        }
    }

});
 类似资料:
  • 我知道如何通过鼠标左键点击所选项目来获取项目。我可以使用。 但我需要用鼠标右键点击项目。显示与单击的项目相关的弹出菜单。我试过这个: 但如果用户用右键点击该项,则会出现问题。右键单击不选择项目。如何按事件坐标选择项目或如何解决此问题?主要我需要得到的对象是点击没有选择项目,如果可能的话。

  • 我有一个JTree,我可以(ctrl)选择多个节点。当我右键点击时,我会弹出一个窗口,在那里我可以选择“刷新”。(本网站上还有其他问题解释如何做到这一点) 问题是,当我选择多个节点并右键单击时,只有右键单击的节点被选中,其他节点被取消选择。 例如,我想选择3个节点(叶),右键单击,选择刷新,仍然选择这3个节点。 有什么建议吗?谢谢。 例子:

  • 问题内容: 我正在尝试使用selenium进行右键单击,对此有任何想法吗? 问题答案: 我已经尝试过ActionSequence,而且效果很好。 找不到ContextClick函数,应使用click。 因此,应如下所示: 元素是您的Web元素,2表示右键。 要大致模拟JavaScript中的右键单击,请查看JavaScript模拟代码中的右键单击。

  • 我正在使用Selenium webdriver。我无法从右键单击打开的选项中选择(例如第二个)选项。 在我当前的代码中,我可以右键单击webElement,但无法从右键单击后打开的列表中选择选项,因为它会自动消失。 因此,有了这段代码,我可以右键单击,但右键单击菜单会自动消失。我想从右键菜单中选择第二个选项。 请帮助!!!

  • 问题内容: 我想知道是否有最佳实践/正确的方法来为React组件设置右键菜单。 我目前有这个… 可以,但是感觉有点混乱,我想知道是否还有其他方法可以使用,任何信息将不胜感激, 谢谢! 问题答案: 更新: 想通了-这是您可以做的 在渲染中,您可以将函数传递给onContextMenu,以使该react组件发生右键单击。