-
ImageIcon problem
Basiclly for now my goal is a drop down list which shows pictures depending on what is selected.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JFrame{
private JLabel ProfilePicture;
private JComboBox HeroNames;
private static String[] Heroes = {"Alchemist", "Ancient_Apparition", "Anti-Mage", "Axe", "Beastmaster",
"Bloodseeker", "Bounty_Hunter", "Broodmother", "Chen", "Clinkz", "Clockwerk", "Crystal_Maiden",
"Dark_Seer", "Dazzle", "Death_Prophet", "Doom_Bringer", "Dragon_Knight", "Drow_Ranger", "Earthshaker",
"Enchantress", "Enigma", "Faceless_Void", "Huskar", "Invoker", "Jakiro", "Juggernaut", "Kunkka",
"Leshrac", "Lich", "Lifestealer", "Lina", "Lion", "Mirana", "Morphling", "Nature's_Prophet", "Necrolyte",
"Night_Stalker", "Omniknight", "Outworld_Destroyer", "Puck", "Pudge", "Queen_Of_Pain", "Razor", "Riki",
"Sand_King", "Shadow_Fiend", "Shadow_Shaman", "Silencer", "Skeleton_King", "Slardar", "Sniper", "Spectre",
"Spirit_Breaker", "Storm_Spirit", "Sven", "Tidehunter", "Tiny", "Tinker", "Ursa", "Vengeful_Spirit",
"Venomancer", "Viper", "Warlock", "Weaver", "Windrunner", "Witch_Doctor", "Zeus"};
private Icon[] pics;
public GUI(){
super("Dota 2 Helper");
for(int i = 0; i < 67 ; i++){
pics[i] = new ImageIcon(getClass().getResource(Heroes[i]+".png"));
}
setLayout(new FlowLayout());
HeroNames = new JComboBox(Heroes);
HeroNames.addItemListener(
new ItemListener(){
public void itemStateChanged(ItemEvent event){
if(event.getStateChange()==ItemEvent.SELECTED)
ProfilePicture.setIcon(pics[HeroNames.getSelectedIndex()]);
}
}
);
add(HeroNames);
ProfilePicture = new JLabel(pics[0]);
add(ProfilePicture);
}
}
Errors at:
pics[i] = new ImageIcon(getClass().getResource(Heroes[i]+".png"));
ProfilePicture = new JLabel(pics[0]);
add(ProfilePicture);
These might be related though
-
Re: ImageIcon problem
You mention "Errors at:..." but neglect to post the error message. That kind of is important information.
-
Re: ImageIcon problem
Exception in thread "main" java.lang.NullPointerException
at GUI.<init>(GUI.java:59)
at Main.main(Main.java:5)
main 5 is calling the class
-
Re: ImageIcon problem
You will need to instantiate the pics array before using it. i.e.,
Code:
private Icon[] pics = new Icon[someNumberGoesHere];
-
Re: ImageIcon problem
*cries tears of joy*
Thank you so much, good sir!
-
Re: ImageIcon problem