JPopupMenu上的JMenuItems有时不会被绘制

时间:2015-10-23 17:01:12

标签: java swing jmenuitem jpopupmenu

我按下JButton时会显示一个JPopupMenu。此菜单包含一系列JMenuItems,每个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);
    }

}

只是为了澄清一下,这些动作工作得很好,JPopupMenu中的8次和所有JMenuItem都是正确绘制的,但我无法弄清楚为什么它们有时不会出现(无论列表是否已更改)或不)。任何帮助将不胜感激。

编辑: 好的,按照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);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

  

我按下JButton时会显示一个JPopupMenu。

首先,我遇到像这样的用户界面的问题。标准是在右键单击(在窗口中)时显示弹出窗口。遵循已知标准。阅读Bringing Up a Popup Menu上Swing教程中的部分,了解更多信息和工作示例。

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

而是使用Swing Timer

Timer设置为在显示菜单200ms后触发。在EDT上调用从Timer IS执行的代码。因此,您restart() Timer mousePressed。和stop() Timer中的mouseReleased