我正在尝试创建一个TreeView显示目录内容的目录,例如:
ABC
BCD
123.php
Run Code Online (Sandbox Code Playgroud)
其中 ABC 和 BCD 都是目录。我觉得我错过了一些东西,因为TreeView在删除完整目录位置之前工作正常,但是一旦删除它,它就不会像上面那样显示。
public void displayTreeView(String inputDirectoryLocation, CheckBoxTreeItem<String> mainRootItem) {
// Creates the root item.
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<>(inputDirectoryLocation);
// Hides the root item of the tree view.
treeView.setShowRoot(false);
// Creates the cell factory.
treeView.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
// Get a list of files.
File fileInputDirectoryLocation = new File(inputDirectoryLocation);
File fileList[] = fileInputDirectoryLocation.listFiles();
// Loop through each file and directory in the fileList.
for (int i = 0; i < fileList.length; i++) …Run Code Online (Sandbox Code Playgroud) 我希望能够以节点为中心显示一个特定标签的节点 - 例如.圆形或矩形节点,例如标签位于中心.这似乎应该是微不足道的,我确信它是,但网上相对稀疏的文档/教程意味着我似乎无法找到答案!
目前我可以在节点上显示标签没问题(默认情况下它出现,所以标签的左上角在中心开始,这不是我想要的)或设置标签显示在节点的右侧(通过为"特定节点"设置标签,但不将其放在中间位置!任何人都能对这一个有所了解吗?
我在tableView中有这个自定义的CellFactory.滚动时,该列异常缓慢.任何理由都是这样,我该如何改进它.
lastTradeColumn.setCellFactory(
new Callback<TableColumn<Stock, Price>,TableCell<Stock, Price>>(){
@Override public TableCell<Stock, Price> call( TableColumn<Stock, Price> p ) {
TableCell<Stock, Price> cell = new TableCell<Stock, Price>() {
@Override public void updateItem(Price price, boolean empty) {
super.updateItem(price, empty);
if (price != null) {
VBox vbox = new VBox(5);
vbox.getChildren().add(new Label("£"+price.toString()));
if( price.getOldPrice() > price.getNewPrice()) {
vbox.setStyle("-fx-background-color:#EA2A15;");
}
else if( price.getOldPrice() < price.getNewPrice()) {
vbox.setStyle("-fx-background-color:#9CF311;");
}
setGraphic( vbox );
}
}
};
return cell;
}
});
Run Code Online (Sandbox Code Playgroud) 我最近在探索javafx时碰到了下面的代码,我看到ObservableList是一个接口,没有它的实现,你怎么能使用它的变量?很明显我在这里遗漏了什么,有人能指出我正确的方向吗?
Run Code Online (Sandbox Code Playgroud)List list = new ArrayList(); ObservableList observableList = FXCollections.observableList(list); observableList.addListener(new ListChangeListener() { @Override public void onChanged(ListChangeListener.Change change) { System.out.println("Detected a change! "); } });
我知道这不是一个很难的问题,但遗憾的是我是数学迟钝的.
我需要从已知起始点沿着已知角度绘制一条50像素的线到未知的终点.角度来自起点(400,400)和鼠标点击; 需要朝着鼠标单击绘制线条,但是朝向点击仅需50个像素.
我已经广泛地进行了谷歌搜索,并找到了许多解决方案,但它并不是为了我而聚集在一起.
这是我如何获得角度:
float angle = (float) Math.toDegrees(Math.atan2(400 - event.getY(), 400 - event.getX()));
float angleInDegrees = (angle + 270) % 360;
Run Code Online (Sandbox Code Playgroud)
"事件"是鼠标单击.
float endX = 250 + 50 * (float)Math.cos(angleInDegrees);
float endY 250 + 50 * (float)Math.sin(angleInDegrees);
line.setStartX(400);
line.setStartY(400);
line.setEndX(endX);
line.setEndY(endY);
Run Code Online (Sandbox Code Playgroud)
我发现的一切都围绕着Math.cos和Math.sin,但我仍然没有得到它.我认为这个问题与将弧度映射到场景坐标有关,但我不确定.所以人们,我以什么方式愚蠢?我很感激任何帮助.
以下代码应该在JavaFX中选择一个单词TextField:
public class NewFXMain extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final TextInputControl textField = new TextField("Hello World, World!");
Button button = new Button("select");
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
textField.positionCaret(0);
textField.selectNextWord();
System.out.println(textField.getSelectedText());
}
});
VBox root = new VBox();
root.getChildren().add(textField);
root.getChildren().add(button);
primaryStage.setScene(new Scene(root, 300, 100));
primaryStage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
它Hello在控制台中打印,但在界面中没有选择任何内容(突出显示).如果对a执行相同操作,TextArea则会正确选择文本.
(错误的)结果TextField:

并且(正确)的结果是TextArea:

这是怎么回事?!?
我正在使用JavaFX for UI.如何将Vbox布局的大小设置为窗口大小?我尝试了以下代码,但无法查看vbox中添加的组件.
VBox vbox = new VBox();
vbox.setPadding(new Insets(10, 10, 10, 10));
vbox.setSpacing(10);
Run Code Online (Sandbox Code Playgroud) 我需要一个集成的或嵌入式的java浏览器.我使用了javafx的WebEngine,它支持基本的css以及html和java脚本,但无法运行flash文件.任何方式这样做.
或者你知道哪个项目可以满足我的要求.
我想在我的新组件中添加自定义操作.这该怎么做?
示例代码:
零件
public class MyCustomComponent extends Region {
public MyCustomComponent(){
super();
this.setOnMouseClicked(new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent event) {
/* throw my custom event here and handle it in my FXML controller - but how? :-( */
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
调节器
public class MyController {
@FXML protected void myCustomAction(ActionEvent event) {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
FXML:
<BorderPane fx:controller="fxmlexample.MyController"
xmlns:fx="http://javafx.com/fxml">
<top>
<MyCustomComponent onAction="#myCustomAction">
</MyCustomComponent>
</top>
</BorderPane>
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助