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
)
);
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.
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.
I created a TableView via SceneBuilder so that it receives dynamic data, that is, I will always have distinct SQL scripts whose data I want to display in the TableView.
So the columns are created according to the amount of fields I detect in sql.
This is already being done. The problem is in the code for popular TableView rows.
For this, I get an ArrayList <String> with resultSet data, as it is dynamic information,
This ArrayList <String> receives each record with the fields separated by #. Then I get the
Number of fields I will have as columns and created TableView columns with similar titles To the field names. That works perfectly.
Then I created a loop to get the ArrayList <String> elements, called
Of aListString.At where:
Data [] [] has the number of rows according to the number of elements of aListString and the quantity Of columns according to the number of fields (qtdeCampos). Until now resolved.
Then the loop fills the array data [] [] without problem, each row with their respective fields.
Finally, I fill out the TableView with:
tableViewResultado.getItems().addAll(Arrays.asList(dados));
Since the TableView is defined as:
#FXML
private TableView<String[]> tableViewResultado;
However, the rows are not populated with the data data [] [], whose information is correctly Stored in this matrix. The code:
for(int j=0;j<qtdeCampos;j++)
{
TableColumn<String[],String> column = new TableColumn();
column.setText(arrayListCampos.get(j).getNomeCampo());
aTableColumn.add(column);
tableViewResultado.getColumns().add(column);
}
String dados[][] = new String[aListString.size()][qtdeCampos];
for(int i=0;i<aListString.size();i++)
{
String dadosX[]=aListString.get(i).split("#");
for(int k=0;k<dadosX.length;k++)
{
dados[i][k]=dadosX[k];
}
}
Where is the error?
Thank you for your attention.
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
}
}
});