1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
public class WChoice extends JPanel implements ItemListener {
private JComboBox meschoix;
ImageIcon[] images;
String[] logoStrings = { "Lyon", "Bordeaux", "Lille", "Lens", "Marseille","Rennes", "Auxerre", "Paris", "Nice", "Le_Mans", "Monaco", "Nancy", "Nantes", "St-Etienne", "Toulouse", "Sochaux", "Troyes", "Ajaccio","Strasbourg", "Metz" };
public void init() {
images = new ImageIcon[logoStrings.length];
Integer[] intArray = new Integer[logoStrings.length];
Championnat ch = new Championnat("fic.res");
str = new String[ch.getNbrMaxEq()];
maxInd = ch.getNbrMaxEq();
for (int i = 0; i < ch.getNbrMaxEq(); i++) {
str[i] = ch.getNomEquipe(i + 1);
intArray[i] = new Integer(i);
images[i] = createImageIcon("images/"+logoStrings[i] + ".jpg");
if (images[i] != null)
images[i].setDescription(logoStrings[i]);
JComponent newContentPane = new WChoice();
newContentPane.setOpaque(true);
}
// Creation du combo box
meschoix = new JComboBox(intArray);
// mon custom d'un combobox
ComboBoxRenderer renderer = new ComboBoxRenderer();
// taille du combobox
renderer.setPreferredSize(new Dimension(80, 50)); meschoix.setRenderer(renderer);
meschoix.setMaximumRowCount(3);
add(meschoix, BorderLayout.PAGE_START);
meschoix.addItemListener(this);
}
protected static ImageIcon createImageIcon(String path) {
URL imgURL = WChoice.class.getResource(path);
if (imgURL != null)
return new ImageIcon(imgURL);
else { System.err.println("Couldn't find file: " + path); return null; }
}
...
// cette classe se trouve toujours dans la première classe initiale WChoice
class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private Font monFont;
/** Procédure qui définit le nouveau Combobox */
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(LEFT);
setVerticalAlignment(TOP);
}
/** Fonction qui trouve l'image et le texte correspondant au choix */
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
int selectedIndex = ((Integer) value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
ImageIcon icon = images[selectedIndex];
String logo = logoStrings[selectedIndex];
setIcon(icon);
if (icon != null) {
setText(logo);
setFont(list.getFont());
} else { setmonText(logo + " (no image available)", list.getFont()); }
return this;
}
/** Fonction qui écrit le texte */
protected void setmonText(String monText, Font normalFont) {
if (monFont == null)
monFont = normalFont.deriveFont(Font.ITALIC);
setFont(monFont);
setText(monText);
}
} |
Partager