gpt4 book ai didi

JavaFX TableView 不显示内容

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:23 24 4
gpt4 key购买 nike

我试图在 TableView 中插入一些值,但它不会在列中显示文本,即使它们不是空的,因为有三行(与我在 TableView 中插入的项目数相同) ) 是可点击的。

I can select raws but I can't see them

我有类“ClientiController”,它是 fxml 文件的 Controller ,这些是 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>>类型。

如果我选择第一个原始文件(即使我看不到列内的文本)然后像这样打印,

ArrayList<String> person =  (ArrayList<String>) clientitable.getSelectionModel().getSelectedItem();
System.out.println(person);

一切正常,它打印出我首先插入的值,所以我知道这些值实际上在该表中。

我还尝试将此类中的私有(private) String 字符串更改为 SimpleStringProperty,就像我在此处的其他讨论中看到的那样,但没有任何改变。

如何让 tableView 显示它的内容?

编辑:我认为问题出在作业上

            List<String> clienti = (List<String>) fc.processRequest("ReadClienti");

loadTable() 的第一个原始文件中,因为我无法转换类型 ArrayList<ArrayList<String>>进入List<String> .但是 ObservableList<String>我必须在其中放置值来填充 ListView 仅获取列表或简单的 ArrayLists 作为输入。如何转换我的 ArrayList<ArrayList<String>>变量匹配 ObservableList 类型?

最佳答案

您的“Cliente”类没有 getter 和 setter。还要注意参数的 getter 和 setter 的名称。 getter 和 setter 不应与上面声明的参数不同。
例如:

private String Clienteid;
private String ClienteName;

public getClientid(){
return Clienteid;
}

这行代码需要类的getters和setter...

id.setCellValueFactory(new PropertyValueFactory<Cliente, String>("id"));
nome.setCellValueFactory(new PropertyValueFactory<Cliente, String>("nome"));

关于JavaFX TableView 不显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32656447/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com