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
| public class Test {
public static class Entreprise {
private String nom;
private String adresse;
public Entreprise(String nom, String adresse) {
this.nom = nom;
this.adresse = adresse;
}
public String getNom() {
return this.nom;
}
}
public static void main(String[] args) {
MainFrame m = new Test.MainFrame();
m.setVisible(true);
}
public static class MainFrame extends JFrame {
public MainFrame() {
super();
setMinimumSize(new Dimension(800,600));
setPreferredSize(new Dimension(800,600));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList list = new JList();
CarnetEntrepriseModel model = new CarnetEntrepriseModel();
model.addEntreprise(new Entreprise("Microsoft", "3 rue de la fenêtre"));
model.addEntreprise(new Entreprise("Apple", "5s rue de la poire"));
list.setModel(model);
getContentPane().add(list);
}
}
public static class CarnetEntrepriseModel extends AbstractListModel {
private List<Entreprise> entrepriseList;
public CarnetEntrepriseModel() {
this.entrepriseList = new ArrayList<Entreprise>();
}
public void addEntreprise(Entreprise entreprise) {
entrepriseList.add(entreprise);
}
@Override
public int getSize() {
return entrepriseList.size();
}
@Override
public Object getElementAt(int index) {
String result = null;
if (index < getSize()) {
Entreprise entreprise = entrepriseList.get(index);
result = entreprise == null ? "" : entreprise.getNom();
}
return result;
}
}
} |
Partager