T
T
Temirlan2016-04-20 19:34:36
css
Temirlan, 2016-04-20 19:34:36

How to split a ListView into cells?

You need to break the ListView into cells like in a TableView so that you can see the message, the status of sending it and the time. There is another option to use a tableView , but the column headings get in the way. Is it possible to remove them so that only the columns are visible?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kornachev, 2016-04-20
@Aukal

It can be like this:

public class Main extends Application {
    
    @Override
    public void start(Stage primaryStage) throws Exception{
        Pane root = new Pane();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();


        //подготавливаем список для листа
        ObservableList<TestEntity> list = FXCollections.<TestEntity>observableArrayList();
        
        list.addAll(
                new TestEntity(new Button("button1"), "str1"),
                new TestEntity(new Button("button2"), "str2"),
                new TestEntity(new Button("button3"), "str3"));


        ListView<TestEntity> listView = new ListView<TestEntity>(list);

        //устанавливаем фабрику для отрисовки ячейки.
        listView.setCellFactory(new Callback<ListView<TestEntity>, ListCell<TestEntity>>() {
            @Override
            public ListCell<TestEntity> call(ListView<TestEntity> param) {

                //тут нужно собрать объект ListCell, и вернуть его
                ListCell<TestEntity> listCell = new ListCell<TestEntity>(){

                    //отрисовка происходит здеся
                    @Override
                    protected void updateItem(TestEntity item, boolean empty) {


                        super.updateItem(item, empty);

                        if (empty || item == null) {
                            setText(null);
                            setGraphic(null);
                        } else {

                            //тут конструируешь все что захочешь
                            //или можешь подгружать fxml
                            HBox hBox = new HBox();


                            Label label = new Label(item.getString());
                            hBox.getChildren().addAll(item.getButton(), label);

                            //устанавливаем графику
                            setGraphic(hBox);

                        }
                    }
                };

                return listCell;



            }
        });

        root.getChildren().add(listView);

    }

    class TestEntity{
        Button button;
        String string;
        TestEntity(Button button, String str){
            setButton(button);
            setString(str);
        }

        public Button getButton() {
            return button;
        }

        public void setButton(Button button) {
            this.button = button;
        }

        public String getString() {
            return string;
        }

        public void setString(String string) {
            this.string = string;
        }
    }


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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question