0

enter image description here

What I want is when I click subject combo box the Batch Name: combo box should change according to subject selection. I tried this but nothing happens. This code is in Subject Name: combo box action performed function.

private void combosubjActionPerformed(java.awt.event.ActionEvent evt) {                                          
 String sub= evt.getSource().toString();
 try {
   btchcombo.removeAllItems();
   Connection conn = getConnection();
   PreparedStatement prpd = conn.prepareStatement("SELECT batch FROM BATCHLIST where subject=?");
   prpd.setString(1,sub);
   ResultSet rs = prpd.executeQuery();
   while (rs.next()) {
     String pat = rs.getString("BATCH");
     btchcombo.addItem(pat);
   }
   } catch (Exception e) {
     e.printStackTrace();
     JOptionPane.showMessageDialog(null, e);
   }    
 }                                       
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Hard code some data to replace the DB. – Andrew Thompson May 11 '17 at 07:16
  • BTW - 1) `combosubjActionPerformed(` that looks suspicious. Have you checked this method is actually being called? 2) `{ ] String sub= evt.getSource().toString();` That rubbish would not even compile. Don't waste your time, or more importantly **our** time, by posting a 'close facsimile' of the code used. Copy / paste an MCVE / SSCCE that you have confirmed shows the error. – Andrew Thompson May 11 '17 at 07:21
  • If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach. – trashgod May 11 '17 at 09:43

1 Answers1

1

It isn't that simple. Just calling addItem() is simply not sufficient to update the combo box data; and the UI representation of that thing.

You want to look into this question here for the required details to get that done.

But beyond that: you should really step back here and do some reading how to properly build a UI application that connects to a database. It is simply absolutely wrong for an ActionListener to directly pull data from the database!

First of all, this violates any ideas of layering. You put abstractions between your UI and the database. See here or there for further reading.

Then: that actionPerformed() method is called by the so called event dispatcher thread. That thread is meant for dispatching event. You shouldn't "occupy it" for making calls down to the database. You know, what if you run out of DB connections; and that DB code ... sits there for a minute, waiting to get its turn? That will cause your UI to freeze because the thread responsible for keeping the UI going ... sits there and waits for your DB.

Long story short: it seems that you are over-burdening yourself. You should step back, and spent quite some time learning about such aspects. Otherwise, the result of your work will be buggy, and nobody will want to use it.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248