B4J Question Combobox Gravity

khwarizmi

Active Member
Licensed User
Longtime User
Hi all

I found many results when I use the forum search about how to set the combobox gravity to center, but the problem was not solved.
B4X:
Dim jo As JavaObject = ComboBox1
jo.GetFieldJO("scene").RunMethod("setNodeOrientation", Array("CENTER"))

I got the error: Input string was not in a correct format.
I tried all the ways in this: https://www.b4x.com/android/forum/threads/help-right-aligning-combo-box-items.96718/ without success.
 
Solution
Well that was a rabbit hole. Turns out I was wrong with my assumption.

There is a problem adding nodes to a combobox as when an item with a Node is selected, the standard cell factory removes the Node from the list view and adds it to the button area. Which is why the text disappears. It should return it to the list view once another item is selected but it does not re-appear.

A bit more digging and I found a solution. You need create the items as standard text items (no nodes) and add a style sheet that contains:

B4X:
.combo-box .list-cell {
    -fx-alignment: center;
}

If you only want to center text on specific comboboxes, then you need to add a styleclass to the combobox and change the CSS to something like:

B4X:
.cmb-center...

stevel05

Expert
Licensed User
Longtime User
It always pays to check the Java documentation when using reflection (JavaObject). The relevant method is here: https://docs.oracle.com/javase/8/ja...eOrientation-javafx.geometry.NodeOrientation-

As you will see if you click on NodeOrientation to see the enum documentation, there are only three possible values. INHERIT, RIGHT_TO_LEFT or LEFT_TO_RIGHT. While this achieves the required result in the example to RIGHT justify the text, it is a hack.

The correct solution is number 4 in the linked post although it should use pane (or B4xView) instead of AnchorPane , but you then need to add some css to manage the text colour when the drop down list (integral to the combobox) states change.

I have to go out right now, but if you need help with the css, I can take a look later unless someone beats me to it.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Well that was a rabbit hole. Turns out I was wrong with my assumption.

There is a problem adding nodes to a combobox as when an item with a Node is selected, the standard cell factory removes the Node from the list view and adds it to the button area. Which is why the text disappears. It should return it to the list view once another item is selected but it does not re-appear.

A bit more digging and I found a solution. You need create the items as standard text items (no nodes) and add a style sheet that contains:

B4X:
.combo-box .list-cell {
    -fx-alignment: center;
}

If you only want to center text on specific comboboxes, then you need to add a styleclass to the combobox and change the CSS to something like:

B4X:
.cmb-center .list-cell {
    -fx-alignment: center;
}

assuming cmb-center is the class you give it.

The attached project demonstrates with a standard ComboBox and B4xCombobox.
 

Attachments

  • ComboboxGravity.zip
    2.6 KB · Views: 199
Last edited:
Upvote 1
Solution
Top