【发布时间】:2015-12-15 20:44:48
【问题描述】:
我正在尝试在 TableView 中插入一些值,但它不显示列中的文本,即使它们不是空的,因为有三行(与我在 TableView 中插入的项目数相同) ) 是可点击的。
我有类“ClientiController”,它是 fxml 文件的控制器,这些是 TableView 的声明和 tableView 的列。
@FXML // fx:id="clientitable"
private TableView clientitable; // Value injected by FXMLLoader
@FXML // fx:id="nome"
private TableColumn nome; // Value injected by FXMLLoader
@FXML // fx:id="id"
private TableColumn id; // Value injected by FXMLLoader
@FXML // fx:id="telefono"
private TableColumn telefono; // Value injected by FXMLLoader
我在 initialize() 方法上调用了一个名为 loadTable() 的函数,它将内容添加到 TableView。
loadTable 在同一个类“ClientiController”中,这是它的实现:
@FXML
void loadTable()
{
//Cliente
try {
List<String> clienti = (List<String>) fc.processRequest("ReadClienti");
ObservableList<String> clienteData = FXCollections.observableArrayList(clienti);
id.setCellValueFactory(new PropertyValueFactory<Cliente, String>("id"));
nome.setCellValueFactory(new PropertyValueFactory<Cliente, String>("nome"));
cognome.setCellValueFactory(new PropertyValueFactory<Cliente, String>("cognome"));
telefono.setCellValueFactory(new PropertyValueFactory<Cliente, String>("n_tel"));
System.out.println(clienteData);
clientitable.setItems(clienteData);
} catch (SecurityException | NoSuchMethodException
| ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在Cliente 类中,我有这些字符串:
private String id, nome, cognome, n_tel;
还有每个 String 元素的 get 和 set 函数,它们从数据库中获取值。
在这个类中,我还有 readAll 方法 (ReadClienti)。
在List<Cliente> clienti函数loadTable()中有readAll方法的结果,函数返回一个ArrayList<ArrayList<String>>类型。
如果我选择第一个 raw(即使我看不到列内的文本)然后像这样打印它,
ArrayList<String> person = (ArrayList<String>) clientitable.getSelectionModel().getSelectedItem();
System.out.println(person);
一切正常,它会打印我首先插入的值,所以我知道这些值实际上在那个表中。
我还尝试将此类中的私有 String 字符串更改为 SimpleStringProperty,就像我在此处的其他讨论中看到的那样,但没有任何改变。
如何让 tableView 显示其内容?
编辑: 我认为问题出在作业中
List<String> clienti = (List<String>) fc.processRequest("ReadClienti");
在 loadTable() 的第一个 raw 中,因为我无法将类型 ArrayList<ArrayList<String>> 转换为 List<String>。
但是ObservableList<String> 我必须在其中放置值以填充 ListView 仅获取 Lists 或简单的 ArrayLists 作为输入。
如何转换我的 ArrayList<ArrayList<String>> 变量以匹配 ObservableList 类型?
【问题讨论】:
-
您发布的代码sn-ps相互不兼容。如果
loadTable被执行,那么你说的代码会显示选中的项目,你会得到一个ClassCastException(因为Cliente不是ArrayList)。创建并发布minimal reproducible example。 -
我编辑了问题,发现我认为可能是唯一的问题,但我找不到解决方案(我在帖子末尾写了)
-
您需要决定如何表示您的数据并相应地修复您的类型。我可能会首先删除所有 raw type 声明,例如
private TableView clientiTable;应该是private TableView<Something> clientiTable;。编译器应该在这里给你警告。如果您进行这些更改,您将收到类型不兼容的编译错误,因此更容易找到和修复这些错误。如果没有minimal reproducible example,就不可能提供更多帮助。 -
我解决了在 Clienti 类中创建构造函数的问题,这样我就可以创建 Clienti 类型的对象并使用 get 函数引用它们。那件事在我的代码中丢失了。感谢您的回答!