[英]change text of treeitem when click on button
Is there a way to change the text of a TreeItem of TreeView, when I click a button? 当我单击按钮时,是否可以更改TreeView的TreeItem的文本? I tried to do something like shown in the oracle example http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm but I don't want to change the TreeItem by click on it, but rather clicking on the button. 我试图做类似于oracle示例http://docs.oracle.com/javafx/2/ui_controls/tree-view.htm中所示的操作,但是我不想通过单击它来更改TreeItem,而是单击在按钮上。 In a second step, I want to work with context menu to open a window with a Textfield, where I can manually insert the text to change the treeitems naming. 在第二步中,我想使用上下文菜单打开一个带有Textfield的窗口,我可以在其中手动插入文本以更改treeitems的命名。
package treeviewexample;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class TreeViewExample extends Application {
@Override
public void start(Stage primaryStage) {
TreeItem root = new TreeItem ("root");
TreeItem item1 = new TreeItem ("Level1");
TreeItem item2 = new TreeItem ("Level1");
TreeItem item11 = new TreeItem ("Level2");
TreeView tree = new TreeView ();
item1.getChildren().add(item11);
tree.setRoot(root);
tree.getRoot().getChildren().addAll(item1, item2);
tree.getRoot().setExpanded(true);
StackPane rootPane = new StackPane();
tree.setEditable(true);
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>(){
@Override
public TreeCell<String> call(TreeView<String> param) {
return new TextFieldTreeCellImpl();
}
});
rootPane.getChildren().add(tree);
Button btn = new Button();
btn.setText("Change Name to 'TEST'");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
//CHANGE TEXT OF SELECTED TreeItem to "TEST"?
}
});
rootPane.getChildren().add(btn);
Scene scene = new Scene(rootPane);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public class TextFieldTreeCellImpl extends TreeCell<String> {
private TextField textField;
public TextFieldTreeCellImpl (){
}
@Override
public void startEdit() {
super.startEdit();
if (textField == null) {
createTextField();
}
setText(null);
setGraphic(textField);
textField.selectAll();
}
public void cancelEdit() {
super.cancelEdit();
setText((String) getItem());
setGraphic(getTreeItem().getGraphic());
}
@Override
public void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (isEditing()) {
if (textField != null) {
textField.setText(getString());
}
setText(null);
setGraphic(textField);
} else {
setText(getString());
setGraphic(getTreeItem().getGraphic());
}
}
};
private void createTextField() {
textField = new TextField(getString());
textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER) {
commitEdit(textField.getText());
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}
}
You can simply use setValue() method to change the text of TreeItem . 您可以简单地使用setValue()方法来更改TreeItem的文本。
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
desired_tree_item.setValue("TEST");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.