我设置了一个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());
}
}
答案 0 :(得分:15)
将MouseListener添加到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);
}
}
});
答案 1 :(得分:0)
将此代码粘贴到“鼠标/鼠标单击侦听器方法”中
byte index = (byte)jList.getSelectedIndex();
System.out.println(index);
if(index > -1){
BTremoveImage.setEnabled(true);`
BTshowImage.setEnabled(true);
}