Javafx Pagination

Asked

Viewed 294 times

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.

inserir a descrição da imagem aqui

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);

}

}

  • 3

    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].

  • I edited my question as requested. Thank you

1 answer

2


Pagination needs pages created by pageFactory to work properly. For this you use the function . setPageFactory(), as shown below:

pagination.setPageFactory(new Callback<Integer, Node>() {
       public Node call(Integer pageIndex) {
           // Crie um nó aqui
       }
   });

Basically this Callback will be executed by clicking on the page number (pageIndex), returning the corresponding Node.

Usually pagination is used to navigate through multiple pages (Nodes) where a single content has been divided into smaller parts. Maybe not ideal to pass item by item. I hope it still helps!

More information in the documentation of Pagination.

Examples of use: Using Javafx UI Controls: 25 Pagination Control.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.