gpt4 book ai didi

javafx - 引用 TreeView 中除 String 以外的类型的数据字段

转载 作者:行者123 更新时间:2023-12-02 05:00:21 29 4
gpt4 key购买 nike

我在 JAVA FX 2 站点上使用了示例树用户界面,并将类型从 String 更改为 PayString,一切似乎都运行良好。我的下一步是在 setCellFactory 'TextFieldTreeCellImpl' 中,我需要根据 PayString.level 的值(一个整数)为单元格分配不同的上下文菜单。

如何引用与当前单元格关联的数据字段的值。

下面是treeviewsample包中的两个源文件代码。我需要数据的位置用++++++++++++++++++++++++++标记,方便查找。

`/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package treeviewsample;

import java.util.Arrays;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

import javafx.beans.property.SimpleStringProperty;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;

public class TreeViewSample extends Application {

private final Node rootIcon;
private final Image depIcon;
List<Employee> employees = Arrays.<Employee>asList(
new Employee(1, "Ethan Williams", "Sales Department"),
new Employee(2, "Emma Jones", "Sales Department"),
new Employee(3, "Michael Brown", "Sales Department"),
new Employee(4, "Anna Black", "Sales Department"),
new Employee(5, "Rodger York", "Sales Department"),
new Employee(6, "Susan Collins", "Sales Department"),
new Employee(7, "Mike Graham", "IT Support"),
new Employee(8, "Judy Mayer", "IT Support"),
new Employee(9, "Gregory Smith", "IT Support"),
new Employee(10, "Jacob Smith", "Accounts Department"),
new Employee(11, "Isabella Johnson", "Accounts Department"));
TreeItem<PayString> rootNode;
Integer next;

public static void main(String[] args) {
Application.launch(args);
}

public TreeViewSample() {
this.next = 12;
this.depIcon = new Image(getClass().getResourceAsStream("department.png"));
this.rootIcon = new ImageView(new Image(getClass().getResourceAsStream("root.png")));
this.rootNode = new TreeItem<>(new PayString ("MyCompany Human Resources", 0,0), rootIcon);
}

@Override
public void start(Stage stage) {
rootNode.setExpanded(true);
for (Employee employee : employees) {
TreeItem<PayString> empLeaf = new TreeItem<>(new PayString(employee.getName(),2,employee.getId()));
boolean found = false;
for (TreeItem<PayString> depNode : rootNode.getChildren()) {
if (depNode.getValue().toString().contentEquals(employee.getDepartment())){
depNode.getChildren().add(empLeaf);
found = true;
break;
}
}
if (!found) {
TreeItem depNode = new TreeItem<>(new PayString(employee.getDepartment(),1,employee.getId()),
new ImageView(depIcon)
);
rootNode.getChildren().add(depNode);
depNode.getChildren().add(empLeaf);
}
}

stage.setTitle("Tree View Sample");
VBox box = new VBox();
final Scene scene = new Scene(box, 400, 300);
scene.setFill(Color.LIGHTGRAY);

TreeView<PayString> treeView = new TreeView<>(rootNode);
treeView.setEditable(true);
treeView.setCellFactory(new Callback<TreeView<PayString>,TreeCell<PayString>>(){
@Override
public TreeCell<PayString> call(TreeView<PayString> p) {
return new TextFieldTreeCellImpl();
}
});

box.getChildren().add(treeView);
stage.setScene(scene);
stage.show();
}

private final class TextFieldTreeCellImpl extends TreeCell<PayString> {

private TextField textField;
private ContextMenu addMenu = new ContextMenu();

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
* This is where I need to be able to extract the values in the current TreeCell<PayString>
* to be able to create the appropriate context menu.
* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

private String curr = getString();

public TextFieldTreeCellImpl() {
TreeItem<PayString> paystring = treeItemProperty().getValue();
MenuItem addMenuItem = new MenuItem("Add Employee");
MenuItem addMenuItem2 = new MenuItem("Add Address");
MenuItem addMenuItem3 = new MenuItem(curr);
addMenu.getItems().add(addMenuItem2);
addMenu.getItems().add(addMenuItem3);
addMenuItem.setOnAction(new EventHandler() {
@Override
public void handle(Event t) {
TreeItem newEmployee =
new TreeItem<>(new PayString ("New Employee",3,next));
next ++;
getTreeItem().getChildren().add(newEmployee);
}
});
}

@Override
public void startEdit() {
super.startEdit();

if (textField == null) {
createTextField();
}
setText(null);
setGraphic(textField);
textField.selectAll();
}

@Override
public void cancelEdit() {
super.cancelEdit();

setText(getString());
setGraphic(getTreeItem().getGraphic());
}

@Override
public void updateItem(PayString 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().isLeaf()&&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(new PayString (textField.getText(),3,next));
} else if (t.getCode() == KeyCode.ESCAPE) {
cancelEdit();
}
}
});

}

private String getString() {
return getItem() == null ? "" : getItem().toString();
}
}

public static class Employee {

private final SimpleStringProperty name;
private final SimpleStringProperty department;
private final Integer id;

private Employee(Integer id, String name, String department) {
this.name = new SimpleStringProperty(name);
this.department = new SimpleStringProperty(department);
this.id = new Integer(id);
}

public Integer getId() {
return id;
}

public String getName() {
return name.get();
}

public void setName(String fName) {
name.set(fName);
}

public String getDepartment() {
return department.get();
}

public void setDepartment(String fName) {
department.set(fName);
}
}
}

package treeviewsample;

import java.util.Objects;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

/**
*
* @author Len
*/
public class PayString {
private final String description;
private final Integer level;
private final Integer id;

public PayString(String description, Integer level, Integer id) {
this.id = id;
this.level = level;
this.description = description;
}

public int getId() {
return id;
}



public int getLevel() {
return level;
}


public String getDescription() {
return description;
}


@Override
public int hashCode() {
int hash = 7;
return hash;
}

public boolean contentEquals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final PayString other = (PayString) obj;
if (!Objects.equals(this.description, other.description)) {
return false;
}
return true;
}

@Override
public String toString() {
return description;
}


}
`

最佳答案

您始终可以通过调用来访问与您的手机关联的当前数据

PayString value = getTreeItem().getValue()

但是,单元格与其值之间没有 1:1 的关系。 JavaFX 创建它需要显示的尽可能多的 TreeCell。然后通过调用方法将它们动态分配给底层数据项

public void updateItem(PayString item, boolean empty) { ... }

您可以在此处设置依赖于当前数据项的上下文菜单。无论如何,您都会得到作为参数传入的数据项。

在您标记当前数据项的代码位置将为null

关于javafx - 引用 TreeView 中除 String 以外的类型的数据字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16890283/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com