2016-11-30 73 views
0

如何使用List填充javafx中CellValueFactory的TableColumns单元格?我有一列特定的整数,我想在我的列中显示,我不知道如何正确使用setCellValueFactory来显示我已经存储在这个列表中的整数。使用List填充TableColumn CellValueFactory <Integer>

package com.editor.controller; 

import java.util.ArrayList; 
import java.util.List; 

import com.editor.drops.DropManager; 
import com.editor.drops.DropTable; 

import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.fxml.FXML; 
import javafx.scene.control.TableColumn; 
import javafx.scene.control.TableView; 

public class EditorController { 

@FXML 
private TableView<Integer> idTable; 

@FXML 
private TableColumn ids; //What goes here exactly? <P, S> ? 

private static final ObservableList<Integer> NPCS = FXCollections.observableArrayList(); 

public static final List<DropTable> TABLES = new ArrayList<>(); 

@FXML 
private void initialize() { 
     DropManager.loadDrops(); 
     TABLES.forEach(table -> { 
      table.getIds().forEach(id -> NPCS.add(id)); 
     }); 
} 
} 

回答

1

你需要创建一个类ID(整数)的性质,它应该看起来像:

public class SomeClass { 
    private int id; 

public int getId() { 
    return pid; 
} 

public void setId(int id) { 
    this.id = id; 
} 
} 
public class EditorController { 

@FXML 
private TableView<SomeClass> idTable; 

@FXML 
private TableColumn<SomeClass, Integer> ids; 
private static final ObservableList<Integer> NPCS = FXCollections.observableArrayList(); 

public static final List<DropTable> TABLES = new ArrayList<>(); 

@FXML 
private void initialize() { 
    DropManager.loadDrops(); 
    TABLES.forEach(table -> { 
     table.getIds().forEach(id -> NPCS.add(id)); 
    }); 
ids.setCellValueFactory(new PropertyValueFactory<SomeClass, Integer>("id")); 
} 
}