0

I am facing a problem with populating list values in a JComboBox. I've read the integer values from a file by using a class method(which is not subscribed to the ActionEvent), and successfully modified the combo box list contents. However I'm unable to see the contents of the UI get updated .

Why is it so? However when I use the same code within an action listener of a button, I can the see the combo box UI get updated with the modified list values.

How can I update a combo box UI using a simple method that reads a file and modifies the combo box list values.

public class NewJFrame extends javax.swing.JFrame{

public NewJFrame() { //Constructor to initialize the Form
        initComponents();
        }

private void initComponents() {
ButNext = new javax.swing.JButton();
QuestionList = new javax.swing.JComboBox(); 
….
…. //lines of code

QuestionList.setMaximumRowCount(10);

org.jdesktop.beansbinding.Binding binding =      org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, QuestionList, org.jdesktop.beansbinding.ELProperty.create("${selectedItem}"), QuestionList, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
    bindingGroup.addBinding(binding);

QuestionList.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
            QuestionListItemStateChanged(evt);
        }
    });
QuestionList.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
            QuestionListActionPerformed(evt);
        }
    });

…
…
bindingGroup.bind();

pack();
setLocationRelativeTo(null);

} //end of initComponents



private void ButNextActionPerformed(java.awt.event.ActionEvent evt) {      // If I I click Next button on the UI then the JComboBox is populated with the vales read from the file
        ….    
        …. // lines of code                              

        QuesListContent.add(qnumber);
        QuestionList.addItem(qnumber); 
 …. // lines of code   
}


private  int defineExamstate() throws FileNotFoundException, IOException{   // If I I call this function, then on the UI  the JComboBox is NOT populated with the vales read from the file,                       
                                                                                                                      //however the values of the list is printed correctly when checked in submain() function
String answersPath=NewJFrame.SavedanswerPath; //file to be read

Scanner input1 = new Scanner(new File(answersPath));


    while(input1.hasNextLine())
    {

    String message = input1.nextLine(); //User answers

        switch (message) {
            case "START":

                startcounter+=1;
                lastquestion=0;

                break;
            case "END":

                QuesListContent.clear();
                QuestionList.removeAllItems();
                maxquestion=0;
                endcounter+=1;

                break;
            default:
                record=message.split("Q");
                lastquestion=Integer.parseInt(record[0]);
                if(!QuesListContent.contains(lastquestion)){     
                QuesListContent.add(lastquestion);
                QuestionList.addItem(lastquestion);

                }

                break;


        }
        }
}

 protected  void submain() throws FileNotFoundException, IOException, Throwable {   //Function
 …
 …. //lines of code

 int size = QuestionList.getItemCount();
 for(int i=0;i<size;i++) {
                Object element = QuestionList.getItemAt(i);
                System.out.println("Element at " + i + " = " + element);  // Able to print the content of the combo box
            }
}


// Variables declaration - do not modify                     
  private javax.swing.JButton ButNext;
  private javax.swing.JComboBox QuestionList;

} // end of class definition
user2582651
  • 33
  • 1
  • 8
  • 2
    *"Why is it so?"* In two words? 'The code.' For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). – Andrew Thompson Jun 19 '15 at 09:35
  • You can refer [this](http://stackoverflow.com/questions/3173149/how-do-i-populate-jcombobox-from-a-text-file/30933238#30933238) – Madhan Jun 19 '15 at 10:25
  • Hello ! I've updated my code. Hope it helps in bringing clarity. I Found a link that is kind of useful to me, however I am unable to change the ComboBox type in my UI. Because I am unable to edit the variable : private javax.swing.JComboBox QuestionList; I am using Netbeans design view for for making the UI and source view for writing the code. Please excuse as I'm new to Java so my naming conventions and code might not be according to the standards. Thanks – user2582651 Jun 19 '15 at 14:16
  • The link that I found useful is this : http://stackoverflow.com/questions/22273311/updating-jcombobox – user2582651 Jun 19 '15 at 14:28
  • *"I've updated my code. Hope it helps in bringing clarity."* Can you copy/paste that into an IDE and compile it **as is** without any changes? If not, it's not an MCVE. Also a tip: Add @Madhan (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Jun 19 '15 at 14:34
  • 1
    Variable names should NOT start with an upper case character. Some of your names are correct and some are not. Be consistent and follow Java conventions!!! – camickr Jun 19 '15 at 15:25
  • Post full code or the part which has errors and do as said by @AndrewThompson – Madhan Jun 19 '15 at 16:06
  • @Madhan *"Post full code or the part which has errors as said by AndrewThompson"* Note that what I said equates to *neither* of those things. An MCVE is a very specific form of code. – Andrew Thompson Jun 19 '15 at 16:09
  • @AndrewThompson I wanted the user to do what you've said and also what I wanted him to do – Madhan Jun 19 '15 at 16:16

0 Answers0