Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda