Salut,
Je ne devrais pas le faire mais c'est mon jour de bonté et comme je l'ai dans mes cartons...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Dimension;
import java.util.Arrays;
public class ComboBox2 extends JPanel
implements ActionListener
{
JLabel label;
JComboBox listeNoms;
String libelle;
String titre = "LISTE DE NOMS";
static String titre2 = "LISTE DE NOMS";
String laLettre = "";
String defaut = "*";
String lib = "\n\nEntrer le début du nom. Exemples: A, Al ...";
static Dimension screen;
static int w;
static int h;
boolean reSaisir = true;
public ComboBox2()
{
super (new BorderLayout());
// Ici le tableau nécessaire a la création de ComboBx2 est faites "en dur",
// mais peut etre faite en lisant par exemple un fichier noms, ou a partir d'une saisie de noms
// tableau de tous les noms:
String[] tableauNoms = {"moooonaaaa", "Aabbb", "Aaabb", "Abccc", "Abbbb", "Acdddd", "Accccc",
"Alain", "Alizz", "Aliaaa", "Aliwww", "Alibbcc", "Alibb", "Aliyyy", "Alidddd",
"Alifff", "Alieaa", "Alig",
"Baaaa", "Babbb", "Babcc", "Babcd", "Bbbbb", "Bbccc", "Bccccc", "Bcdddd",
"mopqrstuvw", "moprstuvwx", "mppppobbbb", "mpqqqrssss", "Bibi"};
// tri du tableau:
Arrays.sort(tableauNoms);
int nombre = 0;
while(reSaisir)
{
reSaisir false; // sera forcé a true si nombre 0
entreeClavier();
// comptage des noms qui commencent par laLettre (ou les lettres)
// si la lettre est ("*") on prend tout
for (int i = 0; i < tableauNoms.length; i++)
{
if (!laLettre.equals("*")) //c'est ce tableau qui sert a créer la combo
{
if (tableauNoms[i].startsWith(laLettre))
nombre++;
}
else
{
nombre++;
}
}
nombre++; // pour **FIN**
System.out.println("nombre = " + nombre);
if (nombre == 1)
{
javax.swing.JOptionPane.showMessageDialog
(null,"Il n'y a pas de nom qui commence par " + laLettre,
titre, JOptionPane.INFORMATION_MESSAGE);
reSaisir = true;
}
}
if (!reSaisir)
{
int j = 0;
String[] tableauReduit = new String[nombre]; //c'est ce tableau qui sert a créer la combo
// on crée le tableau réduit (qui sera complet si la lettre est("*"))
for (int i = 0; i < tableauNoms.length -1; i++)
{
if (!laLettre.equals("*"))
{
if (tableauNoms[i].startsWith(laLettre))
{
tableauReduit[j] = tableauNoms[i];
j++;
}
}
else
{
// si laLettre est ("*") on prend tout.
tableauReduit[j] = tableauNoms[i];
j++;
}
}
tableauReduit[j] = "**FIN**";
// création de la combo box
listeNoms = new JComboBox(tableauReduit); // c'est ici qu'on crée la combo
listeNoms.setSelectedIndex(0); // selection par defaut du nom n° 1
listeNoms.addActionListener(this);
// set up label
label = new JLabel();
label.setFont(label.getFont().deriveFont(Font.ITALIC));
label.setHorizontalAlignment(JLabel.CENTER);
updateLabel(tableauReduit[listeNoms.getSelectedIndex()]);
label.setBorder(BorderFactory.createEmptyBorder(40, 0, 0, 0)); // 40, 0, 0. 0
// taille
label.setPreferredSize(new Dimension(400, 300));
// lay out
add(listeNoms, BorderLayout.PAGE_START);
add(label, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
}
public void entreeClavier()
{
/*
*
* entree = (String)javax.swing.JOptionPane.showInputDialog
* (null, "Sortie par sans saisie " + lib, // message
* "INTRO CLAVIER", // titre
* javax.swing.JOptionPane.PLAIN_MESSAGE, // type message
* null, // icone (image)
* null,
* defaut); // valeur par defaut
*
*/
laLettre = (String)javax.swing.JOptionPane.showInputDialog
(null, "Sortie par .\n\n"
+ "La valeur (*) par defaut permet d'obtenir la liste de tous les noms.\n\n" + lib,
titre,
javax.swing.JOptionPane.PLAIN_MESSAGE,
null, // icone (image)
null,
defaut); // valeur par defaut
try
{
int longueurSaisie laLettre.length(); // si longueur null
}
catch (NullPointerException npe)
{
String info = "\n\nSORTIE PAR \n\n"
+ " FIN";
javax.swing.JOptionPane.showMessageDialog
(null,info,titre, JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
// Listen.
public void actionPerformed(ActionEvent ae)
{
JComboBox jcb = (JComboBox)ae.getSource();
String nomCas = (String)jcb.getSelectedItem();
System.out.println("nomSel : " + nomCas + " (Choisi).");
if (nomCas.equals("**FIN**"))
{
String info = "\n\nSORTIE PAR SÉLECTION DE **FIN**\n\n"
+ " FIN";
javax.swing.JOptionPane.showMessageDialog
(null,info,titre, JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
label.setText(libelle);
}
protected void updateLabel(String nom)
{
System.out.println("nomDef : " + nom + " (Selectionne par defaut au lancement).");
//libelle = "CHOISIR UN AUTRE NOM, OU BIEN, FERMER LA FEN\u00caTRE (X)";
libelle = "CHOISIR UN AUTRE NOM, OU, SÉLECTIONNER (**FIN**)";
label.setText(libelle);
}
public static void main(String s[])
{
//titre = "LISTE DE NOMS";
//System.out.println(test);
//Recupereration des dimensions de l'ecran utilise:
screen = Toolkit.getDefaultToolkit().getScreenSize();
w = screen.getSize().width;
h = screen.getSize().height;
System.out.println("WIDTH/HEIGHT: " + w + "/" + h);
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame fr = new JFrame("ComboBox2: (" + titre2 + ")");
fr.setBounds(w-450, 0, 450, h-30);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComponent newContentPane = new ComboBox2();
newContentPane.setOpaque(true);
fr.setContentPane(newContentPane);
fr.pack();
fr.setVisible(true);
}
}
Cordialement,
...\ Dan /...