JavaFX ListView com colunas

Olá, preciso criar uma lista com colunas e que apareça um item por coluna, semelhante ao exemplo abaixo:

lista%20com%20colunas

Tem como usar algum componente em JavaFX para fazer isso no qual os itens sejam clicáveis? Agradeço desde já.

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class ListView1 extends Application {

    private static class Car {
        public String carro;
        public String motor;
        public String combustivel;

        public Car(String carro, String motor, String combustivel ) {
            this.carro = carro;
            this.motor = motor;
            this.combustivel = combustivel;
        }

    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage arg0) throws Exception {
        ListView<Car> plateList = new ListView<Car>();
        plateList.setCellFactory(new Callback<ListView<Car>, ListCell<Car>>() {

            @Override
            public ListCell<Car> call(ListView<Car> param) {
                ListCell<Car> cell = new ListCell<Car>() {
                    HBox hbox = new HBox(5);                    
                    Button bcarro = new Button();
                    Button bmotor = new Button();
                    Button bcombustivel = new Button();
                    {
                      bcarro.setPrefWidth(100);
                      bmotor.setPrefWidth(60);
                      bcombustivel.setPrefWidth(90);
                      hbox.getChildren().addAll(bcarro, bmotor, bcombustivel );  
                    } 

                    @Override
                    protected void updateItem(Car item, boolean empty) {
                        super.updateItem(item, empty);
                        if (item != null) {
                            bcarro.setText(item.carro);
                            bmotor.setText(item.motor);
                            bcombustivel.setText(item.combustivel);
                            setGraphic(hbox);
                             
                        } else {
                            setGraphic(null);
                            setText("");
                        }
                    }
                };
                return cell;
            }
        });
        Button delete = new Button("Delete");
        ObservableList<Car> sample = FXCollections.observableArrayList();
        sample.add(new Car("Corsa", "1.4", "gasolina"));
        sample.add(new Car("Gol", "1.0", "alcool"));
        sample.add(new Car("Ford KA", "1.0", "flex"));

        delete.setOnAction((e) -> {
            plateList.getItems().remove(plateList.getSelectionModel().getSelectedItem());
            ObservableList<Car> t = plateList.getItems();
            plateList.setItems(t);
        });

        plateList.setItems(sample);
        arg0.setScene(new Scene(new VBox(plateList, delete), 380, 350 ) );
        arg0.show();
    }
}

Bons Codigos

1 curtida

Muito obrigado por compartilhar, vlw mesmo

1 curtida