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
| import java.awt.Color;
import java.util.Arrays;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
public class Test {
public static void main(String[] args) {
Object[][] donnees = {
{10, "Astral", "standard", Color.red, Boolean.TRUE},
{5, "Mistral", "standard", Color.yellow, Boolean.FALSE},
{50, "Oasis", "standard", Color.blue, Boolean.FALSE},
{40, "boomerang", "compétition", Color.green, Boolean.TRUE},
{25, "Omega", "performance", Color.cyan, Boolean.TRUE},
} ;
String[] titreColonnes = { "index","modèle", "homologation",
"couleur", "vérifiée ?"};
JTable table = new JTable(donnees, titreColonnes);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(table));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// récupérer les valeurs de la colonne
int[] tableau = toIntArray(table, 0);
// trier
Arrays.sort(tableau);
// afficher les valeur les plus forte et plus faible
System.out.println("Valeur la plus faible : "+tableau[0]);
System.out.println("Valeur la plus forte : "+tableau[tableau.length - 1]);
}
private static int[] toIntArray(JTable table, int column) {
TableModel model = table.getModel();
int[] tableau = new int[model.getRowCount()];
for (int row = 0 ; row < model.getRowCount() ; row++) {
Object o = model.getValueAt(row, column);
if (o instanceof Integer) {
tableau[row] = (Integer) o;
}
}
return tableau;
}
} |
Partager