Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda