2

I have a Java applet that will consist of several 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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Jumhyn
  • 6,687
  • 9
  • 48
  • 76

2 Answers2

3

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

MByD
  • 135,866
  • 28
  • 264
  • 277
  • I see. I was confused about what JPopupMenu actually does. If i want the kind of menu hat essentially has a button that when clicked displays a JPopupMenu, is there an Object for that or will I have to just make it be a button? – Jumhyn May 14 '11 at 21:31
  • If I get you right - You will need to create each of them using JMenuItem. – MByD May 14 '11 at 21:36
  • What I want is something that will represent like this: http://i.imgur.com/VCal4.png , where clicking on it will show the JPopupMenu. – Jumhyn May 14 '11 at 21:58
  • This one is called JComboBox. – MByD May 14 '11 at 22:02
  • that's JSpinner http://download.oracle.com/javase/tutorial/uiswing/components/spinner.html – mKorbel May 14 '11 at 22:39
  • @mKorbel - unless I am blind - this is JComboBox :) – MByD May 14 '11 at 22:41
  • @MByD @mKorbel I agree with both of you guys this is a JComboBox that looks like JSpinner since it is apple customised combobox. :) – Boro May 14 '11 at 22:49
  • @MByD +1 for answer and comments. Good stuff. – Boro May 14 '11 at 22:51
  • @ MByD the why OP find out JPopupMenu funcionalities for something what s/he wants, and we are now voting if is that "half JComboBox/half JSpinner" :-) – mKorbel May 14 '11 at 22:55
  • @MByD it is all please my friend. PS: you are like a lightning on all the questions. Your average response time is less then 5 mins. Good job. :) – Boro May 14 '11 at 22:56
  • @Boro mKorbel - `:D` @Boro - Thanks, I think I should get a life :) – MByD May 14 '11 at 22:59
  • @MByD Knowledge is power. Having brains that help you to sort out problems, like lots of people here on stack, helps lots to many in an unmeasurable way. Keep up the good work. It is appreciated. PS: the real life can wait :) – Boro May 14 '11 at 23:04
  • @mKobel: For reference, [here](http://stackoverflow.com/questions/2010098/jspinner-date-editor-in-buddhist-calendar/2010819#2010819) is a `JSpinner` in the same L&F, `aqua`. – trashgod May 15 '11 at 01:06
  • @ MByD agreed, hmmm link http://code.google.com/p/seaglass/ from todays thread http://stackoverflow.com/questions/6008058/customizing-jtextfield , one big sorry man – mKorbel May 15 '11 at 16:47
0

Had to use a JComboBox instead of JPopupMenu

Jumhyn
  • 6,687
  • 9
  • 48
  • 76