JavaFX ComboBox displaying object not it's property - javafx

i have built GUI with combo box. I have ObservableList<SimpleTableObject> types
which should display types of material. It looks like this
material_comboBox_type.getItems().addAll(types);
material_comboBox_type.setCellFactory((ListView<SimpleTableObject>
param) -> {
final ListCell<SimpleTableObject> cell = new
ListCell<SimpleTableObject>() {
#Override
public void updateItem(SimpleTableObject item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getName().get());//return String, actuall name of material
}
else {
setText(null);
}
}
};
return cell;
});
Now the problem is this: when i click combobox, it shows names as desired. But when i select one, instead of the string property, an object itself is displayed, which looks like that classes.SimpleTableObject#137ff5c.
How can I achieve it?

The selected item in a combo box is displayed in a cell called the buttonCell. So you need to set the button cell as well as the cell factory (which generates the cells in the dropdown).
To do this, it's probably easier to refactor your cell implementation as a (named) inner class:
private static class SimpleTableObjectListCell extends ListCell<SimpleTableObject> {
#Override
public void updateItem(SimpleTableObject item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getName().get());//return String, actuall name of material
}
else {
setText(null);
}
}
}
And then:
materialComboBoxType.setCellFactory(listView -> new SimpleTableObjectListCell());
materialComboBoxType.setButtonCell(new SimpleTableObjectListCell());

Ok, i did this with converter:
material_comboBox_type.setConverter(new StringConverter<SimpleTableObject>() {
#Override
public String toString(SimpleTableObject object) {
return object.getName().get();
}
#Override
public SimpleTableObject fromString(String string) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
});

Related

Unable select Javafx combobox items programmatically which uses objects

In the JavaFx ComboBox which uses a class object list .I want to select items in the ComboBox programmatically using getSelectionModel().select(object or index). i am not getting the desired result Although the value is set but it is something like this main.dao.Company.Company.CompanyTableData#74541e7b.
The code is somewhat like this.
ComboBox<CompanyTableData> company = new ComboBox<>();
company.setItems(GetCompany.getCompanyTableData());//where Observable list is set..
GetCompany.getCompanyTableData() returns observablelist of CompanyTableData class.
The ComboBox Looks as follows.
The CompanyTableData Class is as.
public class CompanyTableData {
private SimpleStringProperty itemCompanyId;
private SimpleStringProperty itemCompanyName;
private SimpleStringProperty createBy;
private SimpleStringProperty createdOn;
public CompanyTableData(CompanyData companyData){
this.itemCompanyId = new SimpleStringProperty(companyData.getItemCompanyId());
this.itemCompanyName = new SimpleStringProperty(companyData.getItemCompanyName());
this.createBy = new SimpleStringProperty(companyData.getCreatedBy());
this.createdOn = new SimpleStringProperty(companyData.getCreatedOn());
}
public String getItemCompanyId() {
return itemCompanyId.get();
}
public SimpleStringProperty itemCompanyIdProperty() {
return itemCompanyId;
}
public void setItemCompanyId(String itemCompanyId) {
this.itemCompanyId.set(itemCompanyId);
}
public String getItemCompanyName() {
return itemCompanyName.get();
}
public SimpleStringProperty itemCompanyNameProperty() {
return itemCompanyName;
}
public void setItemCompanyName(String itemCompanyName) {
this.itemCompanyName.set(itemCompanyName);
}
public String getCreateBy() {
return createBy.get();
}
public SimpleStringProperty createByProperty() {
return createBy;
}
public void setCreateBy(String createBy) {
this.createBy.set(createBy);
}
public String getCreatedOn() {
return createdOn.get();
}
public SimpleStringProperty createdOnProperty() {
return createdOn;
}
public void setCreatedOn(String createdOn) {
this.createdOn.set(createdOn);
}
}
The Cell Factory is set
company.setCellFactory(param -> new CompanyCell());
And the CompanyCell
public class CompanyCell extends ListCell<CompanyTableData> {
#Override
protected void updateItem(CompanyTableData item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.getItemCompanyName() == null) {
setText(null);
} else {
setText(item.getItemCompanyName());
}
}
}
After all this when i try to set the items programmetically as
company.getSelectionModel().select(getSelectedCompanyIndex());
The getSelectedCompanyIndex() function is as follows.
public static CompanyTableData getSelectedCompanyIndex(){
CompanyTableData c = null,i;
Iterator<CompanyTableData> itr = GetCompany.getCompanyTableData().iterator();
while (itr.hasNext()){
i = itr.next();
if (i.getItemCompanyName().equals(Element.getItemTableData().getCompany())){
c = i;
}
}
return c;
}
And the result i am getting is
And
At the end it should select a name or item in the list but it has set some type of object i think.
Now what should i do. Is there any type of string conversion required.
The buttonCell used to display the item when the combobox popup is not shown is not automatically created using the cellFactory. You need to set this property too to use the same cell implementation:
company.setCellFactory(param -> new CompanyCell());
company.setButtonCell(new CompanyCell());

