简体   繁体   中英

How to fill a tablecolumn with an Arraylist in Javafx

I m trying to fill a table column with a String ArrayList. Normally i know how to fill it with object attributes but in my case i just need to fill it with Strings from an ArrayList. How can i do it ?

through the answer which i got still it doesnt seems to work :(

table = new TableView(FXCollections.observableArrayList(id));

            linkC.setCellValueFactory((p) -> {
                return new ReadOnlyStringWrapper(p.getValue());
            });

It's possible, it just may not be the best idea. Tables are designed to be used with a class of data. A single column table is just a list.

import java.util.ArrayList;
import java.util.Arrays;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;

public class TableTest extends Application {
    public static void main(String[] args) { launch(args); }

    @Override
    public void start(Stage stage) {
        TableView<String> tv = new TableView(FXCollections.observableArrayList(
                new ArrayList<String>(Arrays.asList(new String[]{
                    "a","bb","ccc","dddd","eeeee"
                }))));
        TableColumn<String, String> tc = new TableColumn<>("string");
        tc.setCellValueFactory((p) -> {
            return new ReadOnlyStringWrapper(p.getValue());
        });
        tv.getColumns().add(tc);
        stage.setScene(new Scene(tv));
        stage.show();
    }

}

example below:

List<Double> doubles = new ArrayList<Double>();
doubles.add(12.12d);
ObservableList<Double> names = FXCollections.observableArrayList(doubles);
ListView<Double> listView = new ListView<Double>(names);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM