简体   繁体   中英

How to hide a JPopupMenu by pressing a button?

I'm making a program that has a popup menu with two buttons, one of which should close the popup menu, but I have no idea how to do that and googling hasn't gone too well.

I've tried using popup.hide() but then the menu wouldn't come back, despite doing so when I tried just moving the popup. It also required me to put a SuppressWarning in that case and it took a few seconds for it to close at all. Is there any better way of doing it?

I'm not sure what kind of code is relevant, but here's the relevant buttons and their roles in this(I skipped all the creating the GUI parts that didn't seem relevant, everything looks good and I know that the buttons are working): package test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

interface CustomButton {

    JButton create();

    void react(JPopupMenu popup, JFrame frame);
}

class ErrandsButton implements CustomButton {

    private JButton errands = new JButton("Errands");

    public JButton create() {
        return errands;
    }

    public void react(JPopupMenu popup, JFrame frame) {
        errands.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                popup.show(frame, 120, 65);
            }
        });
    }
}

class Test {

    static JFrame frame = new JFrame("List");
    static CustomButton errands = new ErrandsButton();
    static JButton cancelTask = new JButton("Cancel");
    static JPopupMenu popup = new JPopupMenu();

    static void cancelTask() {
        cancelTask.addActionListener(new ActionListener() {
            @SuppressWarnings("deprecation")
            public void actionPerformed(ActionEvent e) {
                popup.hide();
            }
        });
    }

    public static void main(String args[]) {
        createInterface();
        cancelTask();
        errands.react(popup, frame);
    }

    static void createInterface() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        JPanel popup1 = new JPanel();
        JPanel button = new JPanel();
        popup1.add(cancelTask);
        popup.add(popup1);
        frame.add(popup);
        button.add(errands.create());
        frame.getContentPane().add(BorderLayout.CENTER, button);
        frame.setVisible(true);
    }
}

使用popup.setVisible(true)和popup.setVisible(false)。

frame.add(popup); is the problem. Do not add a JPopupMenu to a Container. Instead, use setComponentPopupMenu .

Alternatively, you could do the work yourself by adding a MouseListener whose mousePressed, mouseReleased and mouseClicked methods call isPopupTrigger and show . (It is vital that you do this in all three of those methods—different platforms have different conditions for showing popup menus.)

But really, using setComponentPopupMenu is easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM