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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package com.ourco.util;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WChoice extends JPanel implements ItemListener {
private JComboBox meschoix;
private String str[];
private int ind;
private int maxInd;
private String logo;
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("test.txt");
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);
ComboBoxRenderer renderer = new ComboBoxRenderer(); //mon custom d'un combobox
renderer.setPreferredSize(new Dimension(130, 50)); //taille du combobox
meschoix.setRenderer(renderer);
meschoix.setMaximumRowCount(3);
// Lay out the demo.
add(meschoix, BorderLayout.PAGE_START);
meschoix.addItemListener(this);
}
/** Retourne une imageIcone sinon null si le path n'est pas valide */
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = WChoice.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void itemStateChanged(ItemEvent e) {
ind = -1;
for (int i = 0; i < maxInd; i++) {
System.out.println("valeur de str:"+str[i]);
System.out.println("valeur de i:"+i);
System.out.println("valeur de getItem:"+ e.getItem());
System.out.println("valeur de logo:"+ logo);
if ((logo).compareTo(str[i]) == 0)
ind = i + 1;
System.out.println("valeur de ind dans if:"+ind+"\n");
}
}
public int getInd() {
if (ind == 0)
ind = 1;
System.out.println("valeur de ind WChoice1 retourné:"+ind);
return ind;
}
class ComboBoxRenderer extends JLabel implements ListCellRenderer {
private Font monFont;
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(LEFT);
setVerticalAlignment(TOP);
}
/**
* This method finds the image and text corresponding to the selected
* value and returns the label, set up to display the text and image.
*/
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
// Get the selected index. (The index param isn't
// always valid, so just use the value.)
int selectedIndex = ((Integer) value).intValue();
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
// Set the icon and text. If icon was null, say so.
ImageIcon icon = images[selectedIndex];
logo ="";
logo = logoStrings[selectedIndex];
setIcon(icon);
if (icon != null) {
setText(logo);
setFont(list.getFont());
} else {
setmonText(logo + " (no image available)", list.getFont());
}
return this;
}
// Set the font and text when no image was found.
protected void setmonText(String monText, Font normalFont) {
if (monFont == null) { // lazily create this font
monFont = normalFont.deriveFont(Font.ITALIC);
}
setFont(monFont);
setText(monText);
}
}
} |
Partager