All jComboBoxes are mirroring each other

Yash Yelmame :

I am using Net-beans so, I have 40 Combo box in one frame enclosed in a panel. So, i resized 8 of them to my convenience and i copied and pasted all 8 Combo Boxes in the panel 5 times. In run time however, whenever i select 1 Combo Box say to index 2 all the other FORTY of them get automatically selected to index 2 as well.

So, i did some testing and found out that when I remove this following piece of code all JComboBoxes work perfectly. Here's the code...

public void MoveToComboBox()
{
try
{
Class.forName("java.sql.Driver");
Connection conn= (Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/teacher?zeroDateTimeBehavior=convertToNull","root","1234");//standard
PreparedStatement pst=conn.prepareStatement("Select * from sections");
DefaultComboBoxModel dcm=new DefaultComboBoxModel(); 
ResultSet rs=pst.executeQuery();      
while(rs.next())
{
    String a=rs.getString("section");  
    dcm.addElement(a);  
}      

d11.setModel(dcm);
d12.setModel(dcm);
d13.setModel(dcm);
d14.setModel(dcm);
d15.setModel(dcm);
d16.setModel(dcm);
d17.setModel(dcm);
d18.setModel(dcm); //these are just 8 of them rest 40 are all like this just like this
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,"Please Prepare the data for first use first.");
}
}

I created a method to get the data from a SQL table called sections and set it on all the combo boxes then at window activated i run this method...

private void f3WindowActivated(java.awt.event.WindowEvent evt) {                                   
f3.setLocationRelativeTo(null);     
MoveToComboBox(); // <-- This is the code if I comment this everything works fine
t1.requestFocus();
}                                  

Yep that's it and the said problem of mirroring combo boxes arises;

Joop Eggen :

It is such that a data model will have one or more views as listeners and model changers. Does the model change, will all views change (on receiving a change event). In this case the model:

  • contains all elements
  • maintains a selected element (which causes the problem mentioned)

In your case make N DefaultComboBoxModels and share just the data:

Vector<String> sections = new Vector<>();
try (Connection conn = DriverManager.getConnection(...);
        PreparedStatement pst = conn.prepareStatement("SELECT section FROM sections");
        ResultSet rs = pst.executeQuery()) {
    while(rs.next()) {
        String s = rs.getString(1);  
        sections.add(s);  
    }
} 
d11.setModel(new DefaultComboBoxModel(sections));
d12.setModel(new DefaultComboBoxModel(sections));
d13.setModel(new DefaultComboBoxModel(sections));
...

Above I use an ugly construct try-with-resources that ensures the following will be called at the end:

  • rs.close();
  • pst.close();
  • conn.close();

(This is because all objects are AutoCloseable.)

You can call them also explicitly at the end.

Class.forName exists just to load MySQL's product, as during compilation one does not need (or event want) to import a MySQL specific driver. However that is nowadays not really needed, as JDBC database drivers can now be found dynamically. And your Class.forName did not load the MySQL class.

Note that the Vector class is rather old, but DefaultComboBoxModel(Vector) fits.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=159697&siteId=1