javaswingmenujframemenuitem

Get selected menu item & display the corresponding item on frame


I have 3 conditions (3 menu items in menu). In those 3 items, how to get one menu item selected as soon as the JFrame opens and displays the content of that item by default?

Later on, if we select other menu items, then corresponding contents on the JFrame will be displayed as usual.

I have written a very lengthy code for this GUI, so unable to paste here. But got stuck up at this point.


Solution

  • Simply invoke the actionPerformed() method of your ActionListener after your GUI is fully constructed. This is particularly easy if you have implemented the Action interface, as shown in How to Use Actions.

    Using this example, add the following line near setVisible() to simulate adding a few random nodes to the graph:

    gp.control.random.actionPerformed(new ActionEvent(gp, 0, null));
    

    To simulate clicking a button, this line simulates adding a selected node:

    gp.control.defaultButton.doClick();