ComboBox Content Matching

2

How do you make the comboBox options match numeric indexes? Example, in my database I only save numbers from 1 to 8. And in the application I show my user names, I want those names to match those numbers. I was trying to use substrings to do this.

1 - > A

2 - > B

3 - > C

4 - > D

5 - > E

6 - > F

7 - > G

8 - > H

fill in combo with "names"

 meuCombo.getItems().addAll("A", "B", "C", "D", "E", "F", "G", "H");


pojo.setDoPojo(meuCombo.getTypeSelector().substring(1 , 2))

Note: I've just posted this snippet of code just to illustrate, I just want a targeting of what to do, I'm not asking for anything ready.

    
asked by anonymous 17.07.2017 / 13:26

1 answer

1

Assuming your table in the database is something like this:

+-------+-------+
| Col 1 | Col 2 |
+-------+-------+
|   1   |   A   |
+-------+-------+
|   2   |   B   |
+-------+-------+
|  ...  |  ...  |
+-------+-------+

Assuming that you receive the data with a select similar to this: select col2 from tabela order by col1 (so that they are sorted by the index and, therefore, have specific positions). By abstracting the treatment you give to this data in your application, I'll jump to the part of comboBox. Let's say the user clicked on item C, the code to get the correct index would be as follows:

// ... código anterior

// Se o usuário clicou em C (Terceira posição) index terá o valor 2 + 1 = 3
int index = comboBox.getSelectionModel().getSelectedIndex() + 1;

// Caso queira imprimir na tela o item selecionado: (nome terá o valor "C")
String nome = comboBox.getSelectionModel().getSelectedItem();

As I do not know the details of what you want to do I believe the above is enough to remedy your problem.

    
17.07.2017 / 20:42