... colors = { "Blue", "Yellow", "Red" };
27 
28            ButtonGroup colorGroup = new ButtonGroup(); // manages color items
29            popupMenu = new JPopupMenu(); // create pop-up menu
30            items = new JRadioButtonMenuItem[colors.length];
31 
32            // construct menu item, add to pop-up menu, enable event handling
33            for (int count = 0 ; count < items.length; count++)
34            {
35               items[count] = new JRadioButtonMenuItem(colors[count]);
36               popupMenu.add(items[count]); // add item to pop-up menu
37               colorGroup.add(items[count]); // add item to button group
38               items[count].addActionListener(handler); // add handler
39            }
40 
41            setBackground(Color.WHITE);
42 
43            // declare a MouseListener for the window to display pop-up menu
44            addMouseListener(
45               new MouseAdapter() ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.