Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

External files

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

CODE

choices.getText(loan [ i ]);
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()

 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic