CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2004
    Posts
    4

    Unhappy JComboBox add items at runtime

    hi there,

    just need to ask this, it is a pain in the backside, k, basically i have approximetly 6 JComboBox's, the first JComboBox's list is created and properly displays and everything with values i want it to this is basically at the start when the program starts running for the first time, now, basically the list for the the second JComboBox is dependented on what selected in the first combo box, so if i select say an item from the first combobox then a list will be generated, problem is i need to store or put that list in the second combo box. now here is a junk of coding i hope i have explained the problem properly, jus dunno why it doesnt put the new list/elements in the combobox at runtime, here is the coding

    symptoms = new JComboBox();
    storedRead=tempread.readLine();
    while(storedRead!=null)
    {
    symptoms.addItem(storedRead);
    storedRead=tempread.readLine();
    }

    symptoms.setBounds(40, 100, 200, 30);
    symptoms.setMaximumRowCount(3);

    symptoms.addItemListener(
    new ItemListener() {
    public void itemStateChanged(ItemEvent e)
    {
    Object testout = symptoms.getSelectedItem();
    String testout2 = testout.toString();
    printS.setText(testout2);
    }

    }
    );


    printS = new JTextField();
    printS.setBounds(40, 130, 200, 30);
    printS.setEditable (false);

    contentPane.add(symptoms);
    contentPane.add(printS);

    this is the first combo box, which works properly, now, what the user has selected is put in a method thats stores new values to a file. then the file is read in and elements added to the combo box,

    question is how do i do that for the second combobox which the program is running??

    thankx, would really really appreciate your help, just cant get anywhere. thankx

  2. #2
    Join Date
    Apr 2003
    Location
    Los Angeles area
    Posts
    776
    First of all, comboboxes are populated at runtime always. They don't necessarily need to be populated at the constructor stage.

    I am guessing here but you already display the comboboxes as unpopulated except for the first one. A users choice in combobox one determines the contents to be populated into combobox2 and so on down the line. The information for these subsequent populations if file based. Correct so far?

    In the event handler for the first combobox
    Code:
            JComboBox cb1 = new JComboBox();
            cb1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JComboBox cb = (JComboBox)e.getSource();
                    String itemName = (String)cb.getSelectedItem();
                    itemName += ".dat";
                    // open a file based on itemName,
                    // remove any existing items from combobox2
                    // and populate combobox2
                }
            });
    You will need a separate action listener for each combobox.

  3. #3
    Join Date
    Apr 2004
    Posts
    4

    Unhappy JComboBox populating at runtime

    hi there,

    thank you so much for replying promptly, highly appreciated,

    k, have treid the above coding, the below is the coding, i have put into my code, the symptoms combobox is the first combobox, and the symptoms1 is the second combobox:

    symptoms.addItemListener(
    new ItemListener() {
    public void itemStateChanged(ItemEvent e)
    {
    Object testout = symptoms.getSelectedItem();
    String testout2 = testout.toString();
    printS.setText(testout2);
    symptoms.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    symptoms1 = new JComboBox();
    JComboBox symptoms = (JComboBox)e.getSource();
    String itemName = (String)symptoms.getSelectedItem();
    try{
    printSymp.second(itemName);

    symptoms1.removeAllItems();
    storedRead=tempread2.readLine();
    while(storedRead!=null)
    {
    System.out.println(storedRead);
    symptoms1.addItem(storedRead);
    storedRead=tempread2.readLine();
    }
    }
    catch (Exception eeer) {}
    }
    });
    }

    }

    );

    i have put in the System.out.println to see if it takes the new value, the prinSymp method takes the selected value from the the first combobox and created new items which is stores in a file , which are basically read in when populating the other comboboxes, now, the system.out.println perfectly shows that the new value are read in exactly like how i wanted it, but unfrotunetly they are not stored in the dunno why, dunno if i am doing anything wrong, if u could assist me alittle bit further, this is the last stage of the program, thankx

    regards

    h.m.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163
    You created a new JComboBox on the fly (symptoms1), remove all items from it (there won't be any, you only just created it!), then add items to it. Why aren't they displayed? Well, because you haven't arranged to display the new JComboBox - did you add it to your frame window panel? No... so how is the system to know you want to display it?

    Did you actually want to create a new combo box, or should you just re-use one you are already displaying? It's really up to you...

    The sooner you start to code, the longer the program will take...
    R. Carlson
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Apr 2004
    Posts
    4

    Unhappy JComboBox add item at runtime

    hi there, thankx for the reply,

    mmm, well i have created all the six combo box that i need and have created them with empty list, so basically there is nothing displayed in them, at present only the first combo box has items in it, even though the second and third etc...... combo boxes are displayed there isnt really anything in them, so basically i want to re-use the combo boxes that are already displayed and add items into them, create the combo box and everything within the actional listner of the first combo box, but that causes problems, such as not displaying the arrow for the combo box, and also does not display the selected item, so having problems with that, i would be very very grateful if you can help, thankx

    regards
    h.m.

  6. #6
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    here.. i threw this together for you. study it, and learn how splitting things up into different methods makes things much easier:

    Code:
    /*
     * JFrame.java
     *
     * Created on 14 April 2004, 12:05
     */
    
    /**
     *
     * @author  admin
     */
    public class DynCombo extends javax.swing.JFrame {
      boolean jc1_initial = true, jc2_initial = true;
      
      /** Creates new form JFrame */
      public DynCombo() {
        initComponents();
        populateJCombo1();
      }
      
      protected void populateJCombo1(){
        for(int i=65; i<91; i++){
          jComboBox1.addItem( (char)i + " " );
        }
      }
      protected void populateJCombo2(String s){
        jComboBox2.removeAllItems();
        for(int i=65; i<91; i++){
          jComboBox2.addItem( s + (char)i + " " );
        }
      }
      protected void populateJCombo3(String s){
        jComboBox3.removeAllItems();
        for(int i=65; i<91; i++){
          jComboBox3.addItem( s + (char)i + " " );
        }
      }
      
      
      
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      private void initComponents() {
        jComboBox1 = new javax.swing.JComboBox();
        jComboBox2 = new javax.swing.JComboBox();
        jComboBox3 = new javax.swing.JComboBox();
    
        getContentPane().setLayout(new java.awt.FlowLayout());
    
        addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(java.awt.event.WindowEvent evt) {
            exitForm(evt);
          }
        });
    
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox1ActionPerformed(evt);
          }
        });
    
        getContentPane().add(jComboBox1);
    
        jComboBox2.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jComboBox2ActionPerformed(evt);
          }
        });
    
        getContentPane().add(jComboBox2);
    
        getContentPane().add(jComboBox3);
    
        pack();
      }
    
      private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
        if(jc2_initial)
          jc2_initial = false;
        else
          populateJCombo3( (String)jComboBox2.getSelectedItem() );
      }
    
      private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
        if(jc1_initial)
          jc1_initial = false;
        else
          populateJCombo2( (String)jComboBox1.getSelectedItem() );
      }
      
      /** Exit the Application */
      private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
      }
      
      /**
       * @param args the command line arguments
       */
      public static void main(String args[]) {
        new DynCombo().show();
      }
      
      
      // Variables declaration - do not modify
      private javax.swing.JComboBox jComboBox1;
      private javax.swing.JComboBox jComboBox2;
      private javax.swing.JComboBox jComboBox3;
      // End of variables declaration
      
    }
    it's basic, only 3 boxes, populates them very simplistically, and you need to resize the form to see all 3, but its a starting point
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: JComboBox add item at runtime

    Originally posted by hotchiller786 i have created all the six combo box that i need...

    ... basically i want to re-use the combo boxes that are already displayed and add items into them, create the combo box and everything within the actional listner of the first combo box,
    You've lost me. If you've created all the combo boxes you need and you want to re-use them, where does creating a combo box within the action listener come in?

    Whatever, if you want a combo box to be displayed, you need to add it to your display panel.

    but that causes problems, such as not displaying the arrow for the combo box, and also does not display the selected item
    Er, which combo box? What selected item? Can you be a little more specific?

    Precise language is not the problem. Clear language is the problem...
    R. Feynman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  8. #8
    Join Date
    Apr 2004
    Posts
    4

    Talking jcombo populate at runtime

    hi there, thankx for the reply, well the problem was, that, the first combo populated perfectly, as the population occured at startup,but with the the combo's to follow, if i define the combo's seperate in the contentpane it wouldnt get populated, define the combo box inside the listeners of that combo box which its population dependent on, and put the contentPane.add(combobox) inside the listener too, then it created the combo box at runtime and populated it exactly how i wanted it, BUT, normally there is a arrow on the combobox to on the right hand side, that didnt display, and when i tried selecting the item, it wouldnt select, however, the coding that admin (above) sent, did the job perfectly, and i studied it, and manipulated it, and encapsulated the coding within my content pane, and it perfectly work, thank you for all your help, highly appreciated

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured