When a TreeItem that has a parent is set as the root node for a TreeView the x position of the root node is the same as if it was placed inside its parent.
This code shows the behaviour:
import java.util.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TreeViewApp extends Application {
static int pos = 0;
public TreeViewApp() {
// TODO Auto-generated constructor stub
}
public static void main(final String[] args) {
Application.launch(args);
}
@Override public void start(final Stage stage) throws Exception {
final VBox box = new VBox();
final TreeView<String> view = new TreeView<String>();
final List<TreeItem<String>> items = new ArrayList<TreeItem<String>>();
for (int i = 0; i < 10; i++) {
final TreeItem<String> item = new TreeItem<String>("Item" + i);
item.setExpanded(true);
items.add(item);
}
for (int i = 0; i < 9; i++) {
items.get(i).getChildren().add(items.get(i + 1));
}
view.setRoot(items.get(0));
final Button button = new Button("Shift");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(final ActionEvent arg0) {
if (pos < 9) {
view.setRoot(items.get(++pos));
} else {
pos = 0;
view.setRoot(items.get(0));
}
}
});
box.getChildren().add(button);
box.getChildren().add(view);
stage.setScene(new Scene(box));
stage.show();
}
}
This code shows the behaviour:
import java.util.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TreeViewApp extends Application {
static int pos = 0;
public TreeViewApp() {
// TODO Auto-generated constructor stub
}
public static void main(final String[] args) {
Application.launch(args);
}
@Override public void start(final Stage stage) throws Exception {
final VBox box = new VBox();
final TreeView<String> view = new TreeView<String>();
final List<TreeItem<String>> items = new ArrayList<TreeItem<String>>();
for (int i = 0; i < 10; i++) {
final TreeItem<String> item = new TreeItem<String>("Item" + i);
item.setExpanded(true);
items.add(item);
}
for (int i = 0; i < 9; i++) {
items.get(i).getChildren().add(items.get(i + 1));
}
view.setRoot(items.get(0));
final Button button = new Button("Shift");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(final ActionEvent arg0) {
if (pos < 9) {
view.setRoot(items.get(++pos));
} else {
pos = 0;
view.setRoot(items.get(0));
}
}
});
box.getChildren().add(button);
box.getChildren().add(view);
stage.setScene(new Scene(box));
stage.show();
}
}