Tải bản đầy đủ - 0 (trang)
1-13. Associating Keyboard Sequences to Applications

1-13. Associating Keyboard Sequences to Applications

Tải bản đầy đủ - 0trang

CHAPTER 1  JAVAFX FUNDAMENTALS



.text("New")

.accelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN))

.onAction(new EventHandler() {

public void handle(ActionEvent event) {

statusProperty.set("Ctrl-N");

}

})

.build();

menu.getItems().add(newItem);

MenuItem saveItem = MenuItemBuilder.create()

.text("Save")

.accelerator(new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN))

.onAction(new EventHandler() {

public void handle(ActionEvent event) {

statusProperty.set("Ctrl-S");

}

})

.build();

menu.getItems().add(saveItem);

menu.getItems().add(new SeparatorMenuItem());

MenuItem exitItem = MenuItemBuilder.create()

.text("Exit")

.accelerator(new KeyCodeCombination(KeyCode.X, KeyCombination.CONTROL_DOWN))

.onAction(new EventHandler() {

public void handle(ActionEvent event) {

statusProperty.set("Ctrl-X");

}

})

.build();

menu.getItems().add(exitItem);

primaryStage.setScene(scene);

primaryStage.show();

Figure 1-19 displays an application that demonstrates keyboard sequences or keyboard shortcuts.



50



CHAPTER 1  JAVAFX FUNDAMENTALS



Figure 1-19. Keyboard sequences



How It Works

Seeing that the previous recipe was a tad boring, I decided to make things a little more interesting. We

will be creating keyboard shortcuts using the new javafx.scene.input.KeyCodeCombination and

javafx.scene.input.KeyCombination classes. This recipe will display Text nodes onto the scene graph

when the user performs the key combinations. When displaying the Text nodes I applied an inner

shadow effect. The following code snippet creates a Text node with an inner shadow effect:

InnerShadow iShadow = InnerShadowBuilder.create()

.offsetX(3.5f)

.offsetY(3.5f)

.build();

final Text status = TextBuilder.create()

.effect(iShadow)

.x(100)

.y(50)

.fill(Color.LIME)

To create a keyboard shortcut you simply call a menu or button control’s setAccelerator() method.

In this recipe we use a Builder class and set the key combination using the accelerator() method. The

following code line specifies the key combinations for a control-N:

MenuItem newItem = MenuItemBuilder.create()

.text("New")

.accelerator(new KeyCodeCombination(KeyCode.N, KeyCombination.CONTROL_DOWN))



51



CHAPTER 1  JAVAFX FUNDAMENTALS



1-14. Creating and Working with Tables

Problem

You want to display items in a UI table control similar to Java Swing’s JTable component.



Solution

Create an application using JavaFX’s javafx.scene.control.TableView class. The TableView control

provides the equivalent functionality similar to Swing’s JTable component.

To exercise the TableView control you will be creating an application that will display bosses and

employees. On the left you will implement a ListView control containing bosses, and employees

(subordinates) will be displayed in a TableView control on the right.

Shown here is the source code of a simple domain (Person) class to represent a boss or an employee

to be displayed in a ListView or TableView control:

package javafx2introbyexample.chapter1.recipe1_14;

import

import

import

import



javafx.beans.property.SimpleStringProperty;

javafx.beans.property.StringProperty;

javafx.collections.FXCollections;

javafx.collections.ObservableList;



/**

*

* @author cdea

*/

public class Person {

private

private

private

private



StringProperty aliasName;

StringProperty firstName;

StringProperty lastName;

ObservableList employees = FXCollections.observableArrayList();



public final void setAliasName(String value) {

aliasNameProperty().set(value);

}

public final String getAliasName() {

return aliasNameProperty().get();

}

public StringProperty aliasNameProperty() {

if (aliasName == null) {

aliasName = new SimpleStringProperty();

}

return aliasName;

}



52



CHAPTER 1  JAVAFX FUNDAMENTALS



public final void setFirstName(String value) {

firstNameProperty().set(value);

}

public final String getFirstName() {

return firstNameProperty().get();

}

public StringProperty firstNameProperty() {

if (firstName == null) {

firstName = new SimpleStringProperty();

}

return firstName;

}

public final void setLastName(String value) {

lastNameProperty().set(value);

}

public final String getLastName() {

return lastNameProperty().get();

}

public StringProperty lastNameProperty() {

if (lastName == null) {

lastName = new SimpleStringProperty();

}

return lastName;

}

public ObservableList employeesProperty() {

return employees;

}

public Person(String alias, String firstName, String lastName) {

setAliasName(alias);

setFirstName(firstName);

setLastName(lastName);

}

}

The following is our main application code that displays a list view component on the left

containing bosses and a table view control on the right containing employees:

primaryStage.setTitle("Chapter 1-14 Working with Tables");

Group root = new Group();

Scene scene = new Scene(root, 500, 250, Color.WHITE);

// create a grid pane



53



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

1-13. Associating Keyboard Sequences to Applications

Tải bản đầy đủ ngay(0 tr)

×