I have a table in the DB with Id Columns, Number Plate, and Drill on the board.
I would like to show in the JComboBox
the concatenation of "Plate N ° + Drilling".
Example:
Plate 10 - 113.00 mm
But when a combobox item is selected by the user, it captures the corresponding ID of the Placa
object. I'm doing it in Netbeans and MySQL.
Follow the method that searches the database (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 Plate 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 (Do 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 can not complete the reasoning of the Control part, to get the id of the selected item.
I store the details of the table in ArrayList
with DAO above.
placa.add(pl);
And populate the ComboBox (Values Drilling) using the Template 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 following error occurs: