How to get a value from a selected row in jfxtreetableview - javafx

I have searched all over the internet but couldn't find any easy way to get selected value from a jfxtreetableview like good old jtable anyone knows about it?
I have created a jfxtreetableview and populated data.
JFXTreeTableColumn<Person, String> address = new JFXTreeTableColumn("Address");
address.setPrefWidth(100);
address.impl_setReorderable(false);
address.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>()
{
#Override public ObservableValue<String> call(TreeTableColumn.CellDataFeatures<Person, String> param)
{
return param.getValue().getValue().address;
}
});

You can get selected item by doing this :
yourTreeTableView.getSelectionModel().getSelectedItem();

Related

How to Generate Message According to Values in Multiple Fields?

I am building an application using JavaFX. What I am trying to do is generate a message according to the user input values. So there are one text-field and one combo-box and one check-box per row and there are many rows like the following.
Let's say I will generate three different messages according to the user values. So I need to check whether those fields are empty or not and check each field's value to generate a specific message. Checking fields are okay for just three rows like the above. But I have 10 fields. So I have to check each and generate or append my own message. And also if the user checked the check-box need to group all checked row values. So what I am asking is there any good way (best practice) to achieve what I need or an easy one also? I have tried with HashMap and ArrayList. But those are not working for this.
Really appreciate it if anybody can help me. Thanks in advance.
I would probably recommend a custom node that you create on your own like below. This example is not supposed to have the same functionality as your application but just to show how to create and use custom nodes. I kept your idea in mind when creating this example it has your textfield combobox and checkbox and they are a little easier to manage. Give it a run and let me know if you have any questions
public class Main extends Application {
#Override
public void start(Stage stage) {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
ArrayList<String> itemList = new ArrayList<>(Arrays.asList("Dog", "Cat", "Turkey"));
ArrayList<HBoxRow> hBoxRowArrayList = new ArrayList<>();
for (int i = 0; i<3; i++) {
HBoxRow hBoxRow = new HBoxRow();
hBoxRow.setComboBoxValues(FXCollections.observableList(itemList));
hBoxRowArrayList.add(hBoxRow);
vBox.getChildren().add(hBoxRow.gethBox());
}
Button printTextfieldsButton = new Button("Print Textfields");
printTextfieldsButton.setOnAction(event -> {
for (HBoxRow hBoxRow : hBoxRowArrayList) {
System.out.println("hBoxRow.getTextFieldInput() = " + hBoxRow.getTextFieldInput());
}
});
vBox.getChildren().add(printTextfieldsButton);
stage.setScene(new Scene(vBox));
stage.show();
}
//Below is the custom Node
public class HBoxRow {
HBox hBox = new HBox();
ComboBox<String> comboBox = new ComboBox<>();
TextField textField = new TextField();
CheckBox checkBox = new CheckBox();
public HBoxRow(){
hBox.setAlignment(Pos.CENTER);
textField.setPrefWidth(150);
comboBox.setPrefWidth(150);
checkBox.setOnAction(event -> {
textField.setDisable(!textField.isDisabled());
comboBox.setDisable(!comboBox.isDisabled());
});
hBox.getChildren().addAll(checkBox, textField, comboBox);
}
public void setComboBoxValues(ObservableList observableList) {
comboBox.setItems(observableList);
}
public HBox gethBox(){
return hBox;
}
public String getTextFieldInput(){
return textField.getText();
}
}
}

JavaFx Table View Memory leakages

I have created table View in JavaFx 2x,It's has more than 10K rows and each row has 20 columns, Whenever table view is loading java heap old gen memory is increasing and it's not releasing. After loading the same table for 2-3 times(refresh button and every time before loading the table clearing all the references mentioned below). My Application is freezing or goes to not responding state, I have tried have all the options clear the references form memory but no luck
#FXML
private TableView<PersonModel> personlicense;
private void initTable(){
licState
.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<PersonModel, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(
final CellDataFeatures<PersonModel, String> cdf) {
return cdf.getValue().iconStateProperty();
}
});
colCompliance
.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<PersonModel, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(
final CellDataFeatures<PersonModel, String> cdf) {
return cdf.getValue().complianceDetailStrProperty();
}
});
}
priavte void populate(){
final ObservableList<PersonModel> itemsMultiTable = personlicense.getItems();
if (!itemsMultiTable.isEmpty()) {
itemsMultiTable.removeAll(itemsMultiTable);
clean();
}
for (final Person personObj : selectedPersons) {
final PersonModel LicModel = personObj.getLicense();
itemsMultiTable.add(LicModel);
}
}
private void clean(){
Class tcb=TableCellBehavior.class;
try{
Method anchormethod=tcb.getDeclaredMethod("setAnchor",TableView.class,TablePosition.class);
anchormethod.setAccessible(true);
anchormethod.invoke(null,resumeTable,null);
}
catch(Throwable t){
throw new RuntimeException(t);
}
personlicense.setFocusModel(null);
personlicense.setOnMouseClicked(null);
personlicense.setSelectionModel(null);
personlicense.getColumns().clear();
personlicense.getItems().clear();
}
I had seen some posts in this forum are suggesting to upgrade to java8, but my client is not willing to upgrade
is there any possible solution to release the memory...?
MAT Analysis is below
MAT analysis report
Thank you

How to make Hyperlink TableColumn in TableView editable

TableColumn<ComponentObject, Hyperlink> template_id = new TableColumn<ComponentObject, Hyperlink>("Template Id");
template_id.setCellValueFactory(
new PropertyValueFactory<ComponentObject, Hyperlink>("template"));
template_id.setCellFactory(TextFieldTableCell.forTableColumn());
template_id.setOnEditCommit(
new EventHandler<CellEditEvent<ComponentObject, Hyperlink>>() {
#Override
public void handle(CellEditEvent<ComponentObject, Hyperlink> t) {
((ComponentObject) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setTemplate((javafx.scene.control.Hyperlink) t.getNewValue());
}
}
);
Here setCellFactory is giving error as
"
The method setCellFactory(Callback<TableColumn<ComponentObject,Hyperlink>,TableCell<ComponentObject,Hyperlink>>) in
the type TableColumn<ComponentObject,Hyperlink> is not applicable for the arguments
(Callback<TableColumn<Object,String>,TableCell<Object,String>>)
"
How to solve this? I want to make template_id column editable..but note that it is hyperlink..
The quick and dirty way is to specify a stringConverter for the TextFieldTableCell:
StringConverter<Hyperlink> converter = new StringConverter<Hyperlink>() {
#Override
public Hyperlink fromString(String string) {
return new Hyperlink(string);
}
#Override
public String toString(Hyperlink hyperlink) {
return hyperlink.getText();
}
}
template_id.setCellFactory(TextFieldTableCell.forTableColumn(converter));
In general, though, it is a very bad idea to represent your data with UI nodes, which is what you are doing by making the column type a Hyperlink. Strange things may happen here. It would be better to make the data type String, and to implement your own TableCell that showed a Hyperlink when not in editing mode, and a TextField when in editing mode.

How do you create a table cell factory in JavaFX to display a ChoiceBox?

I am trying to display a ChoiceBox inside of a TableView in JavaFX. Right now I am just trying to test if I can get this working, so I am generating fake data within the cell factory, but I can't even get this to work.
My IDE is giving me the error that
forTableColumn (javafx.collections.ObservableList<T>) in ChoiceBoxTableCell cannot be applied
to (javafx.collections.ObservableList<java.lang.String>)
Here is my code.
private ListView<RequirementsProperty> guiPropertyList;
private TableColumn<RequirementsProperty, String> guiSpecifierColumn;
guiSpecifierColumn.setCellFactory(
new Callback<TableColumn<RequirementsProperty, String>, TableCell<RequirementsProperty, String>>() {
#Override
public TableCell<RequirementsProperty, String> call(TableColumn<RequirementsProperty, String> param) {
ObservableList<String> testlist = FXCollections.observableArrayList("A", "B", "C");
return ChoiceBoxTableCell.forTableColumn(testlist);
}
});
Does anyone know what I am doing wrong? I just want to display the list containing A, B, and C right now, and then I can move on to displaying my own data.
ChoiceBoxTableCell.forTableColumn(...) returns a Callback itself (i.e. it returns the cellFactory, not the cell).
You can just do
ObservableList<String> testlist = FXCollections.observableArrayList("A", "B", "C");
guiSpecifierColumn.setCellFactory(ChoiceBoxTableCell.forTableColumn(testlist));
If you are implementing the factory yourself, its call(...) method needs to return the actual cell. In this case you would just construct a ChoiceBoxTableCell directly and return it:
guiSpecifierColumn.setCellFactory(
new Callback<TableColumn<RequirementsProperty, String>, TableCell<RequirementsProperty, String>>() {
#Override
public TableCell<RequirementsProperty, String> call(TableColumn<RequirementsProperty, String> param) {
ObservableList<String> testlist = FXCollections.observableArrayList("A", "B", "C");
return new ChoiceBoxTableCell(testlist);
}
});

JavaFX- how to call setCellValueFactory for all rows even if they are not showing?

My tableview contains more rows than what it can show so it automatically adds a vertical scrollbar, the problem is that I have some code I want to run with the setCellValueFactory method but this method doesn't get called until the rows are rendered, is there a way to force this code to run even if the rows are not showing?
TableColumn<Row, String> col = new TableColumn<>("Column");
col
.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Row, String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(
final CellDataFeatures<Row, String> p) {
System.out.println("Should run before rendering");
return new ReadOnlyObjectWrapper("Value");
}
});

Resources