How to use specific part of output of one method into other? - javafx

i am working on an application. example code is given below
public class Main extends Application
{
private BorderPane border;
#Override
public void start(Stage primaryStage)
{
//Displaying all the functions in Scene
border = new BorderPane();
Scene scene = new Scene(border,750,500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
//Fall-Tree Root Item
TreeItem<String> tree = new TreeItem<String>("Library");
TreeItem<String> item1 = new TreeItem<String>("Module");
TreeItem<String> item1Child = new TreeItem<String>("MX");
item1.getChildren().add(item1Child);
TreeItem<String> item2 = new TreeItem<String>("Unite");
TreeItem<String> item2Child1 = new TreeItem<String>("UX");
TreeItem<String> item2Child2 = new TreeItem<String>("UY");
item2.getChildren().addAll(item2Child1,item2Child2);
item2.setExpanded(true);
TreeItem<String> item3 = new TreeItem<String>("Translate");
TreeItem<String> item3Child = new TreeItem<String>("TX");
item3.getChildren().add(item3Child);
TreeItem<String> item4 = new TreeItem<String>("Rotate");
TreeItem<String> item4Child = new TreeItem<String>("Rx");
item4.getChildren().add(item4Child);
tree.setExpanded(true);
tree.getChildren().addAll(item1,item2,item3,item4);
TreeView<String> treeView = new TreeView<String>(tree);
//Making Tree Editable
treeView.setEditable(true);
treeView.setCellFactory(TextFieldTreeCell.forTreeView());
//Assigning Leaf an Explorer View
VBox box1 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
if (newValue == item2Child1) {
box1.getChildren().add(getrightPane1());
} else {
int i = box1.getChildren().size();
if (i > 0) {
box1.getChildren().remove(0);
}
}
}
});
// Main Branch
VBox box2 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<?> observable, Object oldValue, Object newValue) {
if (newValue == item2) {
box2.getChildren().add(getrightPane2());
} else {
int i = box2.getChildren().size();
if (i > 0) {
box2.getChildren().remove(0);
}
}
}
});
//Displaying all the Functions into border's center
VBox vbox =new VBox(2);
vbox.setPadding(new Insets(5));
VBox.setVgrow(treeView, Priority.ALWAYS);
vbox.getChildren().addAll(new Text("Fall Tree"),treeView);
HBox hb = new HBox();
hb.getChildren().addAll(vbox,box1,box2);
border.setCenter(hb);
}
//Method for 1st Leaf Explorer View
private BorderPane getrightPane1() {
BorderPane root = new BorderPane();
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
TextField textf1 = new TextField();
TextField textf2 = new TextField();
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
//Generating Rectangle's Height by providing Values in Output
textf1.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
//Generating Rectangle's Width by providing Values in Output
textf2.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
//Labels for TextFields
h1.getChildren().addAll(new Label("Y:"), textf1);
h2.getChildren().addAll(new Label("X:"), textf2);
vbox.getChildren().addAll(h1, h2);
root.setLeft(vbox);
return root;
}
//Method for Branch Explorer View
private HBox getrightPane2() {
HBox hbox = new HBox(20);
hbox.setAlignment(Pos.CENTER);
hbox.getChildren().addAll(getrightPane1());
return hbox;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
This is how example code works
"If user put values in field provided in "UX" the rectangle is generated in UX.
But this rectangle vanish when user clicks onto any other branch."
The application should work like this
"If user put values into field in UX, Create Rectangle in UX and in Unite Branch."
Please have a look at method getrightPane2, due to getting all children of getrightPane1, fields are shown in Unite and with that user can create rectangle but that is not right. Only Rectangle created in UX should be shown in Unite nothing else. Please any hint would be fine.
Thank you

Try this code
public class Main extends Application{
private BorderPane border;
#Override
public void start(Stage primaryStage) {
// Displaying all the functions in Scene
border = new BorderPane();
Scene scene = new Scene(border, 750, 500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
// Fall-Tree Root Item
TreeItem<String> tree = new TreeItem<String>("Library");
TreeItem<String> item1 = new TreeItem<String>("Module");
TreeItem<String> item1Child = new TreeItem<String>("MX");
item1.getChildren().add(item1Child);
TreeItem<String> item2 = new TreeItem<String>("Unite");
TreeItem<String> item2Child1 = new TreeItem<String>("UX");
System.out.println();
TreeItem<String> item2Child2 = new TreeItem<String>("UY");
item2.getChildren().addAll(item2Child1, item2Child2);
item2.setExpanded(true);
TreeItem<String> item3 = new TreeItem<String>("Translate");
TreeItem<String> item3Child = new TreeItem<String>("TX");
item3.getChildren().add(item3Child);
TreeItem<String> item4 = new TreeItem<String>("Rotate");
TreeItem<String> item4Child = new TreeItem<String>("Rx");
item4.getChildren().add(item4Child);
tree.setExpanded(true);
tree.getChildren().addAll(item1, item2, item3, item4);
TreeView<String> treeView = new TreeView<String>(tree);
// Making Tree Editable
treeView.setEditable(true);
treeView.setCellFactory(TextFieldTreeCell.forTreeView());
// Assigning Leaf an Explorer View
VBox box1 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
#Override
public void changed(ObservableValue<? extends TreeItem> observable, TreeItem oldValue, TreeItem newValue) {
if (newValue.getValue().equals(item2Child1.getValue())) {
box1.getChildren().add(getrightPane1());
} else {
int i = box1.getChildren().size();
if (i > 0) {
box1.getChildren().remove(0);
}
}
}
});
// Main Branch
VBox box2 = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem>() {
#Override
public void changed(ObservableValue<? extends TreeItem> observable, TreeItem oldValue, TreeItem newValue) {
if (newValue.getValue().equals(item2.getValue())) {
box2.getChildren().add(getrightPane2());
} else {
int i = box2.getChildren().size();
if (i > 0) {
box2.getChildren().remove(0);
}
}
}
});
// Displaying all the Functions into border's center
VBox vbox = new VBox(2);
vbox.setPadding(new Insets(5));
VBox.setVgrow(treeView, Priority.ALWAYS);
vbox.getChildren().addAll(new Text("Fall Tree"), treeView);
HBox hb = new HBox();
hb.getChildren().addAll(vbox, box1, box2);
border.setCenter(hb);
}
TextField textf1 = new TextField();
TextField textf2 = new TextField();
BorderPane root = new BorderPane();
// Method for 1st Leaf Explorer View
private BorderPane getrightPane1() {
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
// Generating Rectangle's Height by providing Values in Output
textf1.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
// Generating Rectangle's Width by providing Values in Output
textf2.setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root.setBottom(rect);
}
}
});
// Labels for TextFields
h1.getChildren().addAll(new Label("Y:"), textf1);
h2.getChildren().addAll(new Label("X:"), textf2);
vbox.getChildren().addAll(h1, h2);
root.setLeft(vbox);
return root;
}
// Method for Branch Explorer View
private HBox getrightPane2() {
HBox hbox = new HBox(20);
hbox.setAlignment(Pos.CENTER);
hbox.setPadding(new Insets(20));
System.out.println(textf1.getText());
if (!textf1.getText().equals("") && !textf2.getText().equals("")) {
Rectangle rectangle = new Rectangle();
rectangle.setHeight(Double.parseDouble(textf1.getText()));
rectangle.setWidth(Double.parseDouble(textf2.getText()));
rectangle.setFill(null);
rectangle.setStroke(Color.RED);
hbox.getChildren().addAll(rectangle);
} else {
Alert alert = new Alert(AlertType.INFORMATION, "First fill the UX Field");
alert.show();
}
return hbox;
}
/**
* #param args
* the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

Related

load a stage in a tab by button click without fxml

i have a GUI, in which i have two tabs. in one tab i want to display a another stage by a button click. i couldn't find a way to solve this problem. i hope you can help me.
I program the whole without fxml.
You can see the gui below. You can also find markings. My Main Window is the one with the markings. The other one should be displaying in the main window with button click.
enter image description here
enter image description here
public class Controller implements Observer {
public static final int WIN_WIDTH = 1064;
public static final int WIN_HEIGHT = 600;
CreateScheme cs = new CreateScheme();
private Pane pane;
VBox vbox = new VBox();
VBox vbox2 = new VBox();
HBox hbox = new HBox();
HBox hbox2 = new HBox();
Pane pane2 = new Pane();
TreeView<File> treeview = new TreeView<File>(new SimpleFileTreeItem(new File("C:\\")));
private TabPane tabPane1 = new TabPane();
private Tab tab3 = new Tab("Bewertungsschema");
private Tab tab4 = new Tab("Ordner");
TabPane tabPane = new TabPane();
Tab tab2 = new Tab("Code");
Tab tab1 = new Tab("Bewertung");
private Button createScheme = new Button("Bewertungsscheme erstellen");
private Button editScheme = new Button("Bewertungsscheme bearbeiten");
private Button exportScheme = new Button("Bewertungsscheme exportieren");
private Button startScheme = new Button("Bewertung starten");
TextArea textarea = new TextArea();
File file;
public void init() {
vbox.setLayoutX(10.0);
vbox.setLayoutY(35.0);
vbox.setSpacing(10.0);
vbox.setMaxHeight(500.0);
vbox.getChildren().add(treeview);
vbox.getChildren().add(tabPane1);
vbox2.setLayoutX(10.0);
vbox2.setLayoutY(50.0);
vbox2.setSpacing(30.0);
vbox2.setPadding(new Insets(80.0, 30.0, 30.0, 30.0));
vbox2.getChildren().addAll(createScheme, editScheme, exportScheme, startScheme);
treeview.setMaxWidth(220.0);
treeview.setMaxHeight(550.0);
treeview.setLayoutY(35.0);
tab2.setContent(textarea);
tab3.setContent(vbox2);
tab4.setContent(treeview);
//tab1.setContent(cs);
textarea.getMaxHeight();
textarea.getMinHeight();
tabPane.getTabs().addAll(tab1, tab2);
tabPane.setLayoutX(250.0);
tabPane.setLayoutY(30.0);
tabPane.setMinWidth(780.0);
tabPane1.getTabs().addAll(tab3, tab4);
tabPane1.setLayoutX(10);
tabPane1.setLayoutY(30);
pane = new Pane();
pane.getChildren().addAll(vbox, tabPane, tabPane1);
createScheme.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
changeStage();
}
});
}
public void changeStage() {
Stage secondStage = new Stage();
CreateScheme cs = new CreateScheme();
try {
cs.start(secondStage);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
secondStage.show();
}
public Pane getPane() {
return this.pane;
}
public class CreateScheme extends Application {
// Variablen
#Override
public void start(Stage secondStage) throws Exception {
// Scene / root
Pane pane = new Pane(); // Layout -> alle Elmente haben die Position 0/0
Scene scene = new Scene(pane, 800, 600);
// Variablen erstellen
VBox vbox = new VBox();
VBox vbox2 = new VBox();
HBox hbox = new HBox();
HBox hbox2 = new HBox();
HBox hbox3 = new HBox();
Label label = new Label("Bewertungsschema erstellen: ");
Button button = new Button("New");
TextField textField1 = new TextField();
CheckBox checkbox = new CheckBox();
Button addButton = new Button("Erstellen");
Button cancelButton = new Button("Abbrechen");
Button saveButton = new Button("Speichern");
Button addTextFieldButton = new Button("+");
TableView<Model> tableview = new TableView<Model>();
TableColumn<Model, String> table1 = new TableColumn<Model, String>("Table1");
TableColumn<Model, String> table2 = new TableColumn<Model, String>("Table2");
TableColumn<Model, String> table3 = new TableColumn<Model, String>("Table3");
TableColumn<Model, String> table4 = new TableColumn<Model, String>("Table4");
TableColumn<Model, String> table5 = new TableColumn<Model, String>("Table5");
ObservableList<Model> list = Listen.getUserList();
tableview.setItems(list);
tableview.setEditable(true);
tableview.getColumns().addAll(table1, table2, table4, table5, table3);
table1.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table2.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table3.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table4.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
table5.setCellFactory(TextFieldTableCell.<Model>forTableColumn());
textField1.setVisible(false);
checkbox.setVisible(false);
vbox2.setVisible(false);
hbox.setSpacing(10.0);
hbox.getChildren().addAll(button, textField1, checkbox);
vbox.setLayoutX(10.0);
vbox.setLayoutY(30.0);
vbox.setSpacing(30.0);
vbox.setPadding(new Insets(10.0, 20.0, 30.0, 10.0));
vbox.getChildren().addAll(label, hbox);
vbox2.setLayoutX(300.0);
vbox2.setLayoutY(30.0);
vbox2.setSpacing(30.0);
vbox2.setMaxHeight(200.0);
vbox2.setPadding(new Insets(50.0, 30.0, 30.0, 30.0));
vbox2.getChildren().add(tableview);
hbox2.setLayoutX(10.0);
hbox2.setLayoutY(500.0);
hbox2.setSpacing(20.0);
hbox3.setLayoutX(10.0);
hbox3.setLayoutY(550.0);
hbox3.setSpacing(20.0);
hbox3.getChildren().addAll(addButton, cancelButton, saveButton, addTextFieldButton);
button.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
textField1.setVisible(true);
checkbox.setVisible(true);
}
});
checkbox.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
vbox2.setVisible(true);
textField1.setEditable(false);
}
});
addTextFieldButton.setOnAction(e -> hbox2.getChildren().add(new TextField()));
addButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//list.add(new Model();
tableview.refresh();
}
});
pane.getChildren().addAll(vbox, vbox2, hbox2, hbox3);
scene.getStylesheets().add("style.css");
secondStage.setTitle("Bewertungsschema erstellen");
secondStage.setScene(scene);
secondStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I think you should load a Parent node instead of a Stage. In the example, BorderPane is used as the root/Parent. BoderPane needs to be the root of the Tab that you want to have this functionality.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author blj0011
*/
public class JavaFXApplication353 extends Application
{
#Override
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
Button btnDisplay1 = new Button("Display 1");
VBox.setMargin(btnDisplay1, new Insets(0, 0, 0, 5));
btnDisplay1.setOnAction((event) -> {
loadDisplay1(root);
});
Button btnDisplay2 = new Button("Display 2");
VBox.setMargin(btnDisplay2, new Insets(0, 0, 0, 5));
btnDisplay2.setOnAction((event) -> {
loadDisplay2(root);
});
VBox vbNav = new VBox(btnDisplay1, btnDisplay2);
vbNav.setAlignment(Pos.CENTER);
vbNav.setSpacing(5);
root.setLeft(vbNav);
loadDisplay1(root);//initial load of scene 1.
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
public void loadDisplay1(BorderPane root)
{
Label label = new Label("Scene 1");
StackPane displayRoot = new StackPane(label);
root.setCenter(displayRoot);
}
public void loadDisplay2(BorderPane root)
{
Label label = new Label("Scene 2");
StackPane displayRoot = new StackPane(label);
root.setCenter(displayRoot);
}
}

