Is it possible to take a value of table column from a event handler in Javafx?
The task is I need to click a button, calculate functions, and return the value to the column.
I have this working:
data.add(new Person(id, name));
I have this:
TableColumn nameCol = new TableColumn ("name");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
// And id as well.
TableColumn resultsCol = new TableColumn ("Results");
resultsCol.setCellValueFactory(new PropertyValueFactory<>("results"));
And this:
add.setOnAction (e -> {
// I don't know what to put
});
Because id and name column are already working, what should I do to separately add a new value to the column from a button?
Because your CellValueFactories are pulling values directly from your Person object you would need to update the variables in the particular row's Person object when your button is clicked. You can do this with simple setter methods.
You could also use a technique like this to create a table with cells that can be edited with a click: http://java-buddy.blogspot.com/2012/04/javafx-2-editable-tableview.html?m=1
I have successfully implemented this technique in my applications.
Related
I have started using JavaFX for a few weeks. Here is a code snap of a TableView
#FXML protected void handleSubmitButtonAction(ActionEvent event) {
log.debug("handleSubmitButtonAction");
service.save(new InputText(inputText.getText()));
List<OutputData> outputDataList = new ArrayList<>();
service.getAll().forEach(e -> outputDataList.add(new OutputData(e)));
ObservableList<OutputData> list = FXCollections.observableArrayList(outputDataList);
outputView.setItems(list);
inputText.setText("");
}
What it does is that
Create a new entry
Save it to DB
Fetch all entries from DB
Create TableView row data for each entry
Create an ObservableList with all of the row data
Add the list to the TableView
That certainly isn't effective. Is a way to add a newly created entry directly to the TableView?
Here is what I would do.
Have service.save(new InputText(inputText.getText())); return a
true if the data was saved to the DB and false otherwise.
If true is returned, I would then add the data to the TableView
using outputView.getItems().add(inputText.getText());
If false is retured, I would use an Alert to let the user know
that the data was not added to the DB.
Receive my greetings everyone!I'm new to javafx.For my first project of Javafx ,I want to create a tableview which Tablecolumns changes according to an event.Suppose that first time,My tableview has 4 columns such as (name,age,email,address).After an event,I want to add the tablecolumn profession for my tableview to have now 5 columns (name,age,email,adress,profession).After an other event,I want to remove profession for my tableview to have again 4 tablecolumns.Thank you for your help.Excuse me for my english.
Your table view has four columns so you already know how to add columns to the table view. Adding another is no different,(unless you originally defined your columns using FXML). Regardless, the addition and removal of columns in code is as demonstrated below.
Create your new column (add appropriate generic type info and initialization code):
final TableColumn fifthColumn = new TableColumn("Alien resistance");
// initialize the column
When you receive an event, add the fifth column:
Button insurrection = new Button("Add"):
insurrection.setOnAction(e ->
tableView.getColumns().add(
fifthColumn
)
);
similarly for the action on a remove button:
Button failedCoup = new Button("Remove"):
failedCoup.setOnAction(e ->
tableView.getColumns().remove(
fifthColumn
)
);
I've made an editable cell in my javafx table using http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/table-view.htm#CJAGAAEE, but whatever I try I cannot get the new value. Instead I always get the value in the first column using
firstNameCol.setOnEditCommit(
(CellEditEvent<Person, String> t) -> {
((Person) t.getTableView().getItems().get(
t.getTablePosition().getRow())
).setFirstName(t.getNewValue());
});
(The code example in the link above)
My code is large, its my project but roughly my code so far for this section is.
timeColumn.setCellFactory(TextFieldTableCell.forTableColumn());
timeColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent>() {
#Override
public void handle(TableColumn.CellEditEvent event) {
tableView.getItems().get(event.getTablePosition().getRow());
}
});
I am editing a cell in row 1, column 2 but for some reason it is always returning the object in row 1 column 1 and not the new value in row 1 column 2.
Any advice?
I've event tried
tableView.getItems().get(event.getTablePosition().getRow()) + 1;
But this did not work
Based on #James_D comment above. If you want to get the first column's value then just do
column2.setOnEditCommit( e -> {
//here you can get the value of column1 corresponding to column2
System.out.println(e.getRowValue().getColumn1());
});
This should help you get the correct person.
TableView tempTable = (TableView)event.getTableView();
Person tempPerson = (Person)tempTable.getItems().get(event.getTablePosition().getRow());
tempPerson.setYourMethod(event.getNewValue());//maybe toString?
If your object is not a Person object, then replace Person with your object type.
I have followed this 1 tutorial to create a tableview with filtering. It works great, but the code in the tutorial creates a new SortedList (sortedData) with the sorted data and binds it with comparotorproperty to the table view, which originally uses a ObservableList as data source.
SortedList<Record> sortedData = new SortedList<Record>(filteredData);
// 4. Bind the SortedList comparator to the TableView comparator.
sortedData.comparatorProperty().bind(tableView.comparatorProperty());
// 5. Add sorted (and filtered) data to the table.
tableView.setItems(sortedData);
In my code I also have a Pie Chart and Bar Chart. They also use the ObservableList as data sources, so my question is ; how can I bind the sortedData to the ObservableList?
My code to get the data to the PieChart (and BarChart) is this:
for (Record record : dataen) {
dataList.add(new PieChart.Data(record.getFieldMonth(), record.getFieldValue()));
}
Where record is a class with Getters and Setters.
So you want to turn ObservableList<Record> into ObservableList<PieChart.Data>, correct? For that you can use EasyBind:
ObservableList<PieChart.Data> chartData = EasyBind.map(sortedData,
r -> new PieChart.Data(r.getFieldMonth(), r.getFieldValue()));
pieChart.setData(chartData);
because I don't really know the sollution.
Lets say i have a TableView that holds info about product: description, quantity and the price. Also have a label that shows a sum of prices (times quantity) of all products from a table. Now i want to be notified about any changes that took affect on this component like changes in existing rows or addition of a new one. Is there any listener or sth similar to achieve it?
Usually you construct a TableView by passing an ObservableList to it. Something like this:
TableView myTable = new TableView<>(myObservableList);
ObservableList<ClassOfTheObjectsInTheList> myObservableList = FXCollections.FXCollections.observableArrayList(anyNoneObservableCollection);
TableView<ClassOfTheObjectsInTheList> myTable = new TableView<>(myObservableList);
You can add a ListChangeListener to any ObservableList at will by doing:
myObservableList.addListener(new ListChangeListener<ClassOfObjectsInTheList>(){
#Override
public void onChanged(javafx.collections.ListChangeListener.Change<? extends ClassOfObjectsInTheList> pChange) {
while(pChange.next()) {
// Do your changes here
}
}
});