我有一个可编辑的秋天树,我想保存并加载这里是我的代码
public class Muddassir extends Application
{
private BorderPane root;
TreeView<String> tree;
TreeItem<String> module,unite,translateA ,translateB,rotate;
@Override
public void start(Stage primaryStage) throws Exception
{
root = new BorderPane();
root.setLeft(getLeftHBox());
root.setTop(getTop());
Scene scene = new Scene(root, 900, 500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
}
private VBox getLeftHBox()
{
TreeItem<String> rootnode = new TreeItem<String>("Library");
rootnode.setExpanded(true);
module = makeBranch("module",rootnode);
makeBranch("Parameter X",module);
unite = makeBranch("unite",module);
makeBranch("Parameter uX",unite);
translateA = makeBranch("translateA",unite);
makeBranch("Parameter taX",translateA);
translateB = makeBranch("translateB",unite);
makeBranch("Parameter tbX",translateB);
rotate = makeBranch("rotate",translateB);
makeBranch("Parameter RX",rotate);
tree= new TreeView<>(rootnode);
tree.setEditable(true);
tree.setCellFactory(TextFieldTreeCell.forTreeView());
VBox vbox =new VBox(10);
vbox.setPadding(new Insets(5));
Text text= new Text("Fall tree");
text.setFont(Font.font("I", FontWeight.BOLD,16));
vbox.getChildren().addAll(text,tree);
return vbox;
}
private TreeItem<String> makeBranch(String title, TreeItem<String> parent) {
TreeItem<String>item = new TreeItem<>(title);
item.setExpanded(true);
parent.getChildren().add(item);
return item;
}
private Parent getTop() {
TextField fieldName = new TextField();
Button btSave = new Button("Save");
btSave.setOnAction(event -> {
SaveData data = new SaveData();
// data.name = ((TreeView<String>) tree).getText();
data.name = fieldName.getText();
try {
Function.save(data, "Javafx.save");
}
catch (Exception e) {
e.printStackTrace();
}
});
Button btLoad = new Button("Load");
btLoad.setOnAction(event -> {
try {
SaveData data = (SaveData) Function.load("Javafx.save");
//((TreeItems<String>)tree).setText(data.name);
fieldName.setText(data.name);
}
catch (Exception e) {
e.printStackTrace();
}
});
HBox hb= new HBox(5,fieldName,btSave,btLoad);
hb.setPadding(new Insets(5));
return hb;
}
public static void main(String[] args)
{
Application.launch(args);
}
}
功能类是
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Function {
public static void save(Serializable data, String fileName) throws Exception {
try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get(fileName)))) {
oos.writeObject(data);
}
}
public static Object load(String fileName) throws Exception {
try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(fileName)))) {
return ois.readObject();
}
}
}
SaveData Class是
import javafx.scene.control.TreeView;
public class SaveData implements java.io.Serializable{
private static final long serialVersionUID = 1L;
public String name;
}
我想编辑树并进行更改,保存更改,下次执行后,我应该更改结果。由于我对数据库了解不多,所以我提出了这种方法来保存文件中的更改并加载该文件。
问题是我可以在文本字段中保存和加载文本,但我无法在树中保存和加载更改。
我想保存树中的更改,并在下次执行时加载这些更改。我试过但失败了。
谢谢。