Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Unable to add Items to a JComboBox when those items are read from a file

  1. #1
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Unable to add Items to a JComboBox when those items are read from a file

    Hey

    So I have this little problem:

    When i read from a file, i am unable to add the items read from that file to a JComboBox.

    I tried saving them to an array first, but it makes no difference.

    The weird thing is, using a debug statement the output tells me that the items are in the JComboBox, but when I click on it nothing appears other then the items that were there at the begining of the program.

    another weird thing, I can add items to that JComboBox when i get input from a user, and then i see it. but when reading from a file, i don't see it. Any Suggestions?

    (here is my code for adding while reading from the file, i left the debug statements which are just the System.out.print)

    Java Code:
     try//reading from file for arrays
            {
                File inFile = new File(saveX+ext);
                FileReader inStream = new FileReader(inFile);
                BufferedReader bufReader = new BufferedReader(inStream);
                
                x = Integer.parseInt(bufReader.readLine());
                inStream.close();
                
                
                inFile = new File(saveCard+ext);
                inStream = new FileReader(inFile);
                bufReader = new BufferedReader(inStream);
                
                choiceExist = 6;
    
                System.out.print('\f');
                searchCard.removeAllItems();
                for (int counter = 0; counter<x; counter++)
                {
                    creditCard[counter] = bufReader.readLine();
                    secretQuestion[counter] = bufReader.readLine();
                    secretAnswer[counter] = bufReader.readLine();
                    passCode[counter] = bufReader.readLine();
                    
                    //System.out.println(bufReader.readLine());
    
                    searchCard.addItem(creditCard[counter]);
                    for (int counter3=0;counter3<searchCard.getItemCount();counter3++)
                    {
                        System.out.println("search Card Items" + searchCard.getItemAt(counter3));
                    }
                    
                    int checker = 0;
                    
                   
                    for (int counter2 = 0;counter2<secureQ.getItemCount();counter2++)
                    {
                        System.out.println("Security Questions Item" + secureQ.getItemAt(counter2));
                        if (secretQuestion[counter].equals(secureQ.getItemAt(counter2)))
                        {
                            checker=1;
                        }
                    }
                    if (checker == 0)
                    {
                        secureQ.addItem((Object)secretQuestion[counter]);
                        choiceExist++;
                    }
                    System.out.println("secret questions array items" + secretQuestion[counter]);
                }
                inStream.close();
                
                System.out.println("program executed");
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. The chances that the problem isn't within the uncompilable snippet posted are rather high.

    And note that it's never necessary to upcast to Object.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    I am working on the SSCCE (the code being in GUI, it is very long to make..)

    All i know is that the problem IS coming from the reading from a file, because i can add items to the JComboBox when i don't read from a file (if I take out the file reader and put instead arrays that I fill manually, they do appear in the JComboBox).

    The (object) was just a test, i tried with (String) also to check if it was the problem, guess it wasn't. Thanks though.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    Quote Originally Posted by jiffi View Post
    I am working on the SSCCE (the code being in GUI, it is very long to make..)
    That's not how you create an SSCCE. Your problem is populating a JComboBox with the Strings read from a file. So you create a GUI with a JComboBox with a few items and a JButton that when clicked, reads the file and adds the items to the JComboBox. Maybe around 60 lines or less.

    If that works you can go back to your original program and find what you've done differently. If it doesn't work, you have a simple code that demonstrates the problem, and once you post it here, along with a sample of the text file, you stand a chance of getting meaningful help in a very short time.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    here is my code.
    Yes, the fact that i create a new Frame is on purpouse, that is the whole point of reading from a file.
    And yes, i used an array because i also use that in my code

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    class JavaForums2 extends JFrame implements ActionListener
    {
        JComboBox combobox;
        JButton saveButton;
        String[] items = {"item 1", "item2", "item3"};
    
        String [] array = new String[10];
        public static void main (String [] args)
        {
            JavaForums2 jf = new JavaForums2();
            jf.setVisible(true);
        }
    
        public JavaForums2()
        {
            setSize(300,200);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);//by default, quit terminal when program exits
    
            Container contentPane = new Container();
            contentPane = getContentPane();//to get the container
            contentPane.setLayout(null);
            contentPane.setBackground(Color.red);// set the background
    
            combobox = new JComboBox(items);
            combobox.setBounds(50,50,100,30);
            combobox.addActionListener(this);
            combobox.setVisible(true);
            combobox.setEditable(true);
            contentPane.add(combobox);
    
            saveButton = new JButton("<html>Put items from file to combo box</html>");
            saveButton.setBounds(150,100,150,60);
            saveButton.addActionListener(this);
            saveButton.setVisible(true);
            contentPane.add(saveButton);
    
        }
    
        public void actionPerformed(ActionEvent event)
        {
            String menuName;
            menuName = event.getActionCommand();
    
    
            if (menuName.equals("<html>Put items from file to combo box</html>"))
            {
                try 
                {
                    File outFile = new File("test.txt");
                    FileOutputStream outFileStream = new FileOutputStream(outFile);
                    PrintWriter outStream = new PrintWriter(outFileStream);
                    outStream.println(combobox.getSelectedItem());
                    outStream.close();
                    
                    this.setVisible(false);
                    JavaForums2 jf = new JavaForums2();
                    jf.setVisible(true);
                    
                    File inFile = new File("test.txt");
                    FileReader inStream = new FileReader(inFile);
                    BufferedReader bufReader = new BufferedReader(inStream);
    
                    
                        array[0] = bufReader.readLine();
                        combobox.addItem(array[0]);
                    inStream.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
    
        }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    Quote Originally Posted by jiffi View Post
    here is my code.
    Yes, the fact that i create a new Frame is on purpouse, that is the whole point of reading from a file.
    Please explain. You understand of course that you're making changes to the combo box, but of course not to the combo box that is now displayed just because it is in a new JavaForums2 object.
    Last edited by Fubarable; 12-10-2011 at 04:13 PM.

  7. #7
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    well I use two frames, that should display at the same time, and the values entered in one are passed to the other, and are automatically added when the frame is closed then reopened (or they should be).

    I thought though that if i first show the new frame and then I add the values they should be added to the NEW jcomboBox, not the old one… right??

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    Quote Originally Posted by jiffi View Post
    well I use two frames, that should display at the same time, and the values entered in one are passed to the other, and are automatically added when the frame is closed then reopened (or they should be).
    This is an unusual design choice and there may be better ways to interact with the user (if you tell us more about what you're trying to achieve).

    I thought though that if i first show the new frame and then I add the values they should be added to the NEW jcomboBox, not the old one… right??
    No. The String(s) are added to the combo box that is part of the current JavaForum2 object, the "this" of the code above. If you create a new JavaForum2 object, it is completely distinct from the current one. There's no way for it to "magically" know the state of the current object. If you want to create a JavaForum2 class that can accept an array of Strings and use it in its JComboBox, you'll want to give it a public method that accepts an array of Strings as its parameters and then in the method gets the JComboBox's model, iterates through the Strings in the parameter array, and places each String into the model. Say

    Java Code:
       public void addToCombo(String[] elements) {
          DefaultComboBoxModel model = (DefaultComboBoxModel) combobox.getModel();
          for (String element : elements) {
             model.addElement(element);
          }
       }

  9. #9
    jiffi is offline Member
    Join Date
    Oct 2011
    Posts
    79
    Rep Power
    0

    Default Re: Unable to add Items to a JComboBox when those items are read from a file

    I do not know how to use any code like that and i don't have time right now to learn it, but you just gave me an idea that will work for sure. Thanks!

Similar Threads

  1. Replies: 2
    Last Post: 02-03-2011, 05:19 AM
  2. Databse items to Jcombobox
    By pra.deep in forum AWT / Swing
    Replies: 6
    Last Post: 09-17-2009, 10:50 AM
  3. folder/file menu items
    By jhughes in forum Advanced Java
    Replies: 2
    Last Post: 07-21-2009, 10:57 PM
  4. how to save added items in jcombobox
    By kwink in forum AWT / Swing
    Replies: 2
    Last Post: 03-22-2009, 06:03 PM
  5. Adding items to a jComboBox
    By tronovan in forum New To Java
    Replies: 0
    Last Post: 08-08-2007, 08:48 AM

Posting Permissions

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