Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
Adding items to JComboBox while program is running

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.

enter image description here

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);
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!