Apply table filter - javafx-8

I have this example of Java table which generates values every second.
Short example:
MainApp.java
public class MainApp extends Application
{
private TableView<Employee> table;
private TextField txtField;
private ObservableList<Employee> data;
MyService myService;
#Override
public void start(Stage stage) throws Exception
{
Label lbl = new Label("Enter text below to filter: ");
initFilter();
initTable();
myService = new MyService();
myService.setDelay(new Duration(300));
myService.setPeriod(new Duration(1000));
myService.start();
VBox container = new VBox();
container.getChildren().addAll(lbl, txtField, table);
StackPane root = new StackPane();
root.getChildren().add(container);
Scene scene = new Scene(root, 500, 400);
stage.setScene(scene);
stage.show();
}
class MyService extends ScheduledService<Void>
{
#Override
protected Task<Void> createTask()
{
return new Task<Void>()
{
#Override
protected Void call() throws Exception
{
data = getTableData();
table.setItems(FXCollections.observableArrayList(data));
return null;
}
};
}
}
public static void main(String[] args)
{
launch(args);
}
private void initTable()
{
table = new TableView<>();
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<Employee, String> empIdCol = new TableColumn<>("Employee ID");
empIdCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Employee, String>, ObservableValue<String>>()
{
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Employee, String> p)
{
return p.getValue().empIdProperty();
}
});
TableColumn<Employee, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Employee, String>, ObservableValue<String>>()
{
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Employee, String> p)
{
return p.getValue().nameProperty();
}
});
TableColumn<Employee, Number> ageCol = new TableColumn<>("Age");
ageCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Employee, Number>, ObservableValue<Number>>()
{
#Override
public ObservableValue<Number> call(TableColumn.CellDataFeatures<Employee, Number> p)
{
return p.getValue().ageProperty();
}
});
TableColumn<Employee, String> cityCol = new TableColumn<>("City");
cityCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Employee, String>, ObservableValue<String>>()
{
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Employee, String> p)
{
return p.getValue().cityProperty();
}
});
table.getColumns().setAll(empIdCol, nameCol, ageCol, cityCol);
}
private void initFilter()
{
txtField = new TextField();
txtField.setPromptText("Filter");
txtField.textProperty().addListener(new InvalidationListener()
{
#Override
public void invalidated(Observable o)
{
if (txtField.textProperty().get().isEmpty())
{
table.setItems(data);
return;
}
ObservableList<Employee> tableItems = FXCollections.observableArrayList();
ObservableList<TableColumn<Employee, ?>> cols = table.getColumns();
for (int i = 0; i < data.size(); i++)
{
for (int j = 0; j < cols.size(); j++)
{
TableColumn col = cols.get(j);
String cellValue = col.getCellData(data.get(i)).toString();
cellValue = cellValue.toLowerCase();
if (cellValue.contains(txtField.textProperty().get().toLowerCase()))
{
tableItems.add(data.get(i));
break;
}
}
}
table.setItems(tableItems);
}
});
}
private ObservableList<Employee> getTableData()
{
ObservableList<Employee> list = FXCollections.observableArrayList();
String[] name =
{
"Sriram", "Pete", "Eric", "Dawson", "John"
};
String[] city =
{
"New York", "Chicago", "Little Rock", "Los Angeles", "Oakland"
};
for (int i = 0; i < 5; i++)
{
Employee emp = new Employee();
emp.setName(name[i]);
emp.setAge((int) (Math.random() * 100));
emp.setCity(city[i]);
emp.setEmpId(String.valueOf(i + 1000));
list.add(emp);
}
return list;
}
}
Employee.java
public class Employee {
private SimpleStringProperty name = new SimpleStringProperty();
private SimpleIntegerProperty age = new SimpleIntegerProperty();
private SimpleStringProperty city = new SimpleStringProperty();
private SimpleStringProperty empId = new SimpleStringProperty();
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getName() {
return name.get();
}
public SimpleIntegerProperty ageProperty() {
return age;
}
public void setAge(Integer age) {
this.age.set(age);
}
p
ublic Integer getAge() {
return age.get();
}
public SimpleStringProperty cityProperty() {
return city;
}
public String getCity() {
return city.get();
}
public void setCity(String city) {
this.city.set(city);
}
public SimpleStringProperty empIdProperty() {
return empId;
}
public void setEmpId(String empId) {
this.empId.set(empId);
}
public String getEmpId() {
return empId.get();
}
}
I noticed that the filter that I use to filter the content is applied only for the current Service run.
For next run the filter is not applied.

