Java Reference
In-Depth Information
as an instance of an anonymous class. This simplifies the code for handling each event, because each
menu item has a dedicated listener object.
The only listener that is implemented at this point is for the Send To Back menu item, and this calls a
SketcherView method that you can implement like this:
private void sendToBack() {
if(highlightElement != null) {
SketcherModel sketch = theApp.getModel();
if(sketch.remove(highlightElement)) {
sketch.add(highlightElement);
} else {
JOptionPane.showMessageDialog(
SketcherView.this,"Element not found to
remove.",
"Remove Element from Sketch",
JOptionPane.ERROR_MESSAGE);
}
}
}
Directory "Sketcher 7 with element context menus"
To cover the ActionEvent and ActionListener types, modify the import statement for
java.awt.event.MouseEvent in the SketcherView.java file to the following:
import java.awt.event.*;
To display the context menu, you need to modify the code in the mousePressed() and
mouseReleased() methods in the MouseHandler inner class. Here's the former:
public void mousePressed(MouseEvent e) {
start = e.getPoint(); // Save the cursor
position in start
buttonState = e.getButton(); // Record which button
was pressed
if(showContextMenu(e)) {
start = null;
buttonState = MouseEvent.NOBUTTON;
return;
}
if(theApp.getWindow().getElementType() == TEXT) return;
Search WWH ::




Custom Search