Java Reference
In-Depth Information
You implement a pop-up menu in Sketcher so it works in any environment. The logic in the mouse event
handlers gets a little convoluted because there are several possible combinations of states. Let's try to put it
together.
TRY IT OUT: Displaying a Pop-Up Menu
In Sketcher, the pop-up menu would sensibly operate in the area where the sketch is displayed — in other
words, triggering the pop-up menu has to happen in the view. Of course, you could have different pop-up
menus for other components in the application window, such as the toolbar.
Assuming you have already added the code to SketcherFrame that creates the pop-up menu as I dis-
cussed earlier, you just need to add a method to SketcherFrame to make the pop-up available to the
view:
// Retrieve the pop-up menu
public JPopupMenu getPopup() {
return popup;
}
Directory "Sketcher 5 displaying a font dialog"
Now the SketcherView object can get a reference to the pop-up menu object that is stored in the Sketch-
erFrame object by using the application object to get to this method.
To maintain proper cross-platform operation for Sketcher, you implement the pop-up triggering in both
the mousePressed() and the mouseReleased() methods in the MouseHandler inner class to Sketch-
erView . The mousePressed() method looks like this:
public void mousePressed(MouseEvent e) {
start = e.getPoint(); // Save the
cursor position in start
buttonState = e.getButton(); // Record which
button was pressed
if(e.isPopupTrigger()) {
theApp.getWindow().getPopup().show(SketcherView.this,
start.x, start.y);
start = null;
buttonState = MouseEvent.NOBUTTON;
return;
}
if(theApp.getWindow().getElementType() == TEXT) return;
if(buttonState == MouseEvent.BUTTON1) {
g2D = (Graphics2D)getGraphics(); // Get graphics
Search WWH ::




Custom Search