Use a FilteredList to manage the filtering. Instead of updating the list directly, replace the contents of its source list from the service. When the text in the text field changes, just update the predicate for the filtered list.
As an aside, your code updates the TableView from a background thread, which violates the threading rules of JavaFX. This is fixed in the example below.
SSCCE:
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FilteredTableViewExample extends Application {
private TableView<Employee> table;
private TextField txtField;
private FilteredList<Employee> tableData;
private ObservableList<Employee> data;
MyService myService;
#Override
public void start(Stage stage) throws Exception {
Label lbl = new Label("Enter text below to filter: ");
initFilter();
initTable();
myService = new MyService();
myService.setDelay(new Duration(300));
myService.setPeriod(new Duration(1000));
myService.start();
VBox container = new VBox();
container.getChildren().addAll(lbl, txtField, table);
StackPane root = new StackPane();
root.getChildren().add(container);
Scene scene = new Scene(root, 500, 400);
stage.setScene(scene);
stage.show();
}
class MyService extends ScheduledService<List<Employee>> {
#Override
protected Task<List<Employee>> createTask() {
Task<List<Employee>> task = new Task<List<Employee>>() {
#Override
protected List<Employee> call() throws Exception {
return getTableData();
}
};
task.setOnSucceeded(e -> data.setAll(task.getValue()));
return task ;
}
}
public static void main(String[] args) {
launch(args);
}
private void initTable() {
table = new TableView<>();
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
TableColumn<Employee, String> empIdCol = new TableColumn<>("Employee ID");
empIdCol.setCellValueFactory(p -> p.getValue().empIdProperty());
TableColumn<Employee, String> nameCol = new TableColumn<>("Name");
nameCol.setCellValueFactory(p -> p.getValue().nameProperty());
TableColumn<Employee, Number> ageCol = new TableColumn<>("Age");
ageCol.setCellValueFactory(p -> p.getValue().ageProperty());
TableColumn<Employee, String> cityCol = new TableColumn<>("City");
cityCol.setCellValueFactory(p -> p.getValue().cityProperty());
table.getColumns().setAll(empIdCol, nameCol, ageCol, cityCol);
data = FXCollections.observableArrayList();
tableData = new FilteredList<>(data);
table.setItems(tableData);
}
private void initFilter() {
txtField = new TextField();
txtField.setPromptText("Filter");
txtField.textProperty().addListener((obs, oldText, newText) -> {
if (txtField.textProperty().get().isEmpty()) {
tableData.setPredicate(employee -> true);
return;
}
tableData.setPredicate(employee -> {
String text = newText.toLowerCase();
for (TableColumn<Employee, ?> col : table.getColumns()) {
String cellValue = col.getCellData(employee).toString();
cellValue = cellValue.toLowerCase();
if (cellValue.contains(text)) {
return true;
}
}
return false;
});
});
}
private List<Employee> getTableData() {
List<Employee> list = new ArrayList<>();
String[] name = { "Sriram", "Pete", "Eric", "Dawson", "John" };
String[] city = { "New York", "Chicago", "Little Rock", "Los Angeles", "Oakland" };
for (int i = 0; i < 5; i++) {
Employee emp = new Employee();
emp.setName(name[i]);
emp.setAge((int) (Math.random() * 100));
emp.setCity(city[i]);
emp.setEmpId(String.valueOf(i + 1000));
list.add(emp);
}
return list;
}
public static class Employee {
private SimpleStringProperty name = new SimpleStringProperty();
private SimpleIntegerProperty age = new SimpleIntegerProperty();
private SimpleStringProperty city = new SimpleStringProperty();
private SimpleStringProperty empId = new SimpleStringProperty();
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getName() {
return name.get();
}
public SimpleIntegerProperty ageProperty() {
return age;
}
public void setAge(Integer age) {
this.age.set(age);
}
public Integer getAge() {
return age.get();
}
public SimpleStringProperty cityProperty() {
return city;
}
public String getCity() {
return city.get();
}
public void setCity(String city) {
this.city.set(city);
}
public SimpleStringProperty empIdProperty() {
return empId;
}
public void setEmpId(String empId) {
this.empId.set(empId);
}
public String getEmpId() {
return empId.get();
}
}
}

