in my Java swing application, I want Jcombobox populate with some data from database as well as allow user to enter keyword they wish in order to search using combobox value. So i set the editable mode to true.But then i lost data coming from database. Is it possible to enable editable mode true and same time populate Jcomobobox with data from database?
private void cmbPvtTrnsCompanySeacrhFocusGained(java.awt.event.FocusEvent evt) {
try {
ArrayList<String> transList = PvtTransDriverController.getPvtTransporterNames();
ComboBoxModel comboBoxModel = new DefaultComboBoxModel(transList.toArray());
cmbPvtTrnsCompanySeacrh.setModel(comboBoxModel);
} catch (ClassNotFoundException ex) {
Logger.getLogger(PvtTransDriver.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(PvtTransDriver.class.getName()).log(Level.SEVERE, null, ex);
}
}
Initialization code for cmbPvtTrnsCompanySeacrh
cmbPvtTrnsCompanySeacrh.setEditable(true);
cmbPvtTrnsCompanySeacrh.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
cmbPvtTrnsCompanySeacrhFocusGained(evt);
}
});
javax.swing.GroupLayout jPanel23Layout = new javax.swing.GroupLayout(jPanel23);
jPanel23.setLayout(jPanel23Layout);
jPanel23Layout.setHorizontalGroup(
jPanel23Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel23Layout.createSequentialGroup()
.addContainerGap()
.addComponent(cmbPvtTrnsCompanySeacrh, 0, 266, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
You can write some code like below:
ArrayList<String> transList = PvtTransDriverController.getPvtTransporterNames();
ComboBoxModel comboBoxModel = new DefaultComboBoxModel(transList.toArray());
cmbPvtTrnsCompanySeacrh.setModel(comboBoxModel );