This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes External files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "External files" Watch "External files" New topic

External files

Rich Wilson

Joined: Jun 30, 2005
Posts: 17
am trying to populate a JComboBox named choices from an external file. I have the import/read portion working because if I add System.out.println(name[ i ]); to the code it will print the contents of the file on the screen. Is the code below correct to populate the box? I get an error code of "cannot find symbol" with eiter get or setText.


choices.getText(loan [ i ]);
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

If you've got an existing JComboBox and you want to set its list of items to the contents of a String array, you could do this:

combo.setModel(new DefaultComboBoxModel(names));

or if you have the "names" array around when you first create the combo box, you can just say

combo = new JComboBox(names);

[Jess in Action][AskingGoodQuestions]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
download a copy of the api docs, and you'll find (in JComboBox)

a method
public void addItem(Object anObject)

a constructor with an array as a parameter
public JComboBox(Object[] items)
"Creates a JComboBox that contains the elements in the specified array.
By default the first item in the array (and therefore the data model)
becomes selected. "
Rich Wilson

Joined: Jun 30, 2005
Posts: 17
ok I am still lost here can someone edit this example from Sun to give me a better idea of what I am doing wrong? I can make it pull the data but not fill a combobox.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I'm not sure what you're trying to do.

You have an editable comboBox, so I'm guessing you want the edited, or new,
value to be added to the drop-down list (when [enter] is hit)

if so, add this if statement to actionPerformed()

I agree. Here's the link:
subject: External files