Filter Data from RecyclerView Firebase

I am trying to implement a searchview in my action bar and for that to filter the RecyclerView Data that I retrieved from Firebase. Right now I am looking for the code that I need to add to the recycleradapter to be able to filter the retrieved data.
This is how I added the recyclerView to my MainActivity.
Query query = mRef.orderByChild("city");
// everything else
FirebaseRecyclerAdapter<City, CityViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<City, CityViewHolder>(
City.class,
R.layout.city_starter,
CityViewHolder.class,
query
) {
#Override
protected void populateViewHolder(CityViewHolder viewHolder, City city, int position) {
viewHolder.setDetails(getApplicationContext(),
city.getCity());
}
// for click of parent item
#Override
public CityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CityViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new CityViewHolder.ClickListener() {
#Override
public void onItemClick(View view, int position) {
//get Data from Firebase
String cCity = getItem(position).getCity();
// pass this data to new activity
Intent secondMainActivity = new Intent(view.getContext(), SecondMainActivity.class);
secondMainActivity.putExtra("city", cCity);
startActivity(secondMainActivity);
}
#Override
public void onItemLongClick(View view, int position) {
// possible to implement another way of interacting for a long click
}
});
return viewHolder;
}
};
// set adapter to recyclerview
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
Is there anybody who is able to help?
Thank you :)
This is what I found searching for solutions but I don't get it how to adapt it to my code:
public void filter(String text) {
items.clear();
if(text.isEmpty()){
items.addAll(itemsCopy);
} else{
text = text.toLowerCase();
for(PhoneBookItem item: itemsCopy){
if(item.name.toLowerCase().contains(text) || item.phone.toLowerCase().contains(text)){
items.add(item);
}
}
}
notifyDataSetChanged();
}
You can use android's Filterable interface..
Follow this link for example
Firstly implements Filterable to your adapter class.. You can use another array list of same type you are using for storing and showing filtered items.
Implements Filterable interface's method getFilter()
Example Code...
#Override
public Filter getFilter() {
return new Filter() {
#Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
contactListFiltered = contactList;
} else {
List<Contact> filteredList = new ArrayList<>();
for (Contact row : contactList) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getName().toLowerCase().contains(charString.toLowerCase()) || row.getPhone().contains(charSequence)) {
filteredList.add(row);
}
}
contactListFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = contactListFiltered;
return filterResults;
}
#Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
contactListFiltered = (ArrayList<Contact>) filterResults.values;
// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
Add search view into your toolbar....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
mAdapter.getFilter().filter(query);
return false;
}
#Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
mAdapter.getFilter().filter(query);
return false;
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
// close search view on back button pressed
if (!searchView.isIconified()) {
searchView.setIconified(true);
return;
}
super.onBackPressed();
}

How can I create a combo box from an object that has an arraylist of objects in javafx?

If I have an object that has an arrayList of objects:
class Event{
private ArrayList<Room> rooms;
//..
public void setRooms(ArrayList<Room> rooms) {
this.rooms = rooms;
}
public ArrayList<Room> getRooms() {
return rooms;
}
}
//---------------------
class Room{
private String roomId;
private String roomName;
public Room(String roomId, String roomName) {
this.roomId = roomId;
this.roomName = roomName;
}
public String getRoomId() {
return roomId;
}
public String getRoomName() {
return roomName;
}
public void setRoomId(String roomId) {
this.roomId = roomId;
}
public void setRoomName(String roomName) {
this.roomName = roomName;
}
}
How can I create a combobox in my table from the array of room objects?
What I have that is only showing an object identifier of some sort.
TableColumn<Event, ArrayList> roomsColumn = new TableColumn<>("Room Select");
roomsColumn.setMinWidth(200);
roomsColumn.setCellValueFactory(new PropertyValueFactory<>("rooms"));
//Create an observable list to populate the table with.
ObservableList<Event> eventList = FXCollections.observableArrayList();
//loop the json to populate the observable list
for (Event event : events.getEventList() ){
eventList.add(event);
}
//populate the table
eventTable.setItems(eventList);
eventTable.getColumns().addAll(eventColumn, bDateColumn, eDateColumn, roomsColumn);
**All of the columns are built but the rooms column shows a comma separated list of room objects:
com.***.Room#345, com.***.Room#653, com.***.Room#889
You need a custom cell factory to return a TableCell with a ComboBox.
roomsColumn.setCellFactory(call -> {
// create a new cell for array lists
return new TableCell<Event, ArrayList<String>>() {
#Override
protected void updateItem(ArrayList<String> item, boolean empty) {
super.updateItem(item, empty);
// if there is no item, return an empty cell
if (empty || item == null) {
setGraphic(null);
}
else {
ComboBox<String> box = new ComboBox<>();
// set combo box items
box.setItems(FXCollections.observableArrayList(item));
// listen for changes
box.valueProperty().addListener((observable, oldValue, newValue) -> {
System.out.println("new room "+newValue);
});
// set cell contents
setGraphic(box);
}
}
};
});

