Popular combobox with a list of a query

Asked

Viewed 23 times

0

Well I have a DAO class Departamentosdao I wanted to popular a combo box with a consultation I do in this DAO

my DAO:

public class DepartamentoDAO {

    private Connection con;

    public DepartamentoDAO() {
        this.con = new ConnectionFactory().getConnection();
    }

    public boolean popularComboBox() throws SQLException{
        List<String> setores = new ArrayList<String>();
        String query = "SELECT * FROM departamentos";
        PreparedStatement ps = con.prepareStatement(query);
        ResultSet rs = ps.executeQuery();
        while(rs.next()){
           setores.add(rs.getString("nome_setor"));
           }

    ps.close();
    return true;
    }
}

my controller:

@FXML
private JFXComboBox<Departamento> cbDepart;
private List<Departamento> departamentos = new ArrayList();

how I could popular my combo box with that DAO list that is in another way?

1 answer

0

a hint would be to return a list instead of Boolean:

public List<String> setores findAllSetores() {
        List<String> setores = new ArrayList<>();
        /*operacoes*/

        return setores;
}

then anywhere else you can get that list simply by doing:

List<String> setores = new DepartamentoDAO().findAllSetores();
if (!setores.isEmpty()){
   /*significa que a lista nao esta vazia*/
}

from this could popular your combobox

JComboBox cb = new JComboBox();
            cb.addItem();

Browser other questions tagged

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