Binding rectangles with checkbox

A treeview in which user can add as many root and coordinates as he want. Coordinates generate
rectangles, which are shown horizontally on root node, here is code
import javafx.scene.Node;
abstract class MyNode {
private final String label;
private Node rectangle;
MyNode(String label) {
this.label = label;
}
String getLabel() {
return label;
}
Node getRectangle() {
return rectangle;
}
void setRectangle(Node rectangle) {
this.rectangle = rectangle;
}
}
class MyRootNode extends MyNode {
MyRootNode(String label) {
super(label);
}
}
class MyCoordinateNode extends MyNode {
MyCoordinateNode(String label) {
super(label);
}
}
and the Main class is
public class Main extends Application {
private static int rootNr = 0;
private static int coordinateNr = 0;
public static void main(String[] args) {
launch(args);
}
private static final Map<TreeItem<MyNode>, BorderPane> map = new HashMap<>();
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
TreeItem<MyNode> mainTree = new TreeItem<>(new MyRootNode("Main System"));
mainTree.setExpanded(true);
TreeView<MyNode> treeView = new TreeView<>(mainTree);
treeView.setCellFactory(p -> new AddMenuTreeCell());
treeView.setOnMouseClicked((event) -> {
final TreeItem<MyNode> treeItem = treeView.getSelectionModel().getSelectedItem();
if (treeItem.getValue() instanceof MyRootNode) {
root.setCenter(getRootsPanel(treeItem));
} else {
root.setCenter(map.get(treeItem));
}
});
root.setLeft(treeView);
Scene scene = new Scene(root, 700, 700);
primaryStage.setTitle("Tree View");
primaryStage.setScene(scene);
primaryStage.show();
}
private static class AddMenuTreeCell extends TextFieldTreeCell<MyNode> {
private ContextMenu menu = new ContextMenu();
AddMenuTreeCell() {
MenuItem newitem1 = new MenuItem("Insert Root");
MenuItem newitem2 = new MenuItem("Insert Coordinates");
menu.getItems().addAll(newitem1, newitem2);
newitem1.setOnAction(arg0 -> {
TreeItem<MyNode> item = new TreeItem<>(new MyRootNode("Root" + rootNr++));
getTreeItem().getChildren().add(item);
});
newitem2.setOnAction(arg0 -> {
TreeItem<MyNode> uxItem1 = new TreeItem<>(new MyCoordinateNode("X"));
map.put(uxItem1, getRightPane(uxItem1));
TreeItem<MyNode> uyItem1 = new TreeItem<>(new MyCoordinateNode("Y"));
map.put(uyItem1, getRightPane(uyItem1));
TreeItem<MyNode> newLeaf = new TreeItem<>(new MyRootNode("Coordinates" + coordinateNr++));
newLeaf.getChildren().add(uxItem1);
newLeaf.getChildren().add(uyItem1);
getTreeItem().getChildren().add(newLeaf);
});
}
#Override
public void updateItem(MyNode item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
setGraphic(null);
} else {
if (!isEditing()) {
setText(item.getLabel());
setGraphic(getTreeItem().getGraphic());
if (item instanceof MyRootNode) {
setContextMenu(menu);
}
}
}
}
}
private static BorderPane getRightPane(final TreeItem<MyNode> curTreeItem) {
TextField textf1 = new TextField();
TextField textf2 = new TextField();
BorderPane root1 = new BorderPane();
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
textf1.setOnKeyReleased(event -> {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = getRectangle(textf1, textf2, Color.BLUE);
root1.setCenter(rect);
curTreeItem.getValue().setRectangle(rect);
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
textf2.setOnKeyReleased(event -> {
if (textf1.getText().length() > 0 && textf2.getText().length() > 0) {
Rectangle rect = getRectangle(textf1, textf2, Color.RED);
root1.setCenter(rect);
curTreeItem.getValue().setRectangle(rect);
}
});
h1.getChildren().addAll(new Label("Y:"), textf1);
h2.getChildren().addAll(new Label("X:"), textf2);
vbox.getChildren().addAll(h1, h2);
root1.setLeft(vbox);
return root1;
}
private static Rectangle getRectangle(TextField textf1, TextField textf2, final Color blue) {
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(blue);
return rect;
}
private static BorderPane getRootsPanel(final TreeItem<MyNode> treeItem) {
BorderPane root = new BorderPane();
CheckBox box1 = new CheckBox("Change order");
CheckBox box2 = new CheckBox("Change View");
VBox vbox = new VBox(30, box1,box2);
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(40));
hbox.setAlignment(Pos.TOP_CENTER);
final List<MyNode> coordinateNodes = getCoordinateNodes(treeItem);
for (final MyNode coordinateNode : coordinateNodes) {
if (coordinateNode.getRectangle() != null) {
Platform.runLater(() -> hbox.getChildren().addAll(coordinateNode.getRectangle()));
}
}
Platform.runLater(() -> root.setLeft(hbox));
return root;
}
private static List<MyNode> getCoordinateNodes(final TreeItem<MyNode> treeItem) {
final List<MyNode> result = new ArrayList<>();
if (treeItem.getValue() instanceof MyRootNode) {
for (final TreeItem<MyNode> child : treeItem.getChildren()) {
result.addAll(getCoordinateNodes(child));
}
} else {
result.add(treeItem.getValue());
}
return result;
}
}
My Question is, How to bind all newly generated rectangles which are shown in root with these two check-boxes that if first checkbox is selected then order of rectangle should be changed like(1,2,3 to 3,2,1), with second checkbox rectangles are moved from horizontal to vertical view and if both checked then both features are applied.
due to the fact that user can add rectangle as many as he want, it is difficult for me to bind all of them.
i tried by putting bounty on the question but still didn't get any response. Please i am desperately searching help on this problem . Please
i will be very Thankfull.

