My program has a GUI where the user first selects a button to load an excel file. Then the content of the excel file is listed in a JTextArea and JComboBox. As you can see in the picture, although the JTextArea contains some texts, JComboBox is empty. I debugged the code and can verify that JComboBox's variable (TheGene) adds the item. In other word, str contains AARS, AARS2, ... in each iteration.
Examples that describe JComboBox statically adds items after the JComboBox is created. I want to add items after running something (when the button's work is finished).
I am using the frame designer of Netbeans, where is automatically generates the following code
public class TheFrame extends javax.swing.JFrame
{
...
private javax.swing.JComboBox<String> TheGene;
private void initComponents() {
...
TheGene = new javax.swing.JComboBox<>();
TheGene.setMaximumRowCount(20);
TheGene.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
TheGeneActionPerformed(evt);
}
});
...
}
private void TheGeneActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
int n = theFile.getGeneNumberFromFile();
for (int i = 0; i < n; i++){
String str = theFile.getGeneNameFromFile( i );
TheGene.addItem(str);
}
}
You could use a model for your JComboBoxlike :
DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>();
JComboBox TheGene = new JComboBox( model );
and then later with clicking of Load Gene List button :
private void TheGeneActionPerformed(java.awt.event.ActionEvent evt)
{
model.removeAllElements();
// TODO add your handling code here:
int n = theFile.getGeneNumberFromFile();
for (int i = 0; i < n; i++) {
String str = theFile.getGeneNameFromFile( i );
model.addElement(str);
}
}
This should make the combo's items populated.