C
C
Chvalov2016-04-17 13:47:32
Java
Chvalov, 2016-04-17 13:47:32

How to pass the value of the fields from the database to the ChoiceBox?

I can't figure out how to correctly pass the value of the fields from the database table to the ChoiceBox.
The table has id and name
I use ORMLite.
In this way I get id and name:

@Test
    public void testGetCategories() throws Exception {

        CategoryService service = new CategoryService();
        List<Category> list = service.getCategories();

        Assert.assertNotNull(list);

        for (Category category : list) {
            System.out.println(category);
        }
    }

Category.java
@DatabaseTable
public class Category {
    @DatabaseField(generatedId = true)
    private int id;
    @DatabaseField
    private String name;

    public Category() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Well, just in case CategoryService.java
public CategoryService() throws SQLException {
        source = new JdbcConnectionSource(url);
        dao = DaoManager.createDao(source, Category.class);
    }

    public List<Category> getCategories() throws SQLException {
        return dao.queryForAll();
    }

ChoiceBox
cbSubject.setItems(FXCollections.observable*****());

In the ChoiceBox itself, I need to pass the name list , but at the same time I need to get the id value into the int variable when I select the item.
UPD: At the moment I'm thinking of creating a two-dimensional array of the String type and passing id and name into it, and taking the data from the array, but I'm not sure what the right solution is.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2016-04-17
@FAQEnD

Hi Chvalov ,
It's not entirely obvious what the end result should be.
Something similar to this, but instead of numbers there will be data from name?
ftTFp.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question