I have a list of JLabels that I am insterting into my JPanel:
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/orc_male.png", "Orc Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/tundrian_male.png", "Tundrian Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/brimlock_male.png", "Brimlock Male")));
I want to add tooltiptext to each of them. Is there any better way than to use a temp variable to hold one of their values and then keep re-using it?
JLabel temp = new JLabel();
temp = new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male"));
temp.setToolTipText("Human Male");
avatarGridPanel.add(temp);
I tried doing something like this(below) but could not get it to work. Thanks for any help!
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male")).setToolTipText("Human Male"));
You could create a function to create these for you. I do it sometimes when I have a big array and need to the same thing over and over:
private static JLabel makeLabel(String path, String name) {
JLabel label = new JLabel(new ImageIcon(path, name));
label.setToolTipText(name);
return label;
}
Then elsewhere in that class:
avatarGridPanel.add(makeLabel("images/gui/orc_male.png", "Orc Male"));