public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof JButton) { JButton b = (JButton) e.getSource(); if (b == robotButton) { // show popup menu for selecting robots robotMenu.show(b, b.getWidth(), 0); } else { if (!typeMap.containsKey(b)) // error? return; // retrieve the type of data selected retrieveType(typeMap.get(b)); } } else if (e.getSource() instanceof JMenuItem) { JMenuItem m = (JMenuItem) e.getSource(); if (!robotMap.containsKey(m)) // error? return; // select the given robot for future data retrievals selectedRobot = robotMap.get(m); robotButton.setText(selectedRobot.name()); robotMenu.setVisible(false); } }
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { boolean none = true; robotMap.clear(); for (RemoteRobot robot : net.getRobots()) { none = false; JMenuItem b = new JMenuItem(robot.name()); robotMap.put(b, robot); b.addActionListener(this); robotMenu.add(b); } if (none) robotMenu.add("None available"); }