开发者

JPopupMenu not appearing?

I have a Java applet that will consist of seve开发者_Go百科ral popup menus that the user will have to interact with. However, the JPopupMenu won't show up when added. Here is my code:

public class Parser extends JApplet implements ActionListener {
    private static final long serialVersionUID = 1L;
    JPopupMenu deviceMenu;
    JButton downloadButton;
    Map <String, Object> deviceDict;

    public void init () {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    createGUI();
                }
            });
        } 
        catch (Exception e) { 
            System.err.println("createGUI didn't successfully complete");
        }
    }
    public void createGUI() {
        try {
            URL url = new URL("[URL]");
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    conn.getInputStream()));
            String inputLine;
            String xml = "";
            while ((inputLine = in.readLine()) != null) 
                xml = xml + inputLine;
        deviceDict = Plist.fromXml(xml);
        System.out.print(deviceDict);
        } 

        catch (XmlParseException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(480, 360));
        setSize(480, 360);
        Iterator <String> deviceIterator = deviceDict.keySet().iterator();
        deviceMenu = new JPopupMenu("Test");
        while (deviceIterator.hasNext()) {
            JMenuItem item = new JMenuItem(deviceIterator.next());
            deviceMenu.add(item);
        }
        add(deviceMenu);
    }
}

Any ideas why?


When do you want it to show up?
You need to call show() if you want to display the popup menu.
See this example and the one from oracle site.

BTW - from your question it seems JDialog


Had to use a JComboBox instead of JPopupMenu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