I was playing with JavaFX and crashed when I wanted to add paging to my little application.
I have listview
filled (as can be seen in the image), when I click on an item, your information is shown on the right.
AstheapplicationwasmodeledusingFXML,Iputthepagination
componentonthescreen,butitdoesnotshowanyactionuntilthatmoment.ThatispreciselywhatIwouldliketoadd.
Whenyouclickonthearrowsornumbersofpagination
,youwantthecorrespondingitemtobeselectedandconsequentlyyourinformationdisplayed.Howtodothis?
Asrequested,hereisthecodeformycontrolclass:Ps.:Thefullcodecanbefoundat link
public class ViewController implements Initializable {
Set<Pessoa> set = new LinkedHashSet<>();
@FXML
private TextField nomeTF;
@FXML
private TextField sobrenomeTF;
@FXML
private TextField celularTF;
@FXML
private TextField telTF;
@FXML
private ListView<Pessoa> pessoasLV;
@FXML
private Pagination pagination;
ObservableList<Pessoa> oList = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
nomeTF.setEditable(false);
sobrenomeTF.setEditable(false);
celularTF.setEditable(false);
telTF.setEditable(false);
populaLista();
pessoasLV.setCellFactory(preencheLista());
pessoasLV.getSelectionModel().
selectedItemProperty().addListener(selecionaLista());
pessoasLV.setItems(oList);
pagination.setPageCount(oList.size());
}
Callback<ListView<Pessoa>, ListCell<Pessoa>> preencheLista() {
return new Callback<ListView<Pessoa>, ListCell<Pessoa>>() {
@Override
public ListCell<Pessoa> call(ListView<Pessoa> param) {
return new ListCell<Pessoa>() {
@Override
protected void updateItem(Pessoa item, boolean empty) {
super.updateItem(item, empty);
if (!empty && item != null) {
setText(item.getNome() + " " + item.getSobrenome());
}
}
};
}
};
}
ChangeListener<Pessoa> selecionaLista() {
return new ChangeListener<Pessoa>() {
@Override
public void changed(ObservableValue<? extends Pessoa> observable, Pessoa oldValue, Pessoa newValue) {
nomeTF.setText(newValue.getNome());
sobrenomeTF.setText(newValue.getSobrenome());
celularTF.setText(Arrays.toString(newValue.getCelular()));
telTF.setText(Arrays.toString(newValue.getTelefoneRes()));
}
};
}
public void populaLista() {
set.add(new Pessoa("Maria", "Silva", "Rua A", 2, " "));
set.add(new Pessoa("Joana", "Carvalho", "Rua Paraíba", 452, "A "));
set.add(new Pessoa("sergio", "Pereira", "Av 2", 143, " "));
oList.addAll(set);
}
}