1
I was playing with Javafx and stopped when I wanted to add paging to my small application.
I have a listview filled (as can be seen in the image), when I click on an item, its information is shown on the right.
As the application was modeled using FXML, I put the component pagination on the screen, however this does not present any action until now. Just what I would like to add.
When you click on the arrows or numbers of pagination, wanted the corresponding item to be selected and consequently its information shown. How to do this?
As requested, here is the code of my control class: Ps.: the full code can be found at https://github.com/marcelcunha/AgendaTest
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);
}
}

It is recommended then for this and even for future questions, always post the relevant code the doubt/ problem along with the question, in the format of a [mcve].
– user28595
I edited my question as requested. Thank you
– Marcel