Bonjour,
Voici un petit exemple:
import
java.awt.*;
import
java.awt.event.*;
import
javax.swing.*;
import
java.awt.Dimension;
public
class
ComboBox2
extends
JPanel
implements
ActionListener
{
JLabel label;
JComboBox listeNoms;
String libelle;
static
Dimension screen;
static
int
w;
static
int
h;
public
ComboBox2()
{
super
(
new
BorderLayout());
// tableau de tous les noms (qui sont "bidons" sauf moooonaaaa:
String[] tableauNoms = {
"Aaaaa"
,
"Aabbb"
,
"Abbbb"
,
"Abccc"
,
"Accccc"
,
"Acdddd"
,
"Baaaa"
,
"Babbb"
,
"Babcc"
,
"Babcd"
,
"Bbbbb"
,
"Bbccc"
,
"Bccccc"
,
"Bcdddd"
,
"moooonaaaa"
,
"mopqrstuvw"
,
"moprstuvwx"
,
"mppppobbbb"
,
"mpqqqrssss"
};
int
nombre = 0;
//Supposons qu'ici on entre la lettre m ou les lettres mo ...
String laLettre =
"m"
;
// c'est donc une simulation
int
j = 0;
// comptage des noms qui commencent par laLettre (ou les lettres)
for
(
int
i = 0; i < tableauNoms.length; i++)
{
if
(tableauNoms[i].startsWith(laLettre))
nombre++;
}
// c'est le tableau suivant qui servira a créer la combo
String[] tableauReduit =
new
String[nombre];
// sa taille (nombre de noms) sera exactement egale au nombre de noms
// qui commence par ("m") dans cet exemple.
// on crée donc le tableau réduit
for
(
int
i = 0; i < tableauNoms.length; i++)
{
if
(tableauNoms[i].startsWith(laLettre))
{
tableauReduit[j] = tableauNoms[i];
j++;
}
}
// création de la combo box
listeNoms =
new
JComboBox(tableauReduit);
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, 400));
// lay out
add(listeNoms, BorderLayout.PAGE_START);
add(label, BorderLayout.PAGE_END);
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
// Listen.
public
void
actionPerformed(ActionEvent ae)
{
JComboBox jcb = (JComboBox)ae.getSource();
String nomCas = (String)jcb.getSelectedItem();
System.out.println(
"L 85 nomSel : "
+ nomCas +
" (Choisi)."
);
label.setText(libelle);
}
protected
void
updateLabel(String nom)
{
System.out.println(
"L 91 nomDef : "
+ nom +
" (Selectionne par defaut au lancement)."
);
libelle =
"CHOISIR UN AUTRE NOM, OU BIEN, FERMER LA FEN\u00caTRE (X)"
;
label.setText(libelle);
}
public
static
void
main(String s[])
{
String test =
"hello "world" ... "
;
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:"
);
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
);
}
}
Bon ce n'est pas complet puisqu'on ne boucle pas pour entrer une ou plusieurs autres lettres mais tu as quelque chose pour continuer.
Cordialement,
...\ Dan /...