[英]javafx TreeTableView string array columns
All existing answers were using a class object to display multiple columns.所有现有答案都使用 class object 来显示多列。 Do I have to use a class?我必须使用 class 吗? Can I just use a string array like C#'s ListViewItem?我可以只使用像 C# 的 ListViewItem 这样的字符串数组吗? If I can, how?如果可以,怎么做?
For example, display "hello" in the first column and "world" in the second column.例如,在第一列显示“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>
Never use raw types: parameterize your types properly:永远不要使用原始类型:正确参数化你的类型:
public class HelloController {
@FXML
private TreeTableView<String[]> mytree;
@FXML
private TreeTableColumn<String[], String> colFirst;
@FXML
private TreeTableColumn<String[], String> colSecond;
// ...
}
Then in the lambda expression, p
is a TreeTableColumn.CellDataFeatures<String[], String>
, so p.getValue()
is a TreeItem<String[]>
and p.getValue().getValue()
is the String[]
representing the row.然后在 lambda 表达式中, p
是TreeTableColumn.CellDataFeatures<String[], String>
,所以p.getValue()
是TreeItem<String[]>
并且p.getValue().getValue()
是String[]
表示行。
So you can do所以你可以做
@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.