1
I have a table in BD with Columns Id, Board N° and Drilling on board.
I’d like to show on JComboBox
the concatenation of "Plate N° + Drilling".
Example:
Plate 10 - 113,00 mm
But when a combobox item is selected by the user, capture the corresponding object ID Placa
. I’m doing it in Netbeans and Mysql.
Follow the method you search in the bank (DAO):
public ArrayList<Placa> ObterTabelaPlaca() {
conectar();
ArrayList<Placa> placa = new ArrayList<Placa>();
Statement stmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM cadastroplaca ORDER BY CAST(furoPL AS DECIMAL(5,2))";
try {
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Placa pl = new Placa();
pl.setId(rs.getInt("id"));
pl.setCodPlaca(rs.getInt("codigoPL"));
pl.setFuracao(rs.getDouble("furoPL"));
placa.add(pl);
}
} catch (Exception e) {
System.out.println("Erro " + e.getMessage());
}
return placa;
}
The Board class(Model):
public class Placa {
int codPlaca, qtdMoldePlaca, id;
double furacao;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
String notaPlaca;
public int getCodPlaca() {
return codPlaca;
}
public void setCodPlaca(int codPlaca) {
this.codPlaca = codPlaca;
}
public int getQtdMoldePlaca() {
return qtdMoldePlaca;
}
public void setQtdMoldePlaca(int qtdMoldePlaca) {
this.qtdMoldePlaca = qtdMoldePlaca;
}
public double getFuracao() {
return furacao;
}
public void setFuracao(double furacao) {
this.furacao = furacao;
}
public String getNotaPlaca() {
return notaPlaca;
}
public void setNotaPlaca(String notaPlaca) {
this.notaPlaca = notaPlaca;
}
}
The method that fills the component (From the controller):
public void MostraComboPlaca() throws SQLException{
CadastroDAO dao = new CadastroDAO();
ArrayList<Placa> listaplaca = dao.ObterTabelaPlaca();
DecimalFormat formato = new DecimalFormat("#0.00");
placaCilindrico.setModel(new ComboModelTipo(listaplaca));
for(int i=0;i<listaplaca.size();i++){
placaCombo.addItem(String.valueOf(formato.format(listaplaca.get(i).getFuracao()).replace('.', ','))+" - PL "+listaplaca.get(i).getCodPlaca());
}
}
I cannot complete the reasoning of the Control part, to get the id of the selected item.
I store the details of the table in one ArrayList
with the DAO above.
placa.add(pl);
And I populate the Combobox (Hurricane Values) using the Model below:
public void MostraComboPlaca() throws SQLException{
CadastroDAO dao = new CadastroDAO();
ArrayList<Placa> listaplaca = dao.ObterTabelaPlaca();
placaCombo.setModel(new ComboModelTipo(listaplaca));
}
I use this class as Comboboxmodel:
public class ComboModelTipo extends AbstractListModel implements ComboBoxModel {
private ArrayList<Placa> lista;
private Placa selected;
public ComboModelTipo(ArrayList<Placa> lista) {
this.lista = lista;
}
@Override
public int getSize() {
return this.lista.size();
}
@Override
public Object getElementAt(int index) {
return this.lista.get(index).getFuracao();
}
@Override
public void setSelectedItem(Object anItem) {
this.selected = (Placa) anItem;
}
@Override
public Object getSelectedItem() {
return this.selected;
}
public Integer getIdObjetoSelecionado() {
return selected.getId();
}
}
After these steps my Combobox is populated, but when I click on some value, the error occurs below:
You have created a proper Combomodel for the board class?
– user28595
diegofm, I tried to do this topic... http://respostas.guj.com.br/1430-buscar-id-do-campo-selected-no-combobox-resolver public class Combomodeltipo extends Abstractlistmodel Implements Comboboxmodel {
– RafB--
Add a [mcve] to simulate the problem, read more here
– user28595