[英]javafx TreeTableView string array columns
所有現有答案都使用 class object 來顯示多列。 我必須使用 class 嗎? 我可以只使用像 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);
}
}
文件格式
<?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>
永遠不要使用原始類型:正確參數化你的類型:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.