tags:

views:

164

answers:

1

Basically i want to be able to allow the user to save bookmarks which are then put into a list on a submenu on a menubar. How would i go about programming a general function for any number of bookmarks that may be added, i basically want the items to put the URL into a textbox when clicked. Would i need to create a new class for this, or is there an inbuilt function?

My program is a simple RSS reader written in Java using Swing.

+2  A: 

You need to add a MenuListener to the menu item that you want to be dynamic. In the void menuSelected(MenuEvent e) method, implement the construction of the submenus. In a first implementation, you can first reset the content of your menu and then rebuid it from scratch instead of updating it :

JMenu menu = new JMenu("Bookmarks");
menu.addMenuListener(new MyMenuListener());

private class MyMenuListener implements MenuListener {

    public void menuCanceled(MenuEvent e) { }

    public void menuDeselected(MenuEvent e) { }

    public void menuSelected(MenuEvent e) {
        JMenu menu = (JMenu) e.getSource();
        populateWindowMenu(menu);
    }
}

void populateWindowMenu(JMenu windowMenu) {
    windowMenu.removeAll();
    // Populate the menu here
}
Denis R.