JavaFX PropertyGrid/Editor

I am trying to write a JavaFx component which is generally called a "property editor" or a "property grid". A property being a name-value pair.
I guess the property sheet is made for this, but I'd like to use a TreeTableView. Mainly because I have nested properties and eventually several columns.
The component on the right is exactly what I try to achieve.
The problem I encountered with the TreeTableView, is the fact that the cell customisation must occur in the CellFactory which leads to a switch on the item type. This solution makes things really unflexible.
For example, what happens if a string value must be updated via a TextField for a given property and via a ComboBox for another property?
Any suggestion is more than welcome!
Relating questions: javafx-8-custom-listview-cells-its-evil
Update1
I tried to implement the #fabian's 1st suggestion.
I have my bean:
public class PropertyItem {
private StringProperty name = new SimpleStringProperty("");
private EditableItem value;
...
}
A default implementation of the EditableItem, to edit a string via in a TextField:
public class DefaultEditableItem implements EditableItem {
String value = "init value";
private TextField textField = new TextField();
public DefaultEditableItem(String value) {
this.setValue(value);
}
// implementations of assignItem, removeItem, startEdit, cancelEdit,... as suggested for the cell behavior
}
My implementation of the TableView:
PropertyItem rootProp = new PropertyItem("ROOT", new DefaultEditableItem("test roots"));
TreeItem<PropertyItem> root = new TreeItem(rootProp);
// the name column is straightforward ...
// value column
TreeTableColumn<PropertyItem, EditableItem> valueColumn = new TreeTableColumn<>("VALUE");
valueColumn.setCellValueFactory(new Callback<TreeTableColumn.CellDataFeatures<PropertyItem, EditableItem>, ObservableValue<EditableItem>>() {
#Override
public ObservableValue<EditableItem> call(TreeTableColumn.CellDataFeatures<PropertyItem, EditableItem> cellData) {
TreeItem<PropertyItem> treeItem = cellData.getValue();
PropertyItem propertyItem = treeItem.getValue();
// this will not compile...
return propertyItem.value();
}
});
valueColumn.setCellFactory(new Callback<TreeTableColumn<PropertyItem, EditableItem>, TreeTableCell<PropertyItem, EditableItem>>() {
#Override
public TreeTableCell<PropertyItem, EditableItem> call(TreeTableColumn<PropertyItem, EditableItem> param) {
return new EditingTreeTableCell();
}
});
valueColumn.setOnEditCommit(...)
treeTableView.getColumns().addAll(nameColumn, valueColumn);
treeTableView.setEditable(true);
My problem is on the cellValueFactory which needs to return a ObservableValue. What should I do, given that I want this column to be editable?
I guess that EditableItem must extends Property? But then, could my DefaultEditableItem extends SimpleStringProperty?
You could store information about how the item should be edited in the item itself (either directly or by allowing you to retrieve it from a map or similar data structure using a suitable key stored in the item).
Example:
public interface EditableItem {
/**
* Modify cell ui the way updateItem would do it, when the item is
* added to the cell
*/
void assignItem(EditingTreeTableCell<?, ?> cell);
/**
* Modify cell ui to remove the item the way it would be done in the updateItem method
*/
void removeItem(EditingTreeTableCell<?, ?> cell);
}
public class EditingTreeTableCell<U, V> extends TreeTableCell<U, V> {
#Override
public void updateItem(V item, boolean empty) {
boolean cleared = false;
V oldItem = getItem();
if (oldItem instanceof EditableItem) {
((EditableItem) oldItem).removeItem(this);
cleared = true;
}
super.updateItem(item, empty);
if (empty) {
if (!cleared) {
setText("");
setGraphic(null);
}
} else {
if (item instanceof EditableItem) {
((EditableItem) item).assignItem(this);
} else {
setText(Objects.toString(item, ""));
// or other default initialistation
}
}
}
}
As this however would increase the size of the items, you could also store the info based on the type of the bean the property recides in and the name of the property, that is if the bean and the name property are assigned for the property:
public interface CellEditor<U, V> {
/**
* Modify cell ui the way updateItem would do it, when the item is
* added to the cell
*/
void assignItem(EditorTreeTableCell<U, V> cell, V item);
/**
* Modify cell ui to remove the item the way it would be done in the updateItem method
*/
void removeItem(EditorTreeTableCell<U, V> cell);
}
public class EditorTreeTableCell<U, V> extends TreeTableCell<U, V> {
public EditorTreeTableCell(Map<Class, Map<String, CellEditor<U, ?>>> editors) {
this.editors = editors;
}
private CellEditor<U, V> editor;
private final Map<Class, Map<String, CellEditor<U, ?>>> editors;
#Override
public void updateIndex(int i) {
if (editor != null) {
editor.removeItem(this);
editor = null;
}
ObservableValue<V> observable = getTableColumn().getCellObservableValue(i);
if (observable instanceof ReadOnlyProperty) {
ReadOnlyProperty prop = (ReadOnlyProperty) observable;
String name = prop.getName();
Object bean = prop.getBean();
if (name != null && bean != null) {
Class cl = bean.getClass();
while (editor == null && cl != null) {
Map<String, CellEditor<U, ?>> map = editors.get(cl);
if (map != null) {
editor = (CellEditor) map.get(name);
}
cl = cl.getSuperclass();
}
}
}
super.updateIndex(i);
}
public void updateItem(V item, boolean empty) {
super.updateItem();
if (editor == null) {
setGraphic(null);
setText(Objects.toString(item, ""));
} else {
editor.assignItem(this, item);
}
}
}
This would allow you to select the editor based on the object name and type of bean the object belongs to...

JavaFX TableView: format one cell based on the value of another in the row

I have a class named TransactionWrapper I'm using to populate my ObservableList for the TableView in my application. This wrapper has an attribute (enum) indicating whether it is a withdrawal or a deposit. I need to get to that to render/format the amount cell (display it in red or green based on the nature of the transaction) and I'm not finding anything out there that is helping me with that battle.
Basically what I want to do is look at the row and say if the type is withdrawal, color the text red and if it's a deposit color it green... I'm hoping someone here can help me out with this. I'll post below my attempt at it with setCellFactory as I found in other places. This approach allows me to format the cell and how it is displayed but the problem is inside of the updateItem function, I can get to my value of my transaction type.
amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>, TableCell<TransactionWrapper, String>>()
{
#Override
public TableCell<TransactionWrapper, String> call(
TableColumn<TransactionWrapper, String> param)
{
return new TableCell<TransactionWrapper, String>()
{
#Override
protected void updateItem(String item, boolean empty)
{
if (!empty)
{
// should be something like (transaction.getType().equals(TransactionTypes.DEPOSIT) ? true : false;)
boolean isDeposit = true;
setText(item);
if(isDeposit) // should be if type is deposit
{
setTextFill(Color.GREEN);
}
else
{
setTextFill(Color.RED);
}
}
}
};
}
});
And here's how I'm setting up my column:
amountCol.setCellValueFactory(cellData -> cellData.getValue().getAmountString());
That is running off of an object called TransactionWrapper with the fol:
private final StringProperty transactionTypeString;
private final StringProperty dateString;
private final StringProperty amountString;
private final StringProperty payeeString;
private final StringProperty categoryString;
private final StringProperty notesString;
private Transaction transaction;
Any ideas on this would be much appreciated. :D
Thanks,
Jon
Figured it out! Thanks for the idea James, but I went a bit of a different way. Here's the code for anyone in the future reading this post:
amountCol.setCellFactory(new Callback<TableColumn<TransactionWrapper, String>,
TableCell<TransactionWrapper, String>>()
{
#Override
public TableCell<TransactionWrapper, String> call(
TableColumn<TransactionWrapper, String> param)
{
return new TableCell<TransactionWrapper, String>()
{
#Override
protected void updateItem(String item, boolean empty)
{
if (!empty)
{
int currentIndex = indexProperty()
.getValue() < 0 ? 0
: indexProperty().getValue();
TransactionTypes type = param
.getTableView().getItems()
.get(currentIndex).getTransaction()
.getTransactionType();
if (type.equals(TransactionTypes.DEPOSIT))
{
setTextFill(Color.GREEN);
setText("+ " + item);
} else
{
setTextFill(Color.RED);
setText("- " + item);
}
}
}
};
}
});
The param.getTableView().getItems().get(currentIndex) was key.. had to drill into the parent a bit there, but it got the job done. The biggest challange there was finding the index. Felt a bit silly when I figure out that the indexProperty() function existed.. lol. Didn't think to look at the class level functions that were available. Happy coding!

Resources