Originally Posted by
copeg
If I understand your question, to dynamically add elements on the fly to a JMenu you can add a MenuListener to the JMenu. This will allow you to add/remove elements when the menu is selected/deselected.
Well, I don't really need to do it dynamically, but that could work. Here's an example of how I'm currently doing it:
JMenu mnPots = new JMenu("Pots");
menuBar.add(mnPots);
final JMenuItem mntmPot0 = new JMenuItem("Pot 0");
mntmPot0.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SpinCADBlock pcB = new Pot0CADBlock(50, 100);
dropBlock(panel, pcB);
}
});
mnPots.add(mntmPot0);
So I guess I already am adding them "on the fly", although this code is just in the frame initialization code. Thanks for the answer!