Matching of combobox contents

Asked

Viewed 250 times

2

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

1 -> A

2 -> B

3 -> C

4 -> D

5 -> AND

6 -> F

7 -> G

8 -> H

fill combo with the "names"

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


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

Note: I just posted this snippet of code just to illustrate, I just want a direction of what to do, I’m not asking anything ready.

  • How so? , Do you want to pick up the items by position? for example item B in position 2 and so on?

  • Yeah, I’ll show you A, but I won’t "save" A, I’ll save 1.

  • this setDoPojo is where you save?

  • Each data entered in the combo box takes a position just like in the array, for example if you use the combobox.getItemAt( 0 ); returns A if you put 1 returns B and so on, couldn’t that be so? or necessarily has to start the A by 1?

  • @Andersonhenrique I already appreciate your attention, so I would have to start with the 1. setDoPojo is from the getters and setters of the pojo.

  • Since the database only stores the range 1-8 why not do index + 1?? Thus, selecting item "B" would be myCombo.getSelectionModel.getSelectedIndex() + 1 = 2, and 2 would be stored in the BD. Also not understood why store only 8 values in 1 database, an array would be sufficient.

  • Give me an example then, of how I should do, doing favor

Show 2 more comments

1 answer

1


Assuming your table in the database is something like:

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

Assuming you still receive the data with a select similar to this: select col2 from tabela order by col1 (So that they are ordered by the index and therefore have determined positions). Leaving aside the treatment you give to this data in your application, I’ll skip to the combobox part. 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 don’t know the details of what you want to do I believe the above is enough to remedy your problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.