Related

problem with Nested recyclerview and LiveData observe

I have nested RecyclerView and two LiveData. one is parentList and another one is childList
I managed to use LiveData for ParentAdapter but when I try LiveData for ChildAdapter nothing showen in childAdapter. ParentAdapter is working.
Can someone help me?
Thanks?
this method is in MainActivity.class
private void sendAllDataToAdapter(){
CashFlowViewModel viewModel = ViewModelProviders.of(this).get(CashFlowViewModel.class);
viewModel.cashGroupByDate().observe(this, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
adapter = new MainAdapter(this, this);
recyclerView.setAdapter(adapter);
}
This is ParentAdapter
public class MainAdapter extends ListAdapter<CashFlow, MainAdapter.MainViewHolder>{
Context context;
List<CashFlow> cashFlowList = new ArrayList<>();
List<CashFlow> cashFlowListChild = new ArrayList<>();
CashflowRepository repository;
CashFlowViewModel viewModel;
LifecycleOwner lifecycleOwner;
public MainAdapter(Context context, LifecycleOwner lifecycleOwner) {
super(diffCallback);
this.context = context;
this.cashFlowList = cashFlowList;
this.cashFlowListChild = cashFlowListChild;
this.repository = repository;
this.lifecycleOwner = lifecycleOwner;
viewModel = ViewModelProviders.of((MainActivity) context).get(CashFlowViewModel.class);
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getBody().equals(newItem.getBody());
}
};
#NonNull
#Override
public MainViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.main_adapter, parent, false);
return new MainViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MainViewHolder holder, int position) {
holder.tvDate.setText(getItem(position).getAdded_date());
holder.tvIncome.setText(String.valueOf(getItem(position).getIncome()));
holder.tvExpense.setText(String.valueOf(getItem(position).getExpense()));
ChildAdapter adapter = new ChildAdapter(context);
holder.rvChild.setAdapter(adapter);
viewModel.cashGroupByDate().observe(lifecycleOwner, new Observer<List<CashFlow>>() {
#Override
public void onChanged(List<CashFlow> cashFlows) {
adapter.submitList(cashFlows);
}
});
Log.d("Child", getItem(position).getAdded_date()+"");
}
public class MainViewHolder extends RecyclerView.ViewHolder {
TextView tvDate, tvIncome, tvExpense;
RecyclerView rvChild;
public MainViewHolder(#NonNull View itemView) {
super(itemView);
tvDate = itemView.findViewById(R.id.main_adapter_date);
tvIncome = itemView.findViewById(R.id.main_adapter_income);
tvExpense = itemView.findViewById(R.id.main_adapter_expense);
rvChild = itemView.findViewById(R.id.child_recyclerview);
}
}
This is ChildAdapter
public class ChildAdapter extends ListAdapter<CashFlow, ChildAdapter.ChildViewHolder> {
Context context;
public ChildAdapter(Context context) {
super(diffCallback);
this.context = context;
}
private static final DiffUtil.ItemCallback<CashFlow> diffCallback = new DiffUtil.ItemCallback<CashFlow>() {
#Override
public boolean areItemsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull CashFlow oldItem, #NonNull CashFlow newItem) {
return oldItem.getAdded_date().equals(newItem.getAdded_date())
&& oldItem.getBody().equals(newItem.getBody())
&& oldItem.getTitle().equals(newItem.getTitle())
&& oldItem.getExpense() == newItem.getExpense()
&& oldItem.getIncome() == newItem.getIncome()
&& oldItem.getType().equals(newItem.getType());
}
};
#NonNull
#Override
public ChildViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.child_adapter, parent, false);
return new ChildViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ChildViewHolder holder, int position) {
holder.imageView.setImageResource(getItem(position).getImage_id());
holder.tvTitle.setText(getItem(position).getTitle());
if (getItem(position).getType().equals(BaseActivity.INCOME)){
holder.tvSum.setText(String.valueOf(getItem(position).getIncome()));
}
else if (getItem(position).getType().equals(BaseActivity.EXPENSE)){
holder.tvSum.setText(String.valueOf(getItem(position).getExpense()));
}
}
public class ChildViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
TextView tvTitle, tvSum;
public ChildViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.child_adapter_image);
tvTitle = itemView.findViewById(R.id.child_adapter_title);
tvSum = itemView.findViewById(R.id.child_adapter_sum);
}
}
}
This is my ViewModel.class
public class CashFlowViewModel extends AndroidViewModel {
private CashflowRepository repository;
public CashFlowViewModel(#NonNull Application application) {
super(application);
repository = new CashflowRepository(application);
}
public void insert(CashFlow cashFlow){
repository.insert(cashFlow);
}
public void update(CashFlow cashFlow){
repository.update(cashFlow);
}
public void delete(CashFlow cashFlow){
repository.delete(cashFlow);
}
public LiveData<List<CashFlow>> cashGroupByDate(){
return repository.getCashGroupByDate();
}
public LiveData<List<CashFlow>> cashByDate(String addedDate){
return repository.getCashByDate(addedDate);
}
public void insertCategory(Category category){
repository.insertCategory(category);
}
public void updateCategory(Category category){
repository.updateCategory(category);
}
public void deleteCategory(Category category){
repository.deleteCategory(category);
}
public List<Category> allCategories(String type){
return repository.getAllCategories(type);
}

JavaFx: Table view with different cell data type by row on the same column, HOW TO?

How to get different data class type on each row of the same column for a JavaFx table view?
Based on this post, here is a simple example that demonstrate how to get different data class type by row on the same column in a javaFx table view:
package application;
import java.time.LocalDateTime;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
public class Main extends Application {
private final TableView<Person<?>> tableView = new TableView<>();
private Person<Integer> person1 = new Person<>("Jacob", "Smith", 28, 4);
private Person<Integer> person2 = new Person<>("Isabella", "Johnson", 19, 5);
private Person<String> person3 = new Person<>("Bob", "The Sponge", 13, "Say Hi!");
private Person<LocalDateTime> person4 = new Person<>("Time", "Is Money", 45, LocalDateTime.now());
private Person<Double> person5 = new Person<>("John", "Doe", 32, 457.89);
private final ObservableList<Person<?>> data = FXCollections.observableArrayList(person1, person2, person3, person4,
person5);
#SuppressWarnings("unchecked")
#Override
public void start(Stage primaryStage) {
TableColumn<Person<?>, String> firstNameCol = new TableColumn<>("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
TableColumn<Person<?>, String> lastNameCol = new TableColumn<>("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
TableColumn<Person<?>, Integer> ageCol = new TableColumn<>("Age");
ageCol.setMinWidth(50);
ageCol.setCellValueFactory(new PropertyValueFactory<>("age"));
TableColumn<Person<?>, ?> particularValueCol = new TableColumn<>("Particular Value");
particularValueCol.setMinWidth(200);
particularValueCol.setCellValueFactory(new PropertyValueFactory<>("particularValue"));
tableView.setItems(data);
// Type safety: A generic array of Table... is created for a varargs
// parameter
// -> #SuppressWarnings("unchecked") to start method!
tableView.getColumns().addAll(firstNameCol, lastNameCol, ageCol, particularValueCol);
// Output in console the selected table view's cell value/class to check
// that the data type is correct.
SystemOutTableViewSelectedCell.set(tableView);
// To check that table view is correctly refreshed on data changed..
final Button agePlusOneButton = new Button("Age +1");
agePlusOneButton.setOnAction((ActionEvent e) -> {
Person<?> person = tableView.getSelectionModel().getSelectedItem();
try {
person.setAge(person.getAge() + 1);
} catch (NullPointerException npe) {
//
}
});
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(tableView, agePlusOneButton);
Scene scene = new Scene(new Group());
((Group) scene.getRoot()).getChildren().addAll(vbox);
primaryStage.setWidth(600);
primaryStage.setHeight(750);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public static class Person<T> {
private final StringProperty firstName;
private final StringProperty lastName;
private final IntegerProperty age;
private final ObjectProperty<T> particularValue;
private Person(String firstName, String lastName, Integer age, T particularValue) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.age = new SimpleIntegerProperty(age);
this.particularValue = new SimpleObjectProperty<T>(particularValue);
}
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
public Integer getAge() {
return age.get();
}
public void setAge(Integer age) {
this.age.set(age);
}
public IntegerProperty ageProperty() {
return age;
}
public T getParticularValue() {
return particularValue.get();
}
public void setParticularValue(T particularValue) {
this.particularValue.set(particularValue);
}
public ObjectProperty<T> particularValueProperty() {
return particularValue;
}
}
public static final class SystemOutTableViewSelectedCell {
#SuppressWarnings({ "rawtypes", "unchecked" })
public static void set(TableView tableView) {
tableView.getSelectionModel().setCellSelectionEnabled(true);
ObservableList selectedCells = tableView.getSelectionModel().getSelectedCells();
selectedCells.addListener(new ListChangeListener() {
#Override
public void onChanged(Change c) {
TablePosition tablePosition = (TablePosition) selectedCells.get(0);
Object val = tablePosition.getTableColumn().getCellData(tablePosition.getRow());
System.out.println("Selected Cell (Row: " + tablePosition.getRow() + " / Col: "
+ tablePosition.getColumn() + ") Value: " + val + " / " + val.getClass());
}
});
}
}
}

JavaFX TableView items do not change after changes to the table in the UI

I have a TableView which I populate with MappingItem objects. The goal is to create a mapping between an Excel source fields to database fields.
In the TableView I have two columns. One is of <MappingItem, String> and represents an Excel header. The other is of <MappingItem, GoldplusField> and represents a database field. The second column's cells are ComboBoxTableCell which has a list of fields from my DB.
The problem is that after I change the selection in the second column combobox, the MappingItem does not get updated by my selection. I tried to get the selected Cell and extract the item but I always get Null references.
This is the UI:
This is a sample code:
package tableviewexample;
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.*;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;
public class TableViewExample extends Application {
#Override
public void start(Stage primaryStage) {
TableView<MappingItem> table = new TableView<>();
// FIRST COLUMN
TableColumn<MappingItem, String> colA = new TableColumn<>("Excel Column");
colA.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<MappingItem, String>, ObservableValue<String>> () {
#Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<MappingItem, String> param) {
return new ReadOnlyObjectWrapper(param.getValue().getExcelColumnName());
}
});
//SECOND COLUMN
TableColumn<MappingItem, GoldplusField> colB = new TableColumn<>("Database Field Column");
colB.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<MappingItem, GoldplusField>, ObservableValue<GoldplusField>> () {
#Override
public ObservableValue<GoldplusField> call(TableColumn.CellDataFeatures<MappingItem, GoldplusField> param) {
return new ReadOnlyObjectWrapper(param.getValue().getGpField());
}
});
GoldplusField gp1 = new GoldplusField("T1", "fName", "First Name");
GoldplusField gp2 = new GoldplusField("T1", "phn", "Phone");
ObservableList<GoldplusField> fieldsList = FXCollections.observableArrayList(gp1, gp2);
colB.setCellFactory(ComboBoxTableCell.forTableColumn(new FieldToStringConvertor(), fieldsList));
colB.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<MappingItem, GoldplusField>>() {
public void handle(TableColumn.CellEditEvent<MappingItem, GoldplusField> e) {
GoldplusField gpf = colB.getCellData(table.getFocusModel().getFocusedItem());
System.out.println(gpf.getGpName());
MappingItem item = table.getSelectionModel().getSelectedItem();
System.out.println(item.getGpField().getGpName());
}
});
table.setEditable(true);
table.getColumns().addAll(colA, colB);
MappingItem mi1 = new MappingItem("name");
MappingItem mi2 = new MappingItem("phone");
ObservableList<MappingItem> miList = FXCollections.observableArrayList(mi1, mi2);
table.setItems(miList);
StackPane root = new StackPane();
root.getChildren().add(table);
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);
}
class FieldToStringConvertor extends StringConverter<GoldplusField> {
#Override
public String toString(GoldplusField object) {
if (object != null)
return object.getGpName();
else
return "";
}
#Override
public GoldplusField fromString(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
class MappingItem {
private String excelColumnName;
private GoldplusField gpField;
public String getExcelColumnName() { return excelColumnName; }
public void setExcelColumnName(String excelColumnName) { this.excelColumnName = excelColumnName; }
public GoldplusField getGpField() { return gpField; }
public void setGpField(GoldplusField gpField) { this.gpField = gpField; }
public MappingItem(String columnName) {
this.excelColumnName= columnName;
}
public MappingItem(GoldplusField gpField) {
this.gpField = gpField;
}
public MappingItem(String columnName, GoldplusField gpField) {
this.excelColumnName = columnName;
this.gpField = gpField;
}
}
class GoldplusField {
private String table;
private String dbName;
private String gpName;
public String getDbName() { return dbName; }
public String getGpName() { return gpName; }
public String getTable() { return table; }
public void setDbName(String dbName) { this.dbName = dbName; }
public void setGpName(String gpName) { this.gpName = gpName; }
public void setTable(String table) { this.table = table; }
public GoldplusField(String table, String dbName, String gpName) {
this.dbName = dbName;
this.gpName = gpName;
this.table = table;
}
}
}
OK. As have been mentioned, the problem was, probably, that the properties were not "writable".
I ended up changing my objects properties to JavaFX Properties. Then I set up a PropertyValueFactory for each of them and passed it to the column's CellValueFactory.
Thank you.
private void populateSourceColumnsColumn() {
ArrayList<MappingItem> items = new ArrayList<> ();
ArrayList<String> headers = SheetHelper.getTableHeadersAsString(sheet, true);
for (String header : headers) {
items.add(new MappingItem(header) );
}
ObservableList<MappingItem> itemsList = FXCollections.observableArrayList(items);
mappingTable.setItems(itemsList);
// First Column
PropertyValueFactory<MappingItem, String> fNameCellValueFactory = new PropertyValueFactory<>("excelColumnName");
inputColumnsColumn.setCellValueFactory(fNameCellValueFactory);
// Second Column
PropertyValueFactory<MappingItem, GoldplusField> gpFieldCellValueFactory = new PropertyValueFactory<>("gpField");
goldplusFieldsColumn.setCellValueFactory(gpFieldCellValueFactory);
GoldplusDatabase gpDb = new GoldplusDatabase(DatasourceContext.INSTANCE.getDataSource());
ObservableList<GoldplusField> fieldsList = FXCollections.observableArrayList(gpDb.getContactFields());
goldplusFieldsColumn.setCellFactory(ComboBoxTableCell.forTableColumn(new FieldToStringConvertor(), fieldsList));
}
public class MappingItem {
private StringProperty excelColumnName = new SimpleStringProperty(this, "excelColumnName");
private ObjectProperty<GoldplusField> gpField = new SimpleObjectProperty<GoldplusField>(this, "gpField");
public String getExcelColumnName() {
return excelColumnName.get();
}
public void setExcelColumnName(String excelColumnName) {
this.excelColumnName.set(excelColumnName);
}
public StringProperty excelColumnNameProperty() {
return excelColumnName;
}
public GoldplusField getGpField() {
return gpField.get();
}
public void setGpField(GoldplusField gpField) {
this.gpField.set(gpField);
}
public ObjectProperty gpFieldProperty() {
return this.gpField;
}
public MappingItem() {
super();
}
public MappingItem(String columnName) {
this.excelColumnName.set(columnName);
}
public MappingItem(GoldplusField gpField) {
this.gpField.set(gpField);
}
public MappingItem(String columnName, GoldplusField gpField) {
this.excelColumnName.set(columnName);
this.gpField.set(gpField);
}
}
public class GoldplusField {
private StringProperty table = new SimpleStringProperty(this, "table");
private StringProperty dbName = new SimpleStringProperty(this, "dbName");
private StringProperty gpName = new SimpleStringProperty(this, "gpName");
public String getDbName() {
return dbName.get();
}
public String getGpName() {
return gpName.get();
}
public String getTable() {
return table.get();
}
public void setDbName(String dbName) {
this.dbName.set(dbName);
}
public void setGpName(String gpName) {
this.gpName.set(gpName);
}
public void setTable(String table) {
this.table.set(table);
}
public StringProperty tableProperty() {
return this.table;
}
public StringProperty gpNameProperty() {
return this.gpName;
}
public StringProperty dbNameProperty() {
return this.dbName;
}
public GoldplusField(String table, String dbName, String gpName) {
this.dbName.set(dbName);
this.gpName.set(gpName);
this.table.set(table);
}
}

TableViewer not refreshing in RAP

I tried to run my Eclipse RCP code to run in Eclipse RAP environment. In my Eclipse RCP code, there is functionality to add the rows in to table. But
adding the code does not work in Eclipse RAP. I am using TableViewer.
Following is my code.
public class BasicEntryPoint extends AbstractEntryPoint {
private static final int COLUMNS = 2;
private TableViewer viewer;
private class ViewContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
List<Person> list = (List<Person>) inputElement;
return list.toArray();
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
private class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
Person p = (Person) element;
if (columnIndex == 0) {
return p.getName();
}
return p.getPlace();
}
}
private class Person{
String name;
String place;
public void setName(String name) {
this.name = name;
}
public void setPlace(String place) {
this.place = place;
}
public String getName() {
return name;
}
public String getPlace() {
return place;
}
}
public List<Person> persons() {
List<Person> list = new ArrayList<Person>();
Person person = new Person();
person.setName("bb");
person.setPlace("jjj");
list.add(person);
person = new Person();
person.setName("sss");
person.setPlace("fff");
list.add(person);
return list;
}
#Override
protected void createContents(Composite parent) {
parent.setLayout(new GridLayout(2, false));
viewer = new TableViewer(parent, SWT.NONE);
viewer.setContentProvider(new ViewContentProvider());
viewer.setLabelProvider(new ViewLabelProvider());
final Table table = viewer.getTable();
viewer.setColumnProperties(initColumnProperties(table));
viewer.setInput(persons());
viewer.getTable().setHeaderVisible(true);
Button checkbox = new Button(parent, SWT.CHECK);
checkbox.setText("Hello");
Button button = new Button(parent, SWT.PUSH);
button.setText("World");
button.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
System.out.println("Button clicked");
Person p = new Person();
p.setName("Dee");
p.setPlace("TCR");
persons().add(p);
String prop[] ={"name","place"};
viewer.update(p, prop);
//viewer.refresh();
}
});
}
private String[] initColumnProperties(Table table) {
String[] result = new String[COLUMNS];
for (int i = 0; i < COLUMNS; i++) {
TableColumn tableColumn = new TableColumn(table, SWT.NONE);
result[i] = "Column" + i;
tableColumn.setText(result[i]);
if (i == 2) {
tableColumn.setWidth(190);
} else {
tableColumn.setWidth(70);
}
}
return result;
}
}
You should use:
viewer.add(p);
rather than update to add a new item to a table (both for SWT and RAP).
You must also update your model to contain the new item.

Does anyone have a working examples of ActionCells working within a CompositeCell?

I tried following the example, http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTree , and added two ActionCells inside of the CompositeCell with no luck. The ActionCell's onBrowserEvent() does not get triggered.
This simple example works for me. Since you didn't provide any code or further explanation on what exactly you're trying to achieve, I have no idea whether my example is of any help or not.
public void onModuleLoad() {
CellTable<Person> table = new CellTable<Starter.Person>();
List<HasCell<Person, ?>> cells = new LinkedList<HasCell<Person, ?>>();
cells.add(new HasCellImpl("first name", new Delegate<Person>() {
#Override
public void execute(Person object) {
Window.alert(object.getFirstName());
}
}));
cells.add(new HasCellImpl("last name", new Delegate<Starter.Person>() {
#Override
public void execute(Person object) {
Window.alert(object.getLastName());
}
}));
CompositeCell<Person> cell = new CompositeCell<Person>(cells);
table.addColumn(new TextColumn<Starter.Person>() {
#Override
public String getValue(Person object) {
return object.getFirstName() + " " + object.getLastName();
}
}, "name");
table.addColumn(new Column<Person, Person>(cell) {
#Override
public Person getValue(Person object) {
return object;
}
}, "composite");
LinkedList<Person> data = new LinkedList<Starter.Person>();
data.add(new Person("Amy", "Reed"));
data.add(new Person("Tim", "Gardner"));
table.setRowData(data);
RootPanel.get().add(table);
}
private class HasCellImpl implements HasCell<Person, Person> {
private ActionCell<Person> fCell;
public HasCellImpl(String text, Delegate<Person> delegate) {
fCell = new ActionCell<Person>(text, delegate);
}
#Override
public Cell<Person> getCell() {
return fCell;
}
#Override
public FieldUpdater<Person, Person> getFieldUpdater() {
return null;
}
#Override
public Person getValue(Person object) {
return object;
}
}
private class Person {
private String fFirstName;
private String fLastName;
public Person(String first, String last) {
fFirstName = first;
fLastName = last;
}
public String getFirstName() {
return fFirstName;
}
public String getLastName() {
return fLastName;
}
}