How to get values of multiple selected rows in TableView using Javafx? - javafx

I have tried the following code for TableView Multiple Selection. I just want all the ID's of selected row in an ArrayList. But whenever I select rows instead of showing ID, complete row values are displayed.
package tabletest;
import java.util.ArrayList;
import java.util.ListIterator;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
* #author vikassingh
*/
public class TableTest extends Application {
#Override
public void start(Stage primaryStage) {
TableView<ObservableList<String>> tableLocal = new TableView<>();
tableLocal.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
TableColumn<ObservableList<String>, String> idCol = new TableColumn<>("ID");
TableColumn<ObservableList<String>, String> nameCol = new TableColumn<>("Customer Name");
tableLocal.getColumns().addAll(nameCol);
//idCol
idCol.setCellValueFactory(cellData -> {
ObservableList<String> rowValue = cellData.getValue();
String cellValue = rowValue.get(0);
return new ReadOnlyStringWrapper(cellValue);
});
//nameCol
nameCol.setCellValueFactory(cellData -> {
ObservableList<String> rowValue = cellData.getValue();
String cellValue = rowValue.get(1);
return new ReadOnlyStringWrapper(cellValue);
});
tableLocal.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
ObservableList selectedItems = tableLocal.getSelectionModel().getSelectedItems();
System.out.println(selectedItems);
// TEST
ArrayList<String> selectedIDs = new ArrayList<String>();
for (int i = 0; i < selectedItems.size(); i++) {
ObservableList<String> innerSelectedItems = FXCollections.observableArrayList(selectedItems.get(i).toString());
for (String ID : innerSelectedItems) {
selectedIDs.add(ID);
}
}
ListIterator<String> iterator = selectedIDs.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// END TEST
}
});
ObservableList<String> row1 = FXCollections.observableArrayList();
row1.add("1");
row1.add("Cust One");
ObservableList<String> row2 = FXCollections.observableArrayList();
row2.add("2");
row2.add("Cust Two");
ObservableList<String> row3 = FXCollections.observableArrayList();
row3.add("3");
row3.add("Cust Three");
ObservableList<ObservableList<String>> filteredDataLocal = FXCollections.observableArrayList();
filteredDataLocal.addAll(row1, row2, row3);
tableLocal.setItems(filteredDataLocal);
VBox root = new VBox();
root.getChildren().addAll(tableLocal);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}

Since the id is the first element in the list representing each row, you just need
tableLocal.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
ObservableList<ObservableList<String>> selectedItems = tableLocal.getSelectionModel().getSelectedItems();
System.out.println(selectedItems);
// TEST
ArrayList<String> selectedIDs = new ArrayList<String>();
for (ObservableList<String> row : selectedItems) {
selectedIDs.add(row.get(0));
}
ListIterator<String> iterator = selectedIDs.listIterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// END TEST
}
});

Related

Javafx: On button click open second selection window

I'm making a button which opens a new window containing a gridview of members, which shows their name and rank. A member can be clicked to be set as currentMember. However my JavaFX knowledge is limited and I have no idea how to create this temporary window without changing the current controller. I'm not sure if what I have is a good way of doing it. Am I doing something wrong or is this the correct way of doing it?
This code gets run when the Select Person button is clicked
#FXML
private void setLid(ActionEvent event) {
Stage stage = new Stage();
VBox box = new VBox();
box.setPadding(new Insets(10));
TableView<Persoon> tablePers = new TableView<>();
TableColumn<Persoon, String> voornaam = new TableColumn<>();
TableColumn<Persoon, String> achternaam = new TableColumn<>();
TableColumn<Persoon, String> graad = new TableColumn<>();
voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());
Label label = new Label("Selecteer een persoon");
Button btnSelectCurrentLid = new Button();
btnSelectCurrentLid.setText("Bevestigen");
btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
geselecteerdePersoon = (tablePers.getSelectionModel().getSelectedItem());
stage.close();
});
box.getChildren().add(label);
box.getChildren().add(tablePers);
box.getChildren().add(btnSelectCurrentLid);
Scene scene = new Scene(box, 250, 150);
stage.setScene(scene);
stage.show();
}
Here is a one-file mcve (copy paste the entire code into FxMain.java and run):
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FxMain extends Application {
private Persoon geselecteerdePersoon;
#Override
public void start(Stage primaryStage) throws Exception{
Label selectedPersonInfo = new Label();
Button start = new Button("Show Tabel");
start.setOnAction(e-> {
new LoginDialog().showAndWait();
selectedPersonInfo.setText("Selected: " +geselecteerdePersoon.voornaamProperty().get()
+ " " +geselecteerdePersoon.achternaamProperty().get());
});
BorderPane root = new BorderPane(selectedPersonInfo);
root.setBottom(start);
primaryStage.setScene(new Scene(root, 300,300));
primaryStage.show();
}
public static void main(final String[] args) {
launch(args);
}
class LoginDialog extends Dialog {
public LoginDialog() {
VBox box = new VBox();
box.setPadding(new Insets(10));
TableView<Persoon> tablePers = new TableView<>();
TableColumn<Persoon, String> voornaam = new TableColumn<>("Name");
TableColumn<Persoon, String> achternaam = new TableColumn<>("Last Name");
TableColumn<Persoon, String> graad = new TableColumn<>("Grade");
voornaam.setCellValueFactory(cellData -> cellData.getValue().voornaamProperty());
voornaam.setCellFactory(TextFieldTableCell.forTableColumn());
achternaam.setCellValueFactory(cellData -> cellData.getValue().achternaamProperty());
graad.setCellValueFactory(cellData -> cellData.getValue().graadProperty());
tablePers.getColumns().addAll(voornaam, achternaam, graad);
tablePers.getItems().add(new Persoon("Alice", "Bee","70"));
tablePers.getItems().add(new Persoon("Charly", "Din","32"));
Label label = new Label("Selecteer een persoon");
Button btnSelectCurrentLid = new Button();
btnSelectCurrentLid.setText("Bevestigen");
btnSelectCurrentLid.setOnAction((ActionEvent e) -> {
geselecteerdePersoon = tablePers.getSelectionModel().getSelectedItem();
close();
});
box.getChildren().addAll(label, tablePers, btnSelectCurrentLid);
getDialogPane().setContent(box);
getDialogPane().getButtonTypes().addAll(ButtonType.CANCEL);
}
}
}
class Persoon {
private final SimpleStringProperty lName;
private final SimpleStringProperty fName;
private final SimpleStringProperty grade;
public Persoon(String fName, String lName, String grade) {
this.fName = new SimpleStringProperty(fName);
this.lName = new SimpleStringProperty(lName);
this.grade = new SimpleStringProperty(grade);
}
public final StringProperty achternaamProperty() { return lName; }
public final StringProperty voornaamProperty() { return fName; }
public final StringProperty graadProperty() { return grade; }
}

How can I de-select/ Unfocus everything when presing ESC

I have the following mini-app:
public class TestApp extends Application {
public static void main(final String[] args) {
launch(args);
}
private final AtomicLong counter = new AtomicLong();
#Override
public void start(final Stage primaryStage) {
final VBox root = new VBox(5);
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));
tableView.getColumns().add(column);
// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
final Button button = new Button("Add items");
final TextArea t1 = new TextArea();
button.setOnAction(e -> {
final long oldElement = counter.get();
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
});
root.getChildren().add(button);
root.getChildren().add(t1);
root.getChildren().add(tableView);
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
When it starts up, the button has focus. If I click in the TextArea, it gets focus.
There is no way now to "unfocus" the TextArea again, besides pressing the button (which will trigger an action, which is unwanted if I only want to get rid of the focus).
How can I release all focus and deselect everything, for example when I pres ESC?
I don't know if this fully meets your requirements, but you can create a boolean variable to keep up with the state of ESCAPE. You can also use a ChangeListener that allows the root node to regain focus anytime it loses it.
import java.util.concurrent.atomic.AtomicLong;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TestApp extends Application
{
boolean escActive = false;
final VBox root = new VBox(5);
ChangeListener<Boolean> changeListener1 = new ChangeListener()
{
#Override
public void changed(ObservableValue observable, Object oldValue, Object newValue)
{
System.out.println(newValue);
root.requestFocus();
}
};
public static void main(final String[] args)
{
launch(args);
}
private final AtomicLong counter = new AtomicLong();
#Override
public void start(final Stage primaryStage)
{
root.setPadding(new Insets(10));
root.setAlignment(Pos.CENTER);
final TableView<String> tableView = new TableView<>();
final TableColumn<String, String> column = new TableColumn<>("Text");
column.setCellValueFactory(f -> new SimpleStringProperty(f.getValue()));
tableView.getColumns().add(column);
// Add some sample items to our TableView
for (int i = 0; i < 100; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
final Button button = new Button("Add items");
final TextArea t1 = new TextArea();
button.setOnAction(e -> {
if (!escActive) {
final long oldElement = counter.get();
// Add more elements
for (int i = 0; i < 10; i++) {
tableView.getItems().add("Item #" + counter.incrementAndGet());
}
tableView.scrollTo("Item #" + oldElement);
}
});
root.getChildren().add(button);
root.getChildren().add(t1);
root.getChildren().add(tableView);
root.setOnKeyReleased((event) -> {
System.out.println(event.getCode());
if (event.getCode() == KeyCode.ESCAPE) {
escActive = !escActive;
if (escActive) {
root.requestFocus();
root.focusedProperty().addListener(changeListener1);
}
else {
root.focusedProperty().removeListener(changeListener1);
}
}
});
// Show the Stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

Why is JavaFX tableView's content incomplete?

I'm trying to creating a JavaFX TableView with "tower-shape" columns. Some columns are covered and can not be seen like this:
In the picture, you can see, the scroll bar has already been at the end, but there are still some columns can't be seen.
Here is my code:
import java.util.LinkedList;
import java.util.Queue;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class PaneDemo extends Application {
private TableView<String> table = new TableView<>();
public static void main(String[] args) {
launch(args);
}
private static int getTotal(int layer) {
int total = 0;
for(int i=1;i<=layer;i++) {
int start = 1;
for(int j=0;j<i;j++) {
start *= 5;
}
total += start;
}
return total;
}
#Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
AnchorPane anchorPane = new AnchorPane();
anchorPane.setPrefWidth(900);
anchorPane.setPrefHeight(600);
table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);
TableColumn<String, String> Test1 = new TableColumn<>("Test1");
TableColumn<String, String> Test2 = new TableColumn<>("Test2");
TableColumn<String, String> Test3 = new TableColumn<>("Test3");
TableColumn<String, String> Test4 = new TableColumn<>("Test4");
TableColumn<String, String> Test5 = new TableColumn<>("Test5");
Queue<TableColumn<String, ?>> queue = new LinkedList<>();
table.getColumns().addAll(Test1, Test2, Test3, Test4, Test5);
table.getItems().add("test");
queue.addAll(table.getColumns());
int index = 0;
while(index < getTotal(3)) {
TableColumn<String, ?> root = queue.poll();
TableColumn<String, String> test1 = new TableColumn<>("test1");
TableColumn<String, String> test2 = new TableColumn<>("test2");
TableColumn<String, String> test3 = new TableColumn<>("test3");
TableColumn<String, String> test4 = new TableColumn<>("test4");
TableColumn<String, String> test5 = new TableColumn<>("test5");
root.getColumns().addAll(test1, test2, test3, test4, test5);
queue.addAll(root.getColumns());
index++;
}
while(!queue.isEmpty()) {
generateCellFactory((TableColumn<String, String>) queue.poll());
}
table.prefHeightProperty().bind(anchorPane.heightProperty());
table.prefWidthProperty().bind(anchorPane.widthProperty());
anchorPane.getChildren().add(table);
((Group) scene.getRoot()).getChildren().addAll(anchorPane);
stage.setScene(scene);
stage.show();
}
private void generateCellFactory(TableColumn<String, String> column) {
column.setCellFactory(cell -> {
return new TableCell<String, String>() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText("Test");
}
};
});
}
}
I finally find the answer.
It happans when the column prefwidth is double or you didn't set prefwidth for last layer columns.

How to access all textfields in a row of the TextfieldCellFactory

I'm using a listener to set the height of my Textarea in the TextfieldCellFactory:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextArea;
import javafx.util.Callback;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Supermain extends Application {
#Override
public void start(Stage primaryStage) {
final TableView<myTextRow> table = new TableView<>();
table.setEditable(true);
table.setStyle("-fx-text-wrap: true;");
//Table columns
TableColumn<myTextRow, String> clmID = new TableColumn<>("ID");
clmID.setMinWidth(160);
clmID.setCellValueFactory(new PropertyValueFactory<>("ID"));
TableColumn<myTextRow, String> clmtext = new TableColumn<>("Text");
clmtext.setMinWidth(160);
clmtext.setCellValueFactory(new PropertyValueFactory<>("text"));
clmtext.setCellFactory(new TextFieldCellFactory());
TableColumn<myTextRow, String> clmtext2 = new TableColumn<>("Text2");
clmtext2.setMinWidth(160);
clmtext2.setCellValueFactory(new PropertyValueFactory<>("text2"));
clmtext2.setCellFactory(new TextFieldCellFactory());
//Add data
final ObservableList<myTextRow> data = FXCollections.observableArrayList(
new myTextRow(5, "Lorem","Test"),
new myTextRow(2, "Ipsum","Test2")
);
table.getColumns().addAll(clmID, clmtext,clmtext2);
table.setItems(data);
HBox hBox = new HBox();
hBox.setSpacing(5.0);
hBox.setPadding(new Insets(5, 5, 5, 5));
Button btn = new Button();
btn.setText("Get Data");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
for (myTextRow data1 : data) {
System.out.println("data:" + data1.getText());
}
}
});
hBox.getChildren().add(btn);
BorderPane pane = new BorderPane();
pane.setTop(hBox);
pane.setCenter(table);
primaryStage.setScene(new Scene(pane, 640, 480));
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static class TextFieldCellFactory
implements Callback<TableColumn<myTextRow, String>, TableCell<myTextRow, String>> {
#Override
public TableCell<myTextRow, String> call(TableColumn<myTextRow, String> param) {
TextFieldCell textFieldCell = new TextFieldCell();
return textFieldCell;
}
public static class TextFieldCell extends TableCell<myTextRow, String> {
private TextArea textField;
private StringProperty boundToCurrently = null;
public TextFieldCell() {
textField = new TextArea();
textField.setWrapText(true);
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
this.setGraphic(textField);
}
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (!empty) {
// Show the Text Field
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
// Retrieve the actual String Property that should be bound to the TextField
// If the TextField is currently bound to a different StringProperty
// Unbind the old property and rebind to the new one
ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
SimpleStringProperty sp = (SimpleStringProperty) ov;
if (this.boundToCurrently == null) {
this.boundToCurrently = sp;
this.textField.textProperty().bindBidirectional(sp);
} else if (this.boundToCurrently != sp) {
this.textField.textProperty().unbindBidirectional(this.boundToCurrently);
this.boundToCurrently = sp;
this.textField.textProperty().bindBidirectional(this.boundToCurrently);
}
double height = real_lines_height(textField.getText(), this.getWidth(), 30, 22);
textField.setPrefHeight(height);
textField.setMaxHeight(height);
// if height bigger than the biggest height in the row
//-> change all heights of the row(textfields ()typeof textarea) to this height
// else leave the height as it is
//System.out.println("item=" + item + " ObservableValue<String>=" + ov.getValue());
//this.textField.setText(item); // No longer need this!!!
} else {
this.setContentDisplay(ContentDisplay.TEXT_ONLY);
}
}
}
}
public class myTextRow {
private final SimpleIntegerProperty ID;
private final SimpleStringProperty text;
private final SimpleStringProperty text2;
public myTextRow(int ID, String text,String text2) {
this.ID = new SimpleIntegerProperty(ID);
this.text = new SimpleStringProperty(text);
this.text2 = new SimpleStringProperty(text2);
}
public void setID(int id) {
this.ID.set(id);
}
public void setText(String text) {
this.text.set(text);
}
public void setText2(String text) {
this.text2.set(text);
}
public int getID() {
return ID.get();
}
public String getText() {
return text.get();
}
public StringProperty textProperty() {
return text;
}
public String getText2() {
return text2.get();
}
public StringProperty text2Property() {
return text2;
}
public IntegerProperty IDProperty() {
return ID;
}
}
private static double real_lines_height(String s, double width, double heightCorrector, double widthCorrector) {
HBox h = new HBox();
Label l = new Label("Text");
h.getChildren().add(l);
Scene sc = new Scene(h);
l.applyCss();
double line_height = l.prefHeight(-1);
int new_lines = s.replaceAll("[^\r\n|\r|\n]", "").length();
// System.out.println("new lines= "+new_lines);
String[] lines = s.split("\r\n|\r|\n");
// System.out.println("line count func= "+ lines.length);
int count = 0;
//double rest=0;
for (int i = 0; i < lines.length; i++) {
double text_width = get_text_width(lines[i]);
double plus_lines = Math.ceil(text_width / (width - widthCorrector));
if (plus_lines > 1) {
count += plus_lines;
//rest+= (text_width / (width-widthCorrector)) - plus_lines;
} else {
count += 1;
}
}
//count+=(int) Math.ceil(rest);
count += new_lines - lines.length;
return count * line_height + heightCorrector;
}
private static double get_text_width(String s) {
HBox h = new HBox();
Label l = new Label(s);
l.setWrapText(false);
h.getChildren().add(l);
Scene sc = new Scene(h);
l.applyCss();
// System.out.println("dubbyloop.FXMLDocumentController.get_text_width(): "+l.prefWidth(-1));
return l.prefWidth(-1);
}
}
How can I achieve to read the heights from all Textareas in a row, to compare them and set to the biggest one?
I now add the full code, then it is maybe more clear what I mean.

filling TreeTableView in fxml dynamicly from JavaFX

I want to fill a TreeTableView dynamically. I have created the view with Scenebuilder. In MyController I have a Draw button which by cliking calls a method, which makes a treetable. I have references set. when i click the button only columns are appear. The first column which is ought to show the treecolumn shows nothing and there is also no other value under columns. The TreeTableView works when I just use it alone and add it to scene, but now i want to use the values to fill the treeTable which is made by scene Builder. There is something else that i can not figure out. The columns are set correctly when comment out treeTable = new TreeTableView<>(root); which is under treeTable comment! otherwise the columns are not shown either.
That is what i want to see:
package controller;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import model.DataConstructor;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableCell;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.util.Callback;
public class MainController implements Initializable {
private TreeItem<String> root = new TreeItem<>("Functions");
private DataConstructor dc = new DataConstructor();
#FXML
private TreeTableView<String> treeTable;
#Override
public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
}
public void testDraw(ActionEvent event) {
drawTable();
}
private void drawTable() {
root.setExpanded(true);
Set<String> combinedKeys = new HashSet<>(dc.getCombiFunc().keySet());
Set<String> funcAllKeys = new HashSet<>(dc.getSortedfuncAll().keySet());
funcAllKeys.removeAll(dc.getCombiFunc().keySet());
for (List<String> value : dc.getCombiFunc().values()) {
funcAllKeys.removeAll(value);
}
for (String valueremained : funcAllKeys) {
ArrayList<String> tempNameId = new ArrayList<>();
tempNameId.add(dc.getSortedfuncAll().get(valueremained));
// all elements which are not in combined functions (They are all
// orphan)
root.getChildren().add(new TreeItem<String>(tempNameId.get(0)));
}
Set<String> keyFromcombined = new HashSet<>();
List<String> valueOfCombined = new ArrayList<String>();
for (Entry<String, List<String>> ent : dc.getCombiFunc().entrySet()) {
valueOfCombined.add(ent.getValue().get(0));
}
List<String> rootKeyList = new ArrayList<>();
for (String key : combinedKeys) {
if (!valueOfCombined.contains((key))) {
keyFromcombined.add(dc.getFuncAll().get(key));
rootKeyList.add(key);
}
}
String[] rootKeys = rootKeyList.toArray(new String[rootKeyList.size()]);
// ////////////////treetable////////////////////////////
treeTable = new TreeTableView<>(root);
Arrays.stream(rootKeys).forEach(
rootKey -> root.getChildren().add(
createTreeItem(dc.getCombiFunc(), rootKey)));
// ////////////////First column/////////////////////////
TreeTableColumn<String, String> firstColumn = new TreeTableColumn<>("");
treeTable.getColumns().add(firstColumn);// Tree column
firstColumn
.setCellValueFactory(new Callback<CellDataFeatures<String, String>, ObservableValue<String>>() {
public ObservableValue<String> call(
CellDataFeatures<String, String> p) {
return new ReadOnlyStringWrapper(p.getValue()
.getValue());
}
});
// //////////////////Rest Columns////////////////////////
for (Entry<String, String> ent : dc.getSortedAssignedOrg().entrySet()) {
TreeTableColumn<String, ArrayList<String>> col = new TreeTableColumn<>();
Label label = new Label(ent.getValue());
col.setGraphic(label);
label.setTooltip(new Tooltip(label.getText()));// tooltip for column
// headers
col.setPrefWidth(45);
//cell Value Factory////////////////////////
col.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<String, ArrayList<String>>, ObservableValue<ArrayList<String>>>() {
#Override
public ObservableValue<ArrayList<String>> call(
CellDataFeatures<String, ArrayList<String>> param) {
TreeMap<String, List<String>> temp = (TreeMap<String, List<String>>) dc
.getFuncTypeOrg().clone();
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < dc.getFuncTypeOrg().size(); i++) {
List<String> list = temp.firstEntry().getValue();
String key = temp.firstEntry().getKey();
// root.getChildren();
if (list.get(1).equals(param.getValue().getValue())
&& list.get(5).equals(label.getText())) {
result.add(0, list.get(2));// weight
// //////////////org combi TODO
for (Entry<String, Set<String>> ent : dc
.getCombiOrg().entrySet()) {
if (ent.getKey().contains(col.getText()))
for (Set<String> value : dc.getCombiOrg()
.values()) {
if (value.contains(col.getText()))
System.out.println(col.getText());
}
}
// ///////////////org combi
if (list.size() > 6) {
result.add(1, list.get(list.size() - 1));// color
result.add(2, list.get(6));// App component
}
else
result.add("white");
result.add("noOrg");
} else {
temp.remove(key);
}
}
return new ReadOnlyObjectWrapper<ArrayList<String>>(result);
}
});
// //////////////cellfactory/////////////////////////
col.setCellFactory(new Callback<TreeTableColumn<String, ArrayList<String>>, TreeTableCell<String, ArrayList<String>>>() {
#Override
public TreeTableCell<String, ArrayList<String>> call(
TreeTableColumn<String, ArrayList<String>> param) {
return new TreeTableCell<String, ArrayList<String>>() {
public void updateItem(ArrayList<String> item,
boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setStyle("");
setText("");
} else if (item.contains("Green")) {
float weightInt = Float.parseFloat(item.get(0));
float res = weightInt * 1;
String resString = Float.toString(res);
this.setStyle("-fx-background-color:green");
setTooltip(new Tooltip(item.get(2)));
setText(resString);
} else if (item.contains("yellow")) {
this.setStyle("-fx-background-color:yellow");
setTooltip(new Tooltip(item.get(2)));
setText("0");
} else if (item.contains("white")) {
setText("DD");
}
}
};
};
});
treeTable.getColumns().add(col);
}
// end for col
treeTable.setPrefWidth(1200);
treeTable.setPrefHeight(500);
treeTable.setShowRoot(false);
treeTable.setTableMenuButtonVisible(true);
}
/**
* Create a TreeItem for a TreeView from a set of data given the data and an
* identified root within the data.
*/
private TreeItem<String> createTreeItem(TreeMap<String, List<String>> data,
String rootKey) {
TreeItem<String> item = new TreeItem<>();
item.setValue(rootKey);
item.setExpanded(true);
List<String> childData = data.get(rootKey);
if (childData != null) {
childData.stream().map(child -> createTreeItem(data, child))
.collect(Collectors.toCollection(item::getChildren));
}
String valueName = item.getValue();
//String sorteV = dc.getSortedfuncAll().get(valueName);
item.setValue((dc.getSortedfuncAll().get(valueName)));
return item;
}
}
Your drawTable() method creates a new TreeTableView but I don't see any code where you add that TreeTableView to the UI, or remove the existing TreeTableView.
You probably want to replace
treeTable = new TreeTableView<>(root);
with
treeTable.setRoot(root);

Resources