fge*_*fge 0 java javafx fxml javafx-8
fxml文件如下(省略头文件):
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:id="pane"
fx:controller="com.github.parboiled1.grappa.debugger.mainwindow.MainWindowUi">
<top>
<MenuBar BorderPane.alignment="CENTER">
<Menu mnemonicParsing="false" text="File">
<MenuItem fx:id="loadInput" mnemonicParsing="false"
text="Load file" onAction="#loadFileEvent"/>
<MenuItem fx:id="parse" mnemonicParsing="false"
text="Parse" onAction="#parseEvent"/>
<MenuItem fx:id="closeButton" mnemonicParsing="false"
text="Close" onAction="#closeWindowEvent"/>
</Menu>
</MenuBar>
</top>
<center>
<SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0"
BorderPane.alignment="CENTER">
<SplitPane dividerPositions="0.5" orientation="VERTICAL">
<TreeView fx:id="traceTree" prefHeight="200.0"
prefWidth="200.0" editable="false"/>
<TextArea fx:id="traceDetail" prefHeight="200.0"
prefWidth="200.0"/>
</SplitPane>
<TextArea fx:id="inputText" prefHeight="200.0" prefWidth="200.0"/>
</SplitPane>
</center>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)
TreeView我可以毫无问题地设置根。树已更新,没有问题。
我遇到的问题是我无法设法在视图中的给定项目上触发事件。我尝试使用onMouseClicked简单的 System.out.println() 添加一个事件,无论我在树中单击哪个项目,我都可以看到该事件被触发。但我根本无法获取视图中已单击的项目。
我怎么做?
使用单元工厂向每个树单元注册鼠标侦听器。我不知道您的数据类型TreeView,但如果是的话,String它可能看起来像这样:
// Controller class:
public class MainWindowUi {
@FXML
private TreeView<String> traceTree ;
// ...
public void initialize() {
traceTree.setCellFactory(tree -> {
TreeCell<String> cell = new TreeCell<String>() {
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty) ;
if (empty) {
setText(null);
} else {
setText(item);
}
}
};
cell.setOnMouseClicked(event -> {
if (! cell.isEmpty()) {
TreeItem<String> treeItem = cell.getTreeItem();
// do whatever you need with the treeItem...
}
});
return cell ;
});
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4333 次 |
| 最近记录: |