Page JavaFX

1

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

}

}

    
asked by anonymous 22.11.2016 / 23:33

1 answer

2

The pagination needs pages created by pageFactory to function correctly. For this you use the .setPageFactory () function, 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.

Normally paging is used to navigate through multiple pages (Nodes) where a single content has been divided into smaller parts. It may not be ideal to pass item by item. Hope it still helps!

More information in the Pagination documentation.

Examples of usage: Using JavaFX UI Controls: 25 Pagination Control .

    
22.06.2017 / 02:00