Java Reference
In-Depth Information
You can create the
elementPopup
context menu in the
SketcherView
constructor and also provide the
framework for handling events from the menu items:
public SketcherView(Sketcher theApp) {
this.theApp = theApp;
MouseHandler handler = new MouseHandler(); // create the mouse
listener
addMouseListener(handler); // Listen for
button events
addMouseMotionListener(handler); // Listen for
motion events
// Add the pop-up menu items
JMenuItem moveItem = elementPopup.add(new JMenuItem("Move"));
JMenuItem deleteItem = elementPopup.add(new JMenuItem("Delete"));
JMenuItem rotateItem = elementPopup.add(new JMenuItem("Rotate"));
JMenuItem sendToBackItem = elementPopup.add(new
JMenuItem("Send-to-back"));
// Create the menu item listeners
moveItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// Handle the move event...
}
});
deleteItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// Handle the delete event...
}
});
rotateItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// Handle the rotate event...
}
});
sendToBackItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
sendToBack();
// Handle the
send-to-back event
}
});
}
Directory "Sketcher 7 with element context menus"
You add the menu items using the
add()
method that accepts a
JMenuItem
argument, and returns a refer-
ence to the
JMenuItem
object that it creates. You create each
JMenuItem
object in the expression that is
the argument to the
add()
method. You then create the listener object for each menu item in the pop-up