J'ai du mal à me retrouver dans cet doc car j'utilise le logiciel Netbeans et ma jComboBox est déjà créée et il instancie des paramètres pour la création de sa fenêtre, que j'ai déjà.
Faut-il obligatoirement créer la jComboBox avec ça ?
JComboBox petList = new JComboBox(intArray);
Et faut il obligatoirement créer une classe ListCellrender ?
Appel: ComboBoxRenderer renderer= new ComboBoxRenderer();
Cela :
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
| class ComboBoxRenderer extends JLabel
implements ListCellRenderer {
private Font uhOhFont;
public ComboBoxRenderer() {
setOpaque(true);
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
/*
* 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];
String pet = petStrings[selectedIndex];
setIcon(icon);
if (icon != null) {
setText(pet);
setFont(list.getFont());
} else {
setUhOhText(pet + " (no image available)",
list.getFont());
}
return this;
} |
Voici la class existante qui rempli déjà ma jCOmboBox grâce à la table Mysql et en plus, compare si la BDD dit vrai. Si le chemin du dossier est bien existant.
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
| public void RefOpe() {
Conf.Path = "R:"+File.separator+"Fichiers"+File.separator+jComboBox1.getSelectedItem().toString();
//System.out.println(Conf.Path);
int i;
File[] PathFile= new File(Conf.Path).listFiles();
//jComboBox3.setRenderer(PathFile.length); // c'est l'initialisatrion du composant ?
int idClient1 = id_Client(jComboBox1.getSelectedItem().toString());
mainQuery = test_reception_fichierPUEntityManager.createNamedQuery("Main.findByIdClientM");
mainQuery.setParameter("idClientM", idClient1);
jComboBox3.removeAllItems();
for (Object Ope : mainQuery.getResultList()) {
Main operation = (Main)Ope;
for (i=0; i < PathFile.length; i++) {
// System.out.println(operation.toString()+" = "+PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1));
//System.out.println(operation.toString()+" - "+PathFile[i].toString());
//System.out.println(PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11).equals(operation.toString().substring(0,10))+" "+operation.toString().substring(0,10)+" / "+PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11));
if ( PathFile[i].isDirectory() && PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1).equals(operation.toString()) ){
jComboBox3.addItem(operation.toString());
// Background Vert = Strictement identique
i = PathFile.length+1;
}
else if ( PathFile[i].isDirectory() && PathFile[i].toString().substring(PathFile[i].toString().lastIndexOf("\\")+1, PathFile[i].toString().lastIndexOf("\\")+11).equals(operation.toString().substring(0,10)) ){
jComboBox3.addItem(operation.toString());
// Background Orange = Que le début qui est identique
i = PathFile.length+1;
}
}
if ( PathFile.length == i){
jComboBox3.addItem(operation.toString());
// Background Rouge = Dossier non présent.
}
}
} |
Partager