Javafx: Adjusting TreeView with right order

Here is a TreeView in which User can add tree items like Roots and its Coordinates.
Problem is all newly added Roots have same rectangles generated by Coordinates. Every root should have its own result not same from other Roots. For this do i have to assign index for every root so that it has its own result?
public class Main extends Application
{ public static void main(String[] args)
{launch(args);}
static final Map<TreeItem<String>, BorderPane> map = new HashMap();
#Override
public void start(Stage primaryStage)
{
BorderPane root = new BorderPane();
TreeItem<String> tree = new TreeItem<String>("Main System");
TreeItem<String> item1 = new TreeItem<String>("Roots");
TreeView<String> treeView = new TreeView<String>(tree);
treeView.setOnMouseClicked((event)->{
TreeItem<String> TreeItem = (TreeItem<String>)treeView.getSelectionModel().getSelectedItem();
if(TreeItem.getValue().equals("Roots"))
{
root.setCenter(getRootsPanel());
}
else
{
root.setCenter(map.get(TreeItem));
}
});
treeView.setCellFactory(new Callback<TreeView<String>,TreeCell<String>>(){
#Override
public TreeCell<String> call(TreeView<String> p) {
return new AddMenuTreeCell();
}
});
tree.setExpanded(true);
root.setLeft(treeView);
tree.getChildren().add(item1);
Scene scene = new Scene(root, 700, 500);
primaryStage.setTitle("Tree View");
primaryStage.setScene(scene);
primaryStage.show();
}
TreeItem<String> addNewTreeItem(String name){
TreeItem TreeItem = new TreeItem(name);
return TreeItem;
}
private static class AddMenuTreeCell extends TextFieldTreeCell<String> {
private ContextMenu menu = new ContextMenu();
private TextField textField;
public AddMenuTreeCell() {
MenuItem newitem1 = new MenuItem("Insert Roots");
MenuItem newitem2 = new MenuItem("Insert Coordinates");
menu.getItems().addAll(newitem1,newitem2);
newitem1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
TreeItem<String> item3 = new TreeItem<String>("Roots");
// item3.getChildren().clear();
getTreeItem().getChildren().add(item3);
}
});
newitem2.setOnAction(new EventHandler<ActionEvent>() {
#SuppressWarnings("unchecked")
#Override
public void handle(ActionEvent arg0) {
TreeItem<String> newLeaf = new TreeItem<String>("Coordinates");
TreeItem<String> uxItem1 = new TreeItem("X");
map.put(uxItem1, getrightPane1());
TreeItem<String> uyItem1 = new TreeItem("y");
map.put(uyItem1, getrightPane1());
newLeaf.getChildren().addAll(uxItem1,uyItem1);
getTreeItem().getChildren().add(newLeaf);
}
});
}
#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(item);
}
setText(null);
setGraphic(textField);
} else {
setText(item);
setGraphic(getTreeItem().getGraphic());
if (!(getTreeItem().isLeaf() && getTreeItem().getParent() == null)){
setContextMenu(menu);
}
}
}
}
}
private static BorderPane getrightPane1() {
TextField textf1 = new TextField();
TextField textf2 = new TextField();
BorderPane root1 = new BorderPane();
VBox vbox = new VBox(20);
vbox.setPadding(new Insets(10));
HBox h1 = new HBox(7);
HBox h2 = new HBox(7);
textf1.setPrefWidth(100);
textf1.setPromptText("Enter Height");
textf1.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect1 = new Rectangle();
rect1.setHeight(Double.parseDouble(textf1.getText()));
rect1.setWidth(Double.parseDouble(textf2.getText()));
rect1.setFill(null);
rect1.setStroke(Color.RED);
root1.setCenter(rect1);
}
}
});
textf2.setPrefWidth(100);
textf2.setPromptText("Enter Width");
textf2.setOnKeyReleased(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event)
{
if(textf1.getText().length() > 0 && textf2.getText().length() > 0)
{
Rectangle rect2 = new Rectangle();
rect2.setHeight(Double.parseDouble(textf1.getText()));
rect2.setWidth(Double.parseDouble(textf2.getText()));
rect2.setFill(null);
rect2.setStroke(Color.RED);
root1.setCenter(rect2);
}
}
});
if(textf1.getText().length() > 0 && textf2.getText().length() > 0 && root1.getCenter() == null)
{
Rectangle rect = new Rectangle();
rect.setHeight(Double.parseDouble(textf1.getText()));
rect.setWidth(Double.parseDouble(textf2.getText()));
rect.setFill(null);
rect.setStroke(Color.RED);
root1.setCenter(rect);
}
h1.getChildren().addAll(new Label("Y1:"), textf1);
h2.getChildren().addAll(new Label("X1:"), textf2);
vbox.getChildren().addAll(h1, h2);
root1.setLeft(vbox);
return root1;
}
private static BorderPane getRootsPanel() {
BorderPane root2 = new BorderPane();
HBox hbox = new HBox(10);
hbox.setPadding(new Insets(40));
hbox.setAlignment(Pos.TOP_CENTER);
List<BorderPane> listBordePane = new ArrayList(map.values());
for(BorderPane element : listBordePane)
{
Node node = element.getCenter();
if(node instanceof Rectangle)
{
Rectangle rect1 = ((Rectangle)node);
Rectangle rect2 = new Rectangle();
rect2.setWidth(rect1.getWidth());
rect2.setHeight(rect1.getHeight());
rect2.setFill(rect1.getFill());
rect2.setStroke(rect1.getStroke());
Platform.runLater(()->{hbox.getChildren().add(rect2);});
}
}
Platform.runLater(()->{root2.setLeft(hbox);});
return root2;
}
}
Note: if use clear() to clean every root, it would cause issue for this problem,
" let suppose user has main root in which there are 2 sub roots with 1 coordinate each. so each sub root show 2 rectangles in horizontal order and Main root will have 4 rectangles in horizontal order".
Any idea will be helpful please. Thank you

How to execute the method if a specific item in the tree is selected?

Here is my example given below is editable fall-tree.
public class Main extends Application
{
private BorderPane border;
TreeItem<String> tree;
TreeView<String> treeView;
#Override
public void start(Stage primaryStage)
{
border = new BorderPane();
Scene scene = new Scene(border,750,500);
primaryStage.setTitle("BorderPane");
primaryStage.setScene(scene);
primaryStage.show();
tree = new TreeItem<String>("Library");
TreeItem<String> item1 = new TreeItem<String>("Module");
TreeItem<String> item1Child = new TreeItem<String>("MX");
item1.getChildren().add(item1Child);
TreeItem<String> item2 = new TreeItem<String>("Unite");
TreeItem<String> item2Child = new TreeItem<String>("UX");
item2.getChildren().add(item2Child);
item2.setExpanded(true);
TreeItem<String> item3 = new TreeItem<String>("Translate");
TreeItem<String> item3Child = new TreeItem<String>("TX");
item3.getChildren().add(item3Child);
TreeItem<String> item4 = new TreeItem<String>("Rotate");
TreeItem<String> item4Child = new TreeItem<String>("Rx");
item4.getChildren().add(item4Child);
tree.setExpanded(true);
tree.getChildren().addAll(item1,item2,item3,item4);
treeView = new TreeView<String>(tree);
treeView.setEditable(true);
treeView.setCellFactory(TextFieldTreeCell.forTreeView());
VBox vbox =new VBox(2);
vbox.setPadding(new Insets(5));
VBox.setVgrow(treeView, Priority.ALWAYS);
vbox.getChildren().addAll(new Text("Fall Tree"),treeView);
HBox hb = new HBox();
VBox rightPane = getrightPane();
hb.setSpacing(20);
hb.getChildren().addAll(vbox,rightPane);
border.setCenter(hb);
}
private VBox getrightPane() {
VBox vb = new VBox(30);
vb.setPadding(new Insets(10));
HBox h1 = new HBox(7);
h1.getChildren().addAll(new Label("X:"), new TextField());
HBox box = new HBox(20);
Rectangle rect1 = new Rectangle(150,150);
rect1.setFill(null);
rect1.setStroke(Color.BLACK);
box.getChildren().addAll(rect1);
box.setAlignment(Pos.BOTTOM_CENTER);
vb.getChildren().addAll(h1,box);
return vb;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
As it can be seen on comment section a listener is applied but its only checking text on branch and leaf text.
My question is how to assign getrightPane() whole as a listener to the only one leaf(as an example let say MX). when applied, other leaf shouldn't show the getrightPane().
Please help me assign getrightPane() as a listener to only one selected leaf/branch.
Thank you.
Try this
VBox rightPane = new VBox();
treeView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener() {
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
if (newValue == item1Child) {
rightPane.getChildren().add(getrightPane());
} else {
int i = rightPane.getChildren().size();
if (i > 0) {
rightPane.getChildren().remove(0);
}
}
}
});
You should be able to just do, for example:
treeView.getSelectionModel().selectedItemProperty().addListener((obs, oldItem, newItem) -> {
if (newItem == item1Child) {
System.out.println("MX selected");
// do whatever you need here....
}
});

how to close other tabs except the selected tab

I want to implement TabPane with ContextMenu to close other tabs except the selected tab
public class MainApp extends Application
{
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Tabs");
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);
final TabPane tabPane = new TabPane();
BorderPane borderPane = new BorderPane();
for (int i = 0; i < 5; i++)
{
Tab tab = new Tab();
tab.setText("Tab" + i);
HBox hbox = new HBox();
hbox.getChildren().add(new Label("Tab" + i));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);
tabPane.getTabs().add(tab);
ContextMenu contextMenu = new ContextMenu();
MenuItem close = new MenuItem();
MenuItem closeOthers = new MenuItem();
MenuItem closeAll = new MenuItem();
close.setText("Close");
closeOthers.setText("Close Others");
closeAll.setText("Close All");
contextMenu.getItems().addAll(close, closeOthers, closeAll);
tab.setContextMenu(contextMenu);
final ObservableList<Tab> tablist = tabPane.getTabs();
close.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
tabPane.getTabs().remove(tabPane.getSelectionModel().getSelectedItem());
}
});
closeOthers.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
tabPane.getTabs().removeAll();
}
});
closeAll.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
tabPane.getTabs().removeAll(tablist);
}
});
}
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabPane);
root.getChildren().add(borderPane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
But the code is not working. Can you help me to implement this solution.
Is there any better approach to implement this?
This is pretty simple. Just iterate through all Tab-Objects in the tablist and mark the ones which should be deleted. After the iteration remove the marked Tab-Objects.
closeOthers.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
// ArrayList of Tab-Objects in which we store Tabs which should be deleted..
ArrayList<Tab> markedTabs = new ArrayList<>();
// Iterate through all Tabs in tablist
for (Tab tab : tablist) {
// Is the tab the current active tab? if not, add it to the markedTabs-ArrayList
if (tab != tabPane.getSelectionModel().getSelectedItem())
markedTabs.add(tab);
}
// Remove them from the tabPane..
tabPane.getTabs().removeAll(markedTabs);
}
});

Resources