所有现有答案都使用一个类对象来显示多个列。我必须上一堂课吗?我可以使用像C#的ListViewItem这样的字符串数组吗?如果可以的话,如何使用?
例如,在第一列中显示"hello“,在第二列中显示"world”。
public class HelloController {
@FXML
private TreeTableView mytree;
@FXML
private TreeTableColumn colFirst;
@FXML
private TreeTableColumn colSecond;
@FXML
void initialize()
{
TreeItem<String[]> item = new TreeItem<String[]>(new String[]{"hello", "world"});
colFirst.setCellValueFactory((CellDataFeatures<Object, String[]> p)
-> new ReadOnlyStringWrapper(p.getValue().toString()));
mytree.setRoot(item);
}
}fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<VBox alignment="CENTER" spacing="20.0" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.example.fx2.HelloController">
<TreeTableView fx:id="mytree" prefHeight="200.0" prefWidth="200.0">
<columns>
<TreeTableColumn id="colFirst" prefWidth="75.0" text="First" />
<TreeTableColumn id="colSecond" prefWidth="75.0" text="Second" />
</columns>
</TreeTableView>
</VBox>发布于 2022-02-02 16:17:03
不要使用原始类型:正确地参数化您的类型:
public class HelloController {
@FXML
private TreeTableView<String[]> mytree;
@FXML
private TreeTableColumn<String[], String> colFirst;
@FXML
private TreeTableColumn<String[], String> colSecond;
// ...
}然后在lambda表达式中,p是TreeTableColumn.CellDataFeatures<String[], String>,所以p.getValue()是TreeItem<String[]>,p.getValue().getValue()是表示行的String[]。
所以你可以
@FXML
void initialize() {
TreeItem<String[]> item = new TreeItem<String[]>(new String[]{"hello", "world"});
colFirst.setCellValueFactory(p
-> new ReadOnlyStringWrapper(p.getValue().getValue()[0]));
mytree.setRoot(item);
}https://stackoverflow.com/questions/70958481
复制相似问题