Artigos anteriores
A classe controller inclui uma ação para o botão remover.
<Button fx:id=”removeButton” mnemonicParsing=”false”
onAction=”#removeButtonAction” text=”Remover” />
O fx:id é para identificar o acesso ao botão. A onAction é o código para atribuir a ação do botão. Não há atalhos no teclado para acessar esse botão, então o mnemonicParsing tem o valor false.
Você não pode atualizar diretamente o SortedList, mas você pode aplicar mudanças a lista (ObservableList personList). A SortedList sempre mantém os elementos ordenados mesmo adicionando ou removendo itens.
@FXML
private void removeButtonAction(ActionEvent actionEvent){
personList.remove(selectedPerson);
}
Essa é ação para remover um item da lista.
Para evitar a chamada nula, utilize o código.
removeButton.disableProperty().bind( listPersons.getSelectionModel().selectedItemProperty().isNull());
Para aplicar essas mudanças no projeto tive que fazer a variável selectedPerson que está no método clicouNaPessoa ficar acessível a outros métodos da classe.
Para isso declarei a variável no começo da classe.

Programação do botão remover.
public void removeButtonAction(javafx.event.ActionEvent actionEvent) {
System.out.println("Remover>>> " + selectedPerson);
removeButton.disableProperty().bind(
listPersons.getSelectionModel().selectedItemProperty().isNull());
obsPerson.remove(selectedPerson);
}
Note que na Figura 1 o botão remover e criar estão declarados e precisar ser feito para que funcione. Só declare o botão remover por enquanto, retire o createButton.


Vou colocar os arquivos completos aqui das Scene1.fxml e FXMLController.java.
Scene1.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="610.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.br.com.cursojavanow.FXMLController">
<children>
<SplitPane dividerPositions="0.29797979797979796" prefHeight="400.0" prefWidth="600.0">
<items>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="326.0" prefWidth="169.0">
<children>
<ListView fx:id="listPersons" layoutX="5.0" layoutY="11.0" onMouseClicked="#clicouNaPessoa" prefHeight="368.0" prefWidth="163.0" />
</children>
</AnchorPane>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<GridPane prefHeight="398.0" prefWidth="417.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" maxWidth="202.0" minWidth="10.0" prefWidth="87.0" />
<ColumnConstraints hgrow="SOMETIMES" maxWidth="330.0" minWidth="10.0" prefWidth="330.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label prefHeight="17.0" prefWidth="59.0" text="Nome">
<padding>
<Insets left="10.0" />
</padding>
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label prefHeight="17.0" prefWidth="84.0" text="Sobrenome" GridPane.rowIndex="1">
<padding>
<Insets left="10.0" />
</padding>
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label prefHeight="17.0" prefWidth="90.0" text="Anotações" GridPane.rowIndex="2">
<padding>
<Insets left="10.0" />
</padding>
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<TextField fx:id="firstnameTextField" GridPane.columnIndex="1">
<opaqueInsets>
<Insets />
</opaqueInsets>
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin></TextField>
<TextField fx:id="lastnameTextField" GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin></TextField>
<TextArea fx:id="notesTextArea" prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.rowIndex="2">
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin></TextArea>
<ButtonBar prefHeight="40.0" prefWidth="200.0" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER">
<buttons>
<Button mnemonicParsing="false" text="Novo" />
<Button mnemonicParsing="false" text="Atualizar" />
<Button fx:id="removeButton" mnemonicParsing="false"
onAction="#removeButtonAction" text="Remover" />
</buttons>
<GridPane.margin>
<Insets right="70.0" />
</GridPane.margin>
</ButtonBar>
</children>
</GridPane>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</AnchorPane>
FXMLController.java
package main.java.br.com.cursojavanow;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import main.java.br.com.cursojavanow.model.Person;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
public class FXMLController implements Initializable{
@FXML
private ListView<Person> listPersons;
private List<Person> personList = new ArrayList<>();
private ObservableList<Person> obsPerson;
private Person selectedPerson;
@FXML
private Button removeButton;
@FXML
private TextField firstnameTextField, lastnameTextField;
@FXML
private TextArea notesTextArea;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
Person p1 = new Person("Michel", "Medeiros", "Pessoa 1");
Person p2 = new Person("Angela", "Ferraz", "Pessoa 2");
Person p3 = new Person("Jean", "Medeiros", "Pessoa 3");
personList.add(p1);
personList.add(p2);
personList.add(p3);
obsPerson = FXCollections.observableArrayList(personList);
listPersons.setItems(obsPerson.sorted());
listPersons.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
public void clicouNaPessoa(MouseEvent mouseEvent) {
listPersons.getSelectionModel().selectedItemProperty()
.addListener((obsPerson, oldValue, newValue) -> {
System.out.println("OLD: " + oldValue + ", NEW: " + newValue);
selectedPerson = newValue;
if (newValue != null) {
firstnameTextField.setText(selectedPerson.getFirstname());
lastnameTextField.setText(selectedPerson.getLastname());
notesTextArea.setText(selectedPerson.getNotes());
} else {
firstnameTextField.setText("");
lastnameTextField.setText("");
notesTextArea.setText("");
}
});
}
public void removeButtonAction(javafx.event.ActionEvent actionEvent) {
System.out.println("Remover>>> " + selectedPerson);
removeButton.disableProperty().bind(
listPersons.getSelectionModel().selectedItemProperty().isNull());
obsPerson.remove(selectedPerson);
}
}
Fonte: The Definitive Guide to Modern Java Clients with JavaFX





Deixe um comentário