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
| import java.awt.Color;
import javax.swing.JList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class FenetreBD extends JFrame {
private JPanel container = new JPanel();
private String[] contenu = {"robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien", "robin", "dimitri", "thomas", "julien"};
private JList liste = new JList(contenu);
private JScrollPane frameliste = new JScrollPane(liste);
private JLabel reponse = new JLabel("...");
public FenetreBD(){
this.setTitle("Animation");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(null);
frameliste.setBackground(Color.white);
frameliste.setBounds(10,10,150,200);
reponse.setBounds(10, 220, 150, 20);
container.add(frameliste);
container.add(reponse);
this.setContentPane(container);
this.setVisible(true);
liste.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
// --méthode test qui marche : quand je clique dans la liste, ça ajoute au label
reponse.setText("il y a eu une selection");
// --méthode 2 :afficher la selection dans le label "reponse" : marche pas
//reponse.setText(liste.getSelectedValue());
// --méthode 3 : afficher l'index de la selection dans le label réponse : marche pas
//reponse.setText(liste.getSelectedIndex());
}
}
);
}
public static void main(String[] args) {
new FenetreBD();
}
} |
Partager