如何覆盖JPopupMenu方法显示?

时间:2015-04-15 01:00:15

标签: java swing override jlist jpopupmenu

我的目标是在右键单击后突出显示jlist项目然后显示jpopupmenu .. 我读到了重写方法show的建议是这样的.. 在我的情况下,我在课堂开头宣布我的jpopupmenu

     JPopupMenu popupMenu = new JPopupMenu();
     JMenuItem masterlist,settings;

然后我有一个方法来设置我的menuItems

    public void setPopupMenu(int depth//ignore this var){
    //popupMenu = new JPopupMenu();
    popupMenu.removeAll();

        masterlist = new JMenuItem("Masterlist");
        settings = new JMenuItem("Settings");

        //action for marsterlist
        masterlist.addActionListener(new ActionListener(){
            //stuff here
            }
        });
        //action for settings
        settings.addActionListener(new ActionListener(){
            //stuff here
            }
        });

        popupMenu.add(masterlist);
        popupMenu.add(settings);
 }

我的名单采用不同的方法

list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
list.setComponentPopupMenu(popupMenu);

我尝试将它放在我的列表的鼠标添加器上,但弹出菜单首先触发并忽略突出显示...

   if ( SwingUtilities.isRightMouseButton(mouseEvent) )//highlight the right clicked item
                 {

                    int row = list.locationToIndex(mouseEvent.getPoint());
                    list.setSelectedIndex(row); 
                    String val = (String)list.getSelectedValue();
                    System.out.println(val);
                 }

我知道覆盖就是这样的

  popupmenu = new JPopupMenu(){
  @Override
  public void show(){}
  }

但是我不能这样做因为我在一个方法上操纵menuitems ... 还是有任何其他人可以建议的方法......

1 个答案:

答案 0 :(得分:2)

而不是尝试修改JPopupMenu的状态,为什么不在检测到右键单击时简单地修改菜单项的状态...

JList with right mouse selection and popup

所以,基本上,我使用Actions API来定义JPopupMenu的菜单项,这样就可以将ListSelectionListener注册到基础JList。 ..

public class ShowItemAction extends AbstractAction {

    private JList list;

    public ShowItemAction(JList list) {
        this.list = new JList();
        putValue(NAME, "Showing ...");

        list.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    int index = list.getSelectedIndex();
                    String value = list.getSelectedValue().toString();
                    putValue(NAME, "Showing " + value + " @ " + index);
                }
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // The actual action to be performed when selected
    }

}

所有这一切都是,当选择更改时,更改操作的文本(NAME),根据当前选定的行更改菜单项的文本。

当我创建JList时,我通过JPopupMenu方法为其分配setComponentPopupMenu,这意味着我不再需要关心它,它将根据当前的外观要求

JList list = new JList(model);
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(new ShowItemAction(list));
list.setComponentPopupMenu(popupMenu);

然后我向MouseListener添加JList,当您右键点击JList时,我会用它来更改行选择...

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

这并非完全傻瓜式的证明,就像您在弹出窗口显示时右键单击JList一样,MouseListener似乎没有得到通知:P

可运行的例子......

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.NAME;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                DefaultListModel model = new DefaultListModel();
                model.addElement("One");
                model.addElement("Two");
                model.addElement("Three");
                model.addElement("Four");
                JList list = new JList(model);
                JPopupMenu popupMenu = new JPopupMenu();
                popupMenu.add(new ShowItemAction(list));
                list.setComponentPopupMenu(popupMenu);

                list.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mousePressed(MouseEvent e) {
                        if (SwingUtilities.isRightMouseButton(e)) {
                            int row = list.locationToIndex(e.getPoint());
                            list.setSelectedIndex(row);
                        }
                    }

                });

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(list));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ShowItemAction extends AbstractAction {

        private JList list;

        public ShowItemAction(JList list) {
            this.list = new JList();
            putValue(NAME, "Showing ...");

            list.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        int index = list.getSelectedIndex();
                        String value = list.getSelectedValue().toString();
                        putValue(NAME, "Showing " + value + " @ " + index);
                    }
                }
            });
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        }

    }

}