JPopupMenu 上的 JMenuItems 有时不会被绘制

JMenuItems on a JPopupMenu sometimes aren't drawn

我有一个 JPopupMenu,它在按住 JButton 时显示。此菜单包含一系列 JMenuItems,每个都与一个有时会更改的操作相关联。我遇到的问题是,间歇性地没有绘制 JMenuItems,我只是得到一个灰色的 JPopupMenu,但是如果我将鼠标光标移到它们上面,这些项目就会出现。我认为问题可能出在更改后没有正确重新绘制组件,但测试表明即使项目没有更改,问题也会继续发生。这是相关代码:

    if (!listChanged) {
        myPopupMenu.show(myButton, x, y);
    } else {
        List<String> menuList = getMenuList();
        MyData data = getData();
        myPopupMenu.removeAll();

        for (int i = 0; i < menuList.size(); i++) {
            String name = menuList.get(i);
            JMenuItem item = new JMenuItem(new MyMenuAction(this, name,
                    data, i));
            item.addActionListener(this);
            myPopupMenu.add(item);
            myPopupMenu.validate();
        }
        myPopupMenu.repaint();
        myPopupMenu.show(myButton, x, y);
    }
...

private static class MyMenuAction extends AbstractAction {
    private MyClass parent;
    private int index;
    private MyData data;

    public MyMenuAction (MyClass parent, String name,
            MyData data, int index) {
        super(name);
        this.parent = parent;
        this.index = index;
        this.data = data;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object[] actionParameters;
        try {
            actionParameters = data.getParameters(index);
        } catch (ImmediateException e1) {
            log(e1.getMessage(), "Error");
            return;
        }
        parent.myButtonAction(actionParameters);
    }

}

澄清一下,操作运行良好,10 次中有 8 次 JPopupMenu 和所有 JMenuItems 都绘制正确,但我不明白为什么它们有时不出现(无论列表是否改变与否)。任何帮助将不胜感激。

编辑: 好的,根据 Andrew Thompson 的推荐,这是一个简短的完整示例。许多方法已被剥离,但基本方法仍然存在。只需单击并按住按钮 "SHOW MENU" 即可显示 JPopupMenu。由于问题是间歇性的,可能需要多做几次,直到出现问题。

package main;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.UIManager;

public class MyClass implements ActionListener, MouseListener {
    boolean listChanged = true;
    boolean mousePressed = false;
    long clickStart;
    JPopupMenu myPopupMenu;
    JButton myButton;
    JFrame myFrame;
    ArrayList<String> list;

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.start();
    }

    private void start() {
        myFrame = new JFrame();
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println(e.getClass().getName() + " " + e.getMessage());
        }

        startList();
        myButton = new JButton("SHOW MENU");
        myPopupMenu = new JPopupMenu();

        JToolBar toolbar = new JToolBar();
        toolbar.add(new JButton("Button1"));
        toolbar.add(new JButton("Button2"));
        toolbar.add(new JButton("Button3"));
        toolbar.add(new JButton("Button4"));
        toolbar.add(new JButton("Button5"));
        toolbar.add(myButton);

        myButton.addMouseListener(this);

        toolbar.setBorder(BorderFactory.createEtchedBorder(1));
        JPanel emptyPanel = new JPanel();

        myFrame.add(toolbar, BorderLayout.PAGE_START);
        myFrame.add(emptyPanel, BorderLayout.CENTER);
        myFrame.pack();
        myFrame.setExtendedState(myFrame.getExtendedState()
                | JFrame.MAXIMIZED_BOTH);
        myFrame.setVisible(true);

    }

    public void showMenu() {
        if (!listChanged) {
            myPopupMenu.show(myButton, 0, myButton.getHeight());
        } else {
            listChanged = false;
            List<String> menuList = getMenuList();
            MyData data = getData();
            myPopupMenu.removeAll();

            for (int i = 0; i < menuList.size(); i++) {
                String name = menuList.get(i);
                JMenuItem item = new JMenuItem(new MyMenuAction(this, name,
                        data, i));
                item.addActionListener(this);
                myPopupMenu.add(item);
                myPopupMenu.validate();
            }
            myPopupMenu.repaint();
            myPopupMenu.show(myButton, 0, myButton.getHeight());
        }
    }

    private void startList() {
        list = new ArrayList<String>();
        list.add("Item 1");
        list.add("Item 2");
        list.add("Item 3");
        list.add("Item 4");
        list.add("Item 5");
    }

    private List<String> getMenuList() {
        return list;
    }

    public void myButtonAction() {
        Object[] defaultParameters = getDefaultParameters();
        myButtonAction(defaultParameters);
    }

    private Object[] getDefaultParameters() {
        // Placeholder
        return null;
    }

    public void myButtonAction(Object[] actionParameters) {
        // Placeholder
    }

    private MyData getData() {
        // Placeholder
        return new MyData();
    }

    private void changeList(List<String> newList) {
        list.clear();
        list.addAll(newList);
        listChanged = true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // Placeholder
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (e.getSource() == myButton) {
            mousePressed = true;
            clickStart = System.currentTimeMillis();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    synchronized (this) {
                        while (mousePressed)
                            try {
                                this.wait(10);
                                if (System.currentTimeMillis() - clickStart > 300) {
                                    MyClass.this.showMenu();
                                    return;
                                }
                            } catch (InterruptedException e1) {
                                break;
                            }
                        MyClass.this.myButtonAction();
                    }
                }
            }).start();

        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        mousePressed = false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    private static class MyData {

        public Object[] getParameters(int index) {
            // Placeholder
            return null;
        }

    }

    private static class MyMenuAction extends AbstractAction {
        private MyClass parent;
        private int index;
        private MyData data;

        public MyMenuAction(MyClass parent, String name, MyData data, int index) {
            super(name);
            this.parent = parent;
            this.index = index;
            this.data = data;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Object[] actionParameters;
            try {
                actionParameters = data.getParameters(index);
            } catch (Exception e1) {
                System.out.println(e1.getMessage());
                return;
            }
            parent.myButtonAction(actionParameters);
        }
    }

}

I have a JPopupMenu that is displayed when a JButton is held down.

首先,我对这样的 UI 有疑问。标准是右键单击时显示弹出窗口(在 windows 中)。遵循已知标准。阅读有关 Bringing Up a Popup Menu 的 Swing 教程部分,了解更多信息和工作示例。

其次,我无法重现问题(无论我尝试多久)。随机问题通常是由 GUI 未在 EDT 上更新引起的。所以不要使用线程。

改为使用 Swing Timer

Timer 设置为在您显示菜单的 200 毫秒后触发。从计时器执行的代码是在 EDT 上调用的。所以你会 restart() TimermousePressed 中。 stop() Timer mouseReleased