Mudasir
Mudasir

Reputation: 153

how to edit/change name in output in javafx

here is my code

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;


public class Main extends Application
{
    private BorderPane root;
    TreeView<String> tree;
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        root = new BorderPane();        
        root.setLeft(getLeftHBox());

        Scene scene = new Scene(root, 900, 500);        
        primaryStage.setTitle("JavaFx");
        primaryStage.setScene(scene);
        primaryStage.show();    
    }

    private HBox getLeftHBox()
    {
        HBox hbox = new HBox();
        Text text= new Text("Fall tree");
        text.setFont(Font.font("Helvetica", FontWeight.BOLD,16));
        VBox vbox =new VBox(10);
        vbox.setPadding(new Insets(10));

        TreeItem<String> Library,module,unite,translateA ,translateB,rotate;

        //Root
        Library = new TreeItem<>();
        Library.setExpanded(true);
        //module-Child of Root
        module = makeBranch("module",Library);
        makeBranch("Parameter X",module);
        makeBranch("Parameter y",module);
        //Unite-Child of module
        unite = makeBranch("unite",module);
        makeBranch("Parameter uX",unite);
        makeBranch("Parameter uy",unite);
        //TranslateA-Child of Unite
        translateA = makeBranch("translateA",unite);
        makeBranch("Parameter taX",translateA);
        makeBranch("Parameter tay",translateA);
        //TranslateB-Sibling of TranslateA
        translateB = makeBranch("translateB",unite);
        makeBranch("Parameter tbX",translateB);
        makeBranch("Parameter tby",translateB);
        //Rotate-Child of TranslateB
        rotate = makeBranch("rotate",translateB);
        makeBranch("Parameter RX",rotate);
        makeBranch("Parameter RY",rotate);

        tree= new TreeView<>(Library);
        tree.setShowRoot(false);

        vbox.getChildren().addAll(text,tree);
        hbox.getChildren().addAll(vbox);

        return hbox;
    }

    private TreeItem<String> makeBranch(String title, TreeItem<String> parent) {
        TreeItem<String>item = new TreeItem<>(title);
        item.setExpanded(true);
        parent.getChildren().add(item);
        return item;

        }   


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

in the output i got the fall tree/drop down menu. my question is how to edit the names in output. how can i give new titles/names to branches in output and that appear in my code.

Example: when you execute program, i have a branch name "module" on top of output. i want to change that name and write "Rotate_Module" in output.

(i can simply change the code to do that but i want to change it in output and that should automatically appear in my code)

Note# i want to change every name. thank you

Upvotes: 2

Views: 982

Answers (1)

James_D
James_D

Reputation: 209330

To make the tree editable, which is what I think you mean in this question, you need to specify that you want the tree to use cells which incorporate a text field. You can do this with:

tree.setEditable(true);
tree.setCellFactory(TextFieldTreeCell.forTreeView());

If the user edits the tree and makes changes, then those changes will be stored in the TreeItem's valueProperty(); i.e. you can retrieve the values with module.getValue(), etc.

So to make the user's changes persistent, you will need to retrieve all those values and save them to a file (or database, or some other kind of persistent storage) when the application closes. To use those values, when you start the application, read the values from the file (or database, etc) and use those values when you create the tree items.

Upvotes: 1

Related Questions