user1833168
user1833168

Reputation:

Cannot populate JComboBox

i am trying to populate my Jcombobox with value of a string array but when running it my combo box remain blanks below are my codes

   package UI;

       import Class.*;
     import java.awt.event.ActionEvent;
      import java.awt.event.ActionListener;
       import java.awt.event.ItemEvent;
       import java.awt.event.ItemListener;
      import java.io.*;
       import java.util.Scanner;
       import java.util.logging.Level;
        import java.util.logging.Logger;
           import javax.swing.ButtonGroup;
          import javax.swing.JComboBox;
              import javax.swing.JFileChooser;
           import javax.swing.JOptionPane;


       public class Smith_waterman extends javax.swing.JFrame {


         private String[]algorithm_name = {"Needleman & Wunsch (global alignment)","Smith & Waterman (local alignment)"};
         private PairwiseAlignmentAlgorithm[] algorithm = {new NeedlemanWunsch(),
    new SmithWaterman(),
    };


/**
 * Creates new form main_menu
 */
        public Smith_waterman() {
    initComponents();
    algorithm_combo = new JComboBox(algorithm_name);

can someone tell me how to fix that

Upvotes: 0

Views: 153

Answers (2)

Altrim
Altrim

Reputation: 6756

To populate the combo box you need to use the addItem() method on ComboBox

private String[] algorithm_name = {"Needleman & Wunsch (global alignment)","Smith & Waterman (local alignment)"};
JComboBox jc = new JComboBox(); 

// Then you can iterate over the array to populate the combobox
for (String name : algorithm_name) {
    jc.addItem(name);
}

Upvotes: 2

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

See this example it will give you some idea.................

public class MainClass extends JPanel implements ItemListener {
  public MainClass() {
      JComboBox jc = new JComboBox(); 
      jc.addItem("France"); 
      jc.addItem("Germany"); 
      jc.addItem("Italy"); 
      jc.addItem("Japan"); 
      jc.addItemListener(this); 
      add(jc); 
  }

  public void itemStateChanged(ItemEvent ie) {
      String s = (String)ie.getItem(); 
      System.out.println(s); 
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }

Upvotes: 2

Related Questions