Javafx ComboBox disappearing items after select - image

I have created a (JavaFX) combobox, which I am populating with an observable list made from HBoxes, so that I can display an image with some text in each list cell.
This displays well, other than the fact that whenever you select one of the items in the list, it will disappear. Once you have selected every item, it will not render any items at all. (You can still select them by clicking in the space where they previously were.
Do you know how I might correct this, please?
Part of my code is displayed below:
public class IconListComboBox {
Group listRoot = new Group();
VBox mainVBox = new VBox();
ComboBox selectionBox = new ComboBox();
List<HBox> list = new ArrayList<HBox>();
ListView<HBox> listView = new ListView<HBox>();
ObservableList<HBox> observableList;
public IconListComboBox(int dimensionX, int dimensionY, ArrayList<String> names, ArrayList<ImageView> icons)
{
//VBox.setVgrow(list, Priority.ALWAYS);
selectionBox.setPrefWidth(dimensionY);
selectionBox.setPrefHeight(40);
for(int i = 0; i < names.size(); i++)
{
HBox cell = new HBox();
Label name = new Label(names.get(i));
Label icon = new Label();
icon.setGraphic(icons.get(i));
name.setAlignment(Pos.CENTER_RIGHT);
icon.setAlignment(Pos.CENTER_LEFT);
icon.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(icon, Priority.ALWAYS);
cell.getChildren().add(icon);
cell.getChildren().add(name);
list.add(cell);
}
observableList = FXCollections.observableList(list);
listView.setItems(observableList);
listView.setPrefWidth(dimensionX);
selectionBox.setMaxWidth(dimensionX);
listView.setMaxWidth(dimensionX);
selectionBox.setItems(observableList);
mainVBox.getChildren().add(selectionBox);
mainVBox.getChildren().add(listRoot);
//mainVBox.getChildren().add(listView);
//listRoot.getChildren().add(listView);
}
Thanks in advance for your assistance!

Ok, so I've managed to work this out, thanks to #James_D 's very kind help!
This is for anyone who, like me, was slightly daunted by the example that was given in the Java documentation. (Although, my description below is probably worse!!)
So, I started by adding an HBox which was in the layout I wanted straight into the ComboBox... which is a bad idea!
So, before you go deleting everything you've done, save the HBox somewhere, and do the following:
1. Create a new class to hold your date (Image, and String) which will go into each cell. Make getters/setters to do this. I called mine IconTextCell.
2. Add the following code to the class where your ComboBox is located:
yourComboBox.setCellFactory(new Callback<ListView<T>, ListCell<T>>() {
#Override public ListCell<T> call(ListView<T> p) {
return new ListCell<T>() {
Label name = new Label();
Label icon = new Label();
private final HBox cell;
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
cell = new HBox();
//HERE, ADD YOUR PRE-MADE HBOX CODE
name.setAlignment(Pos.CENTER_RIGHT);
icon.setAlignment(Pos.CENTER_LEFT);
icon.setMaxWidth(Double.MAX_VALUE);
HBox.setHgrow(icon, Priority.ALWAYS);
cell.getChildren().add(icon);
cell.getChildren().add(name);
}
#Override protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
name.setText(item.getLabel());
icon.setGraphic(item.getIcon());
setGraphic(cell);
//HERE IS WHERE YOU GET THE LABEL AND NAME
}
}
};
}
});
You'll see that the main content is very similar to what I had already produced, so no code is lost.
Just replace "T" with your own class for representing a cell.
3. This will display your icon and string in the list, but you need to to also be displayed in the button (the grey top selector part of the combobox, aka the button). Do do this, we need to add the following code:
class IconTextCellClass extends ListCell<T> {
#Override
protected void updateItem(T item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getLabel());
}
}
};
selectionBox.setButtonCell(new IconTextCellClass());
...and that's how I did it. I hope this helps - please compare this to my original post. The actual content (where I create the HBox etc) is obviously not generalised. You can make this as simple or as complex as you want.
Once again, thanks for your help! I hope this post helps others!

This is exactly the example cited in the documentation under "A warning about inserting Nodes into the ComboBox items list".
The list of items in the combo box should represent data - not the UI component used to display the data. The issue is that the HBox cannot appear twice in the scene graph: so it cannot appear both in the "selected cell" and as a cell in the drop-down list.
Instead, create a class that represents the data you are displaying in the ComboBox, and use a cell factory to instruct the ComboBox as to how to display those data. Be sure to set a button cell as well (the cell used for the selected item).

Related

FlipView doesn't show the arrows to navigate in desktop, when more items are added in runtime

I'm using FlipView to show some items in a page. The webservice I'm getting the data from support paging, so I'm adding the next page of items to the FlipView when the user has scrolled to the current last item e.g. if the page size is 5, I'm adding 5 more items whenever the SelectedIndex of FlipView is 4,9,14,19 etc. (index starts at 0).
FlipView has two tiny arrows that we can use in desktop environment (using Mouse) to navigate through the FlipView. When we are at the last item, the right arrow would disappear, because we cannot go any further. Now, if I add more items to the list at this point, the right arrow doesn't reappear, so it doesn't give the impression that there are more items in FlipView. The right arrow would only reappear if
We hover mouse on the left arrow
We click at the location where the right arrow should be
Which isn't a good solution and it requires further user education. Is there any way to address this issue?
Here is a git repo that shows the issue: https://github.com/4bh1sh3k/FlipViewDemo. To repro the issue, scroll till the last item, and then use the button below to add more items to the FlipView.
This should be determined by the control itself. If you do want the button displayed automatically, you may force the next button (which named NextButtonHorizontal in FlipView style and template) to visible after more items added. For how to get the NextButtonHorizontal button you could use VisualTreeHelper class. For example:
private void OnButtonClick(object sender, RoutedEventArgs e)
{
AddImageUris();
IEnumerable<Button> items = FindVisualChildren<Button>(myFlipView);
foreach (Button item in items)
{
if (item.Name == "NextButtonHorizontal")
{
item.Visibility = Visibility;
}
}
}
private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in FindVisualChildren<T>(child))
{
yield return childOfChild;
}
}
}
}
By the way, this can resolve your issue but I'm not very recommend that. Avoid using a flip view control for larger collections, for large collections, consider a ListView or GridView. More details please reference FlipView guidelines.

Why RecyclerView is not working setBackground function?

I tried many Times for setting backgroundColor on RecyclerView. but i try to scroll then background was removed.So I can fix backgroundColor in RecyclerView. Help me please.
Or I Want to change ForegroundColor.
My Issue Video
https://www.youtube.com/watch?v=C29qhPb44FE
I don't know the reason...
You need to first understand how RecyclerView works.
As you scroll through the cells, the views that gets out of the screen will be RECYCLED, and they will subsequently be reused to display the incoming views. Hence the name RecyclerView. This way, views will always be recycled and reused, thus saving memory.
What you need to do is something like this:
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//mList and mSelectedObjects are array lists
View yourView = holder.itemView.findViewById(R.id.your_view);
Object object = mList.get(position);
yourView.setTag(object);
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Object object = (Object) v.getTag();
if (mSelectedObjects.contains(object)) {
mSelectedObjects.remove(object);
v.setBackground(null);
} else {
mSelectedObjects.add(object);
v.setBackgroundColor(Color.GRAY);
}
}
});
}
If you set programmatically the background color. You have to set every time the normal color and the selected color.
RecyclerViews are reusing their views. When an item leaves the screen it will be reused to increase the performance of the recycler view.
In this case when one set programmatically a background color and the item leaves the screen. It will be reused in the new item and the background color is still the same as when the item has left the screen.

Add Context Menu to a dialogue in e4 rcp

I want to add a context menu to a dialogue. I want it in such a way that when clicked anywhere where it is empty a deafaul context menu appears. I have seen example of context menu added to table and tree but not a dialogue as a whole any snippets or examples will be greatly appreciated.
This is what I have tried.
import org.eclipse.jface.action.MenuManager;
#Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(4, false);
layout.marginRight = 5;
layout.marginLeft = 10;
container.setLayout(layout);
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.add(new Action("New Thing") {
/* (non-Javadoc)
* #see org.eclipse.jface.action.Action#run()
*/
#Override
public void run() {
System.out.println("came in options");
}
});
parent.setMenu(menuMgr.createContextMenu(parent));
productListTreeCheckBox(parent);
return super.createDialogArea(parent);
}
Try creating the menu in a similar way to the table/tree menu, but using to top level Composite for the dialog. Using a menu manager that might be something like:
Control topLevelComposite = ... get top level composite
MenuManager menuMgr = new MenuManager();
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(... you menu listener....);
final Menu menu = menuMgr.createContextMenu(topLevelComposite);
topLevelComposite.setMenu(menu);
You will then have to call setMenu on every Composite and control in the dialog which you want to use this menu. You can just use:
control.setMenu(parent.getMenu());
for this (as long as you do it on everything starting from the children of topLevelComposite).

JavaFX: bind disable property of a button to ObservableList size

I have a TableView and I would like to bind the disable property of a Button with the size of the ObservableList model of the table. In particular, I would like to disable the button when the size of the ObservableList is grater than 2.
How can I achieve that?
To disable another button when no row is selected in table I used
editRoadButton.disableProperty().bind(roadsTable.getSelectionModel().selectedItemProperty().isNull());
Is there a similar way?
There are factory methods for useful bindings in the Bindings class. In your case f.i.:
button.disableProperty().bind(Bindings.size(items).greaterThan(2));
You can do something like that
ListProperty<String> list = new SimpleListProperty<>(FXCollections.<String>emptyObservableList());
Button foo = new Button();
foo.disableProperty().bind(new BooleanBinding() {
{
bind(list);
}
#Override
protected boolean computeValue() {
return list.size() > 2;
}
});

How can I set a property for AutoMouseScroll and Mouse hower property to UltraDropdown?

I am using UltraDropDown control to bind a column of one UltraGrid control to list People category in list format.
This drop-down control have more than 25 items and show 8 categories Max, it's very fine. Now whenever I click on drop-down control to see all people category list then i have to hold scroll bar and drag down to see all categories. but I want to show all categories when I mouse scroll and its automatically move up and down to show all, and one more thing I want, when i mouse hover on listed categories then hover item should be shaded or colored.
Please help on both topic.
Thanks & Regards,
Shashi Bhushan Jaiswal
I believe that the first requirement is by default as behavior. Are you handling the MouseWheel event for this to not work?
Here is the code for your second requirement but I do not know if it is a good approach to use the MouseHover event as you like but this is your requirement:
void ultraDropDown1_MouseHover(object sender, EventArgs e)
{
if (cell != null && isInItem) {cell.Cell.Appearance.BackColor = Color.Red;}
}
CellUIElement cell;
bool isInItem = false;
private void ultraDropDown1_MouseEnterElement(object sender, Infragistics.Win.UIElementEventArgs e)
{
if (e.Element is EditorWithTextDisplayTextUIElement && e.Element.Parent.Parent is CellUIElement)
{
cell = (CellUIElement)e.Element.Parent.Parent;
isInItem = true;
}
else isInItem = false;
}

Resources