在JPopupMenu菜单项打开时更新它们

时间:2015-03-18 11:48:26

标签: java swing jpopupmenu

在我的UI中,我有一个JPopMenu,其值为,

例如A,B,C

enter image description here

情景是,

  1. 我打开了JPopupMenu并保持打开状态。

  2. 在计时器运行的后端,它会频繁地将内容B更新为其他字母表。

    3.我希望JPopupMenu在保持打开状态时进行更新。

  3. 在当前行为中,如果我关闭并打开JPopupMenu,则会显示更新的值。

    我尝试了repaint(),但它没有做任何事情。

    这样做的最佳方法是什么?我是新手,请帮帮忙。

2 个答案:

答案 0 :(得分:0)

尝试使用带有.repaint()的.revalidate()可能有所帮助。   The docs建议每次像大小更改时调用revalidate方法,并使用repaint手动调用它似乎可以解决这些问题。

答案 1 :(得分:0)

菜单项可以在运行时更改其内容。如果没有看到您的代码,很难说出您做错了什么,但这是一个有效的例子:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class PopupTest {
    private static final String[] messages = {
        "You are today's 1000th user!",
        "You have won an internet!",
        "Claim your prize!"
    };

    private PopupTest() {
        JFrame frame = new JFrame("You have won");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel lbl = new JLabel("Check your prize!");
        frame.setLocationByPlatform(true);
        frame.add(lbl);
        frame.pack();

        final JPopupMenu menu = new JPopupMenu();
        final JMenuItem item = new JMenuItem(messages[0]);
        menu.add(item);
        menu.add(new JMenuItem("Another item that does not work"));

        final Timer timer = new Timer(1000, new ActionListener() {
            int count;

            @Override
            public void actionPerformed(ActionEvent e) {
                count++;
                count %= messages.length;
                item.setText(messages[count]);
            }
        });

        menu.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                timer.stop();
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
                timer.stop();
            }
        });

        lbl.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    menu.show(e.getComponent(), e.getX(), e.getY());
                    timer.start();
                }
            }
        });

        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PopupTest();
            }
        });
    }
}