Loading...

[javaFX]TreeViewの練習

javaFXです。TreeViewの練習

Using JavaFX UI Controls: Tree View | JavaFX 2 Tutorials and Documentation
上記のリンクまんまだけど
とりあえずコントローラとして実装

package zzz.main; import java.net.URL; import java.util.ResourceBundle; import javafx.event.Event; import javafx.event.EventHandler; import javafx.fxml.Initializable; import javafx.scene.control.ContextMenu; import javafx.scene.control.MenuItem; 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.input.MouseEvent; import javafx.util.Callback; public class Controller implements Initializable { public TreeView<String> treeView; @Override public void initialize(URL arg0, ResourceBundle arg1) { { TreeItem<String> rootItem = new TreeItem<String>("Inbox", null); rootItem.setExpanded(true); for (int i = 1; i < 6; i++) { TreeItem<String> item = new TreeItem<String>("Message" + i); rootItem.getChildren().add(item); } this.treeView.setRoot(rootItem); this.treeView.setEditable(true); this.treeView .setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() { @Override public TreeCell<String> call(TreeView<String> p) { return new TextFieldTreeCellImpl(); } }); } } private final class TextFieldTreeCellImpl extends TreeCell<String> { private TextField textField; private ContextMenu addMenu = new ContextMenu(); @SuppressWarnings("unchecked") public TextFieldTreeCellImpl() { MenuItem addMenuItem = new MenuItem("Add Employee"); addMenu.getItems().add(addMenuItem); addMenuItem.setOnAction(new EventHandler() { public void handle(Event t) { TreeItem<String> newEmployee = new TreeItem<>( "New Employee"); getTreeItem().getChildren().add(newEmployee); } }); setOnDragDetected(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent mouseEvent) { } }); } @Override public void startEdit() { super.startEdit(); if (textField == null) { createTextField(); } setText(null); setGraphic(textField); textField.selectAll(); } @Override 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()); if (getTreeItem().getParent() != null) { setContextMenu(addMenu); } } } } 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(); } } }
リアクション: 
javafx 84562155939825945

コメントを投稿

ホーム item

このブログを検索

Random Posts

Popular Posts

Labels

ADS