Java Reference
In-Depth Information
public Person(String firstName, String lastName, String phone) {
setFirstName(firstName);
setLastName(lastName);
setPhone(phone);
}
public String toString() {
return "Person: " + firstName.getValue() + " " + lastName.getValue();
}
}
Detecting When a Row Is Selected
To detect when the user selects a row in the
TableView
, the StarterApp program adds a
ChangeListener
to the
selectedItem
property of the table view's selection model. The code for accomplishing this is shown in this snippet
from Listing 6-6:
table.getSelectionModel().selectedItemProperty()
.addListener((ObservableValue observable, Object oldValue, Object newValue) -> {
Person selectedPerson = (Person) newValue;
System.out.println(selectedPerson + " chosen in TableView");
});
When the user selects a row, the lambda expression is invoked, which prints data from the underlying
Person
instance represented by that row. This is the behavior you observed in Step 7 of the previous exercise.
Now that we've explored some of the capabilities of the
TableView
, let's move on to the next tab, Accordion/TitledPane.
Creating an
Accordion
and Defining a
TitledPane
As you experienced in Step 9 of the exercise, the
Accordion
shown in Figure
6-4
contains some
TitledPane
instances,
each of which contains nodes and may be expanded and collapsed. The code that defines and populates the
Accordion
in the StarterApp program is shown in Listing 6-8.
Listing 6-8.
The
createAccordionTitledDemoNode()
Method Located in
StarterAppMain.java
Node createAccordionTitledDemoNode() {
TitledPane paneA = new TitledPane("TitledPane A", new TextArea("TitledPane A content"));
TitledPane paneB = new TitledPane("TitledPane B", new TextArea("TitledPane B content"));
TitledPane paneC = new TitledPane("TitledPane C", new TextArea("TitledPane C content"));
Accordion accordion = new Accordion();
accordion.getPanes().addAll(paneA, paneB, paneC);
accordion.setExpandedPane(paneA);
return accordion;
}
As shown in the following snippet from Listing 6-8, a
TitledPane
is typically given the
text
for its title, and a
Node
subclass (in this case a
TextArea
) for its
content
:
TitledPane paneA = new TitledPane("TitledPane A", new TextArea("TitledPane A content"));
...
accordion.setExpandedPane(paneA);
Search WWH ::
Custom Search