0

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())
        );
Sam
  • 7,252
  • 16
  • 46
  • 65
amal
  • 3,470
  • 10
  • 29
  • 43

1 Answers1

2

You can write some code like below:

ArrayList<String> transList = PvtTransDriverController.getPvtTransporterNames();
ComboBoxModel comboBoxModel = new DefaultComboBoxModel(transList.toArray());
cmbPvtTrnsCompanySeacrh.setModel(comboBoxModel );
Apurv
  • 3,723
  • 3
  • 30
  • 51
  • I tried changing code according to the code you posted. But still no luck. I updated the original post with my new code. – amal Mar 25 '13 at 05:49
  • Can you share the code where you are initializing `cmbPvtTrnsCompanySeacrh` ? – Apurv Mar 25 '13 at 05:50
  • This code should be added when you first show the combo box (or when the data from the database is available for read) – Apurv Mar 25 '13 at 05:52
  • Can you try using the above code after setting the combo box to editable (and not in the event handler) ? – Apurv Mar 25 '13 at 05:58
  • Yes now its working fine. I placed the code at constructor. Thnx lot for your help. unfortunately can not vote up due to lack of reputations yet. – amal Mar 25 '13 at 05:59