Java Reference
In-Depth Information
To define the Ctrl+N accelerator key combination, the static keyCombination() method of the KeyCombination
class is used to create a KeyCombination instance. This instance is passed into the setAccelerator() method of the
MenuItem .
The onAction() event handler in the preceding snippet defines a lambda expression that is invoked when the
user selects the New menu item. The resulting message printed to the Java console is the one to which Step 2 of the
earlier exercise refers.
Creating a Toolbar
To create the toolbar, our StarterApp program defines a method that we've arbitrarily named createToolBar() ,
shown in Listing 6-4. This method leverages the Button , Separator , ToggleButton , and ToggleGroup classes, and
returns a ToolBar instance that contains the desired toolbar buttons.
Listing 6-4. The createToolBar() Method Located in StarterAppMain.java
ToolBar createToolBar() {
final ToggleGroup alignToggleGroup = new ToggleGroup();
Button newButton = new Button();
.setGraphic(new ImageView(new Image(getClass().getResourceAsStream
("images/paper.png"))));
newButton.setId("newButton");
newButton.setTooltip(new Tooltip("New Document... Ctrl+N"));
newButton.setOnAction(e -> System.out.println("New toolbar button clicked"));
Button editButton = new Button();
editButton.setGraphic(new Circle(8, Color.GREEN));
editButton.setId("editButton");
Button deleteButton = new Button();
deleteButton.setGraphic(new Circle(8, Color.BLUE));
deleteButton.setId("deleteButton");
ToggleButton boldButton = new ToggleButton();
boldButton.setGraphic(new Circle(8, Color.MAROON));
boldButton.setId("boldButton");
boldButton.setOnAction(e -> {
ToggleButton tb = ((ToggleButton) e.getTarget());
System.out.print(e.getEventType() + " occurred on ToggleButton "
+ tb.getId());
System.out.print(", and selectedProperty is: ");
System.out.println(tb.selectedProperty().getValue());
});
ToggleButton italicButton = new ToggleButton();
italicButton.setGraphic(new Circle(8, Color.YELLOW));
italicButton.setId("italicButton");
italicButton.setOnAction(e -> {
ToggleButton tb = ((ToggleButton) e.getTarget());
System.out.print(e.getEventType() + " occurred on ToggleButton "
+ tb.getId());
System.out.print(", and selectedProperty is: ");
System.out.println(tb.selectedProperty().getValue());
});
ToggleButton leftAlignButton = new ToggleButton();
leftAlignButton.setGraphic(new Circle(8, Color.PURPLE));
 
Search WWH ::




Custom Search