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
|
public JComboBox<Produit> getComboBoxProduits() {
if (comboBoxProduits == null) {
comboBoxProduits = new JComboBox<Produit>();
comboBoxProduits.setPreferredSize(new Dimension(278, 23));
comboBoxProduits.setMinimumSize(new Dimension(278, 23));
try
{
// On récupère tous les noms de produits présents dans la BDD
listeProduits = Requete4.listerProduits(bdd);
}
catch (SQLException e)
{
System.out.println("Sql Erreur : " + e.getMessage());
}
if (listeProduits != null)
{
comboBoxProduits.setModel(new ModeleComboBoxProduits(listeProduits));
comboBoxProduits.setSelectedIndex(-1);
comboBoxProduits.setRenderer(new MyCellRenderer());
}
}
return comboBoxProduits;
}
/**
* Modèle de la comboBox comboBoxProduit
*/
class ModeleComboBoxProduits extends DefaultComboBoxModel<Produit>{
private static final long serialVersionUID = -5812040723015922852L;
private Vector<Produit> listeProduits;
/**
* Création du modèle
*
* @param listeOp la liste des produits
*/
public ModeleComboBoxProduits(Vector<Produit> listeProduits)
{
super();
this.listeProduits = listeProduits;
}
@Override
public int getSize()
{
return listeProduits.size();
}
@Override
public Produit getElementAt(int index)
{
return listeProduits.get(index);
}
@Override
public int getIndexOf(Object element)
{
return listeProduits.indexOf(element);
}
}
class MyCellRenderer extends DefaultListCellRenderer{
private static final long serialVersionUID = 2937187796573906670L;
/* (non-Javadoc)
* @see javax.swing.ListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int, boolean, boolean)
*/
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus)
{
MyCellRenderer c = (MyCellRenderer) (super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus));
for (int i = 0 ; i < listeProduits.size(); i++)
{
if (! listeProduits.get(i).getDangerosite().equals(""))
{
if (index == i)
{
c.setForeground(Color.RED);
}
}
}
return c;
}
} |
Partager