On May 9, 3:23 am, miga <[email protected]> wrote:
> On May 8, 8:57 pm, "* ^ *" <[email protected]> wrote:> // Names of image files. 
> How can I improve this?
> > String[] imgs={"Albania.gif", "Austria.gif", "Cambodia.gif",
> > "Croatia.gif", "Lebanon.gif", Malaysia.gif"};
>
> > I would like to do this:
> > 1. Program reads directory that contains images.
> > 2. Put all image file names into an array.
> > Any ideas?
>
> You may have an array of ImageIcon.
> Then an array of countries.
> From the array of countries, you may build a JComboBox and use two
> final static int to position the selected index.
> Then you iterate over the array of countries to create the ImageIcon
> reading the directory where they are.
> If you map the name of the image to the array of the countries it
> should work.
> You may use getResource to retrieve your app resource path.
Thank you. It is working. I did it more or less as suggested. Below is
my code fragments:
        // Initialise comboBox with teams & set initial selection.
        String[] imgs={"Albania.gif", "Austria.gif", "Cambodia.gif",
"Croatia.gif", "Lebanon.gif", "Malaysia.gif"};
        String[] teams=removeFileExtension(imgs);
        final JComboBox teamsChoice=new JComboBox(teams);
        teamsChoice.setSelectedIndex(teams.length - 1);

        // Set initial icon in display label.
        String path="/images/flagicons";
        final ImageIcon[] flags=createImageIcons(path, imgs);
        displayLabel.setIcon(flags[flags.length - 1]);

        // Event handling for comboBox. Changes icon as per comboBox
selection.
        teamsChoice.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if(e.getActionCommand().equals("comboBoxChanged")){
                    displayLabel.setIcon(flags
[teamsChoice.getSelectedIndex()]);
                }
            }
        });

However, I seek to improve how comboBox is populated.
// This is not very elegant. What can I use to seek a directory of
images, and retrive filenames in it to populate the comboBox?
String[] imgs={"Albania.gif", "Austria.gif", "Cambodia.gif",
"Croatia.gif", "Lebanon.gif", "Malaysia.gif"};
String[] teams=removeFileExtension(imgs);
final JComboBox teamsChoice=new JComboBox(teams);
teamsChoice.setSelectedIndex(teams.length - 1);


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to