JList Renderer

bruncejs Messages postés 3 Date d'inscription vendredi 17 octobre 2003 Statut Membre Dernière intervention 5 mai 2004 - 23 avril 2004 à 13:25
bruncejs Messages postés 3 Date d'inscription vendredi 17 octobre 2003 Statut Membre Dernière intervention 5 mai 2004 - 25 avril 2004 à 14:53
Bonjour à tous!!

J'ai réalisé une JList avec des images mais je n'arrive pas à récupéré l'index de la ligne sélectionnée!!! je vous montre le code; merci d'avance!!!

public Accueil_GUI()
{
/********** INITIALISATION DES ELEMENTS DU PANEL DE DROITE **********/
lbl_desc_accueil = new JLabel("coucou bruno");
lbl_desc_accueil.setFont(new Font("Verdana",Font.PLAIN,12));

lbl_accueil_droit = new JLabel("Accueil");
lbl_accueil_droit.setFont(font_libelle_lst);

jp_accueil_droit = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_accueil_droit.add(new JLabel(new ImageIcon("gif/accueil_info.gif")));
jp_accueil_droit.add(lbl_accueil_droit);

/********** INITIALISATION DU LABEL REPTEL **********/
lbl_reptel = new JLabel("RepTel");
lbl_reptel.setFont(font_libelle_TITRE);

/********** INITIALISATION DES JPANELS PRINCIPAUX **********/
panel_haut = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel_gauche = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel_droit = new JPanel(new GridLayout(2,1));


/********** INITIALISATION DU JPANEL BIENVENUE **********/
jp_bienvenue = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_bienvenue.add(new JLabel(new ImageIcon("gif/img_accueil.gif")));


/********** INITIALISATION DES ELEMENTS CONCERNANT LA LISTE DES OPTIONS **********/
lbl_lst_accueil = new JLabel("Accueil");
lbl_lst_accueil.setFont(font_libelle_lst);
jp_accueil = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_accueil.add(new JLabel(new ImageIcon("gif/accueil_info.gif")));
jp_accueil.add(lbl_lst_accueil);

lbl_lst_mon_compte = new JLabel("Mon compte");
lbl_lst_mon_compte.setFont(font_libelle_lst);
jp_mon_compte = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_mon_compte.add(new JLabel(new ImageIcon("gif/mon_compte.gif")));
jp_mon_compte.add(lbl_lst_mon_compte);

lbl_lst_oubli_pass = new JLabel("Oubli du mot de passe");
lbl_lst_oubli_pass.setFont(font_libelle_lst);
jp_oubli_pass = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_oubli_pass.add(new JLabel(new ImageIcon("gif/oubli_pass.gif")));
jp_oubli_pass.add(lbl_lst_oubli_pass);

lbl_lst_inscription = new JLabel("Nouvelle inscription");
lbl_lst_inscription.setFont(font_libelle_lst);
jp_inscription = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_inscription.add(new JLabel(new ImageIcon("gif/inscription.gif")));
jp_inscription.add(lbl_lst_inscription);

lbl_lst_quitter = new JLabel("Quitter");
lbl_lst_quitter.setFont(font_libelle_lst);
jp_quitter = new JPanel(new FlowLayout(FlowLayout.LEFT));
jp_quitter.add(new JLabel(new ImageIcon("gif/quitter.gif")));
jp_quitter.add(lbl_lst_quitter);

vector_lst_options = new Vector();

vector_lst_options.addElement(jp_accueil);
vector_lst_options.addElement(jp_mon_compte);
vector_lst_options.addElement(jp_oubli_pass);
vector_lst_options.addElement(jp_inscription);
vector_lst_options.addElement(jp_quitter);

lst_options = new JList(); //création de la JList
lst_options.setListData(vector_lst_options); //ajout des données du vector
lst_options.setCellRenderer(new CustomCellRenderer()); //lors du clic
lst_options.setSelectedIndex(0); //on sélectionne accueil par défaut
lst_options.setBorder(new LineBorder(Color.black)); //couleur du bord
lst_options.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //un seul élément


/********** AJOUT DES ELEMENTS AUX JPANELS **********/
panel_haut.add(jp_bienvenue);
panel_haut.add(lbl_reptel);
panel_gauche.add(lst_options);
panel_droit.add(jp_accueil_droit);
panel_droit.add(lbl_desc_accueil);
//panel_droit.setBorder(new LineBorder(Color.black));


/********** AJOUT DES JPANELS AU PANEL RACINE EN LES POSITIONNANT **********/
getContentPane().add(panel_haut,BorderLayout.NORTH);
getContentPane().add(panel_gauche,BorderLayout.WEST);
getContentPane().add(panel_droit,BorderLayout.CENTER);


/********** INITIALISATION DE LA JFRAME **********/
setSize(700,380);
setResizable(false);
setTitle("Bienvenue sur RepTel le répertoire téléphonique");
setVisible(true);


/********** METHODES AGISSANT SUR LA JFRAME **********/
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
affiche_message_quitter();
}
});

/*lst_options.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{

}
});*/


}//FIN DU CONSTRUCTEUR

public void affiche_message_quitter()
{
/*int test;
test = JOptionPane.showConfirmDialog(null,
"Etes-vous sûr(e) de vouloir quitter RepTel?",
"Quitter",
JOptionPane.YES_NO_OPTION);
if(test==0)*/ System.exit(0);
}

//CLASS PERMETTANT DE GERER LES CELLULES DE LA JLIST "LST_OPTIONS"
class CustomCellRenderer implements ListCellRenderer
{
public Component getListCellRendererComponent
(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus)
{
Component component = (Component)value;
component.setBackground(isSelected ? Color.lightGray : Color.white);
component.setForeground(isSelected ? Color.white : Color.white);
return component;
}
}
}

2 réponses

kirua12 Messages postés 1155 Date d'inscription samedi 17 janvier 2004 Statut Membre Dernière intervention 29 avril 2011 7
23 avril 2004 à 14:38
Salut,

en lisant la javadoc de JList :
getSelectedIndex() : Returns the first selected index; returns -1 if there is no selected item.
0
bruncejs Messages postés 3 Date d'inscription vendredi 17 octobre 2003 Statut Membre Dernière intervention 5 mai 2004
25 avril 2004 à 14:53
merci à toi!! en fait, je connaissais déjà cette méthode mais si tu veux j'avais oublié de déclarer des "import" du type "import javax.swing.event.*;" et donc ça ne marchait pas et je comprenais pas!! ceci étant dit, merci de ton aide et de ta réponse!!
0
Rejoignez-nous