Dynamically populating a JMenu

I'm new to Java, but was hoping someone could point me in the right direction for populating a menu with items as it is about to be displayed. This would be a cascading menu off a JMenu off a JMenuBar. I have figured out how to populate the JMenu, but don't know which hook to use to get notification that the user is about to display the cascading menu.

[363 byte] By [DavidJohnMilesa] at [2007-9-30 0:44:22]
# 1

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import java.util.Random;

public class Test extends JFrame {

String[] nums = {"One","Two","Three","Four","Five","Six"};

JMenu jm = new JMenu("Menu");

Random r = new Random();

public Test() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container content = getContentPane();

JMenuBar jmb = new JMenuBar();

setJMenuBar(jmb);

jmb.add(jm);

jm.addMenuListener(new MenuListener() {

public void menuSelected(MenuEvent me) {

jm.removeAll();

for (int i=0; i<5; i++) {

jm.add(new JMenuItem(nums[r.nextInt(nums.length)]));

}

}

public void menuDeselected(MenuEvent me) {}

public void menuCanceled(MenuEvent me) {}

});

setSize(200, 200);

setVisible(true);

}

public static void main(String[] args) { new Test(); }

}

bbrittaa at 2007-7-16 5:16:33 > top of Java-index,Archived Forums,Swing...
# 2
Thank you, bbritta, that was exactly what I wanted!
DavidJohnMilesa at 2007-7-16 5:16:33 > top of Java-index,Archived Forums,Swing...