• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need your help with JcomboBox

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello to all!
I need your help with JcomboBox, while setting files in it.
Examine following code:

File[] file_list = getFileList();
JComboBox box = new JComboBox(file_list);
JFrame frame = new JFrame();
frame.add(box);
frame.pack();
frame.setVisible(true);

Assume getFileList() is a function that returns an list of files residing in a specific directory and cannot be changed.

Above code displays a frame with a combo box where I can choose between all the files.
My problem is the files are shown with their full path (for example c:\documents and settings\administrator\.....) which could be very long and pointless since they're all in the same directory.
I want to see only the file name.

Said that there are 2 ways to fix the above code.

Note:
Values in combobox should still be files, meaning I will be able to do:
File f = (File)box.getSelectedValue();

I have searched many forums, books but still not find any solution.
Will very much appreciate your help!
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Slava, and welcome to the Ranch! Please BeForthrightWhenCrossPostingToOtherSites.
http://www.java-forums.org/awt-swing/41585-need-help-jcombobox.html

You could use a custom renderer that shows only the file name. Read the API for JComboBox and follow the link to the Swing tutorial on "How to Use Combo boxes" where you can find out more about custom renderers.
 
reply
    Bookmark Topic Watch Topic
  • New Topic