Results 1 to 9 of 9
- 12-10-2011, 02:28 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
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(); }
- 12-10-2011, 04:09 AM #2
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-10-2011, 04:33 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
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.
- 12-10-2011, 02:22 PM #4
Re: Unable to add Items to a JComboBox when those items are read from a file
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-10-2011, 03:21 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
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(); } } } }
-
Re: Unable to add Items to a JComboBox when those items are read from a file
Last edited by Fubarable; 12-10-2011 at 04:13 PM.
- 12-10-2011, 04:23 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
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??
-
Re: Unable to add Items to a JComboBox when those items are read from a file
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).
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. SayI 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??
Java Code:public void addToCombo(String[] elements) { DefaultComboBoxModel model = (DefaultComboBoxModel) combobox.getModel(); for (String element : elements) { model.addElement(element); } }
- 12-10-2011, 04:54 PM #9
Member
- Join Date
- Oct 2011
- Posts
- 79
- Rep Power
- 0
Similar Threads
-
Unable to select/deselect items in a JList while it is performing some task
By Subhalaxmi in forum AWT / SwingReplies: 2Last Post: 02-03-2011, 05:19 AM -
Databse items to Jcombobox
By pra.deep in forum AWT / SwingReplies: 6Last Post: 09-17-2009, 10:50 AM -
folder/file menu items
By jhughes in forum Advanced JavaReplies: 2Last Post: 07-21-2009, 10:57 PM -
how to save added items in jcombobox
By kwink in forum AWT / SwingReplies: 2Last Post: 03-22-2009, 06:03 PM -
Adding items to a jComboBox
By tronovan in forum New To JavaReplies: 0Last Post: 08-08-2007, 08:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks