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 71 72 73 74 75 76 77 78 79
| import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import java.util.stream.Stream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class JTableExemple {
public static void main(String[] args) {
JFrame frame = new JFrame();
String[] col = {"Col 1","Col 2"};
AtomicInteger idfactory = new AtomicInteger(1);
Supplier<Object[]> rowgenerator = ()-> new Object[] { idfactory.getAndIncrement(), ThreadLocalRandom.current().nextInt() };
Object[][] data = Stream.generate(rowgenerator).limit(100).toArray(Object[][]::new);
DefaultTableModel tableModel = new DefaultTableModel(data,col);
JTable table = new JTable(tableModel);
AtomicInteger clickCount = new AtomicInteger();
String traceFormat = "Invocation getTableCellRendererComponent pour %1$s.%nNombre de lignes : %2$d.%n";
AtomicReference<String> formatRef = new AtomicReference<>(traceFormat);
String rowTraceFormat = "ligne %d, column %d, %s";
class MyRenderer extends DefaultTableCellRenderer {
public MyRenderer() {
setHorizontalAlignment(JLabel.RIGHT);
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Object[] rowData = { tableModel.getValueAt(row, 0), tableModel.getValueAt(row, 1) };
System.out.printf(
formatRef.get(),
String.format(rowTraceFormat, row, column, Arrays.toString(rowData)),
table.getRowCount(),
clickCount.get());
return component;
};
};
table.setDefaultRenderer(Object.class, new MyRenderer());
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table));
frame.add(panel);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton button = new JButton("Ajouter une ligne");
button.addActionListener(e->{
clickCount.incrementAndGet();
String newtraceFormat = "Ajout d'une ligne %3$d.%n" + traceFormat;
formatRef.set(newtraceFormat);
tableModel.addRow(rowgenerator.get());
});
buttonPanel.add(button);
panel.add(buttonPanel,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
L'affichage avant de cliquer sur le bouton :
Invocation getTableCellRendererComponent pour ligne 0, column 0, [1, 366167011].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 0, column 1, [1, 366167011].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 1, column 0, [2, -1702220055].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 1, column 1, [2, -1702220055].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 2, column 0, [3, 1489430607].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 2, column 1, [3, 1489430607].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 3, column 0, [4, -86647432].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 3, column 1, [4, -86647432].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 4, column 0, [5, -722846418].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 4, column 1, [5, -722846418].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 5, column 0, [6, -186805478].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 5, column 1, [6, -186805478].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 6, column 0, [7, 1087626999].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 6, column 1, [7, 1087626999].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 7, column 0, [8, 616411156].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 7, column 1, [8, 616411156].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 8, column 0, [9, 947709073].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 8, column 1, [9, 947709073].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 9, column 0, [10, -1665835107].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 9, column 1, [10, -1665835107].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 10, column 0, [11, 1891308892].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 10, column 1, [11, 1891308892].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 11, column 0, [12, -1537931540].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 11, column 1, [12, -1537931540].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 12, column 0, [13, -1932611427].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 12, column 1, [13, -1932611427].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 13, column 0, [14, 50961568].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 13, column 1, [14, 50961568].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 14, column 0, [15, -181799284].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 14, column 1, [15, -181799284].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 15, column 0, [16, 1305536022].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 15, column 1, [16, 1305536022].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 16, column 0, [17, -429787450].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 16, column 1, [17, -429787450].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 17, column 0, [18, 432956467].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 17, column 1, [18, 432956467].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 18, column 0, [19, 718849351].
Nombre de lignes : 100.
Invocation getTableCellRendererComponent pour ligne 18, column 1, [19, 718849351].
Nombre de lignes : 100.
Puis je clique sur le bouton :
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 0, column 0, [1, 366167011].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 0, column 1, [1, 366167011].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 1, column 0, [2, -1702220055].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 1, column 1, [2, -1702220055].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 2, column 0, [3, 1489430607].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 2, column 1, [3, 1489430607].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 3, column 0, [4, -86647432].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 3, column 1, [4, -86647432].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 4, column 0, [5, -722846418].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 4, column 1, [5, -722846418].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 5, column 0, [6, -186805478].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 5, column 1, [6, -186805478].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 6, column 0, [7, 1087626999].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 6, column 1, [7, 1087626999].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 7, column 0, [8, 616411156].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 7, column 1, [8, 616411156].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 8, column 0, [9, 947709073].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 8, column 1, [9, 947709073].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 9, column 0, [10, -1665835107].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 9, column 1, [10, -1665835107].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 10, column 0, [11, 1891308892].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 10, column 1, [11, 1891308892].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 11, column 0, [12, -1537931540].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 11, column 1, [12, -1537931540].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 12, column 0, [13, -1932611427].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 12, column 1, [13, -1932611427].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 13, column 0, [14, 50961568].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 13, column 1, [14, 50961568].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 14, column 0, [15, -181799284].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 14, column 1, [15, -181799284].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 15, column 0, [16, 1305536022].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 15, column 1, [16, 1305536022].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 16, column 0, [17, -429787450].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 16, column 1, [17, -429787450].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 17, column 0, [18, 432956467].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 17, column 1, [18, 432956467].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 18, column 0, [19, 718849351].
Nombre de lignes : 101.
Ajout d'une ligne 1.
Invocation getTableCellRendererComponent pour ligne 18, column 1, [19, 718849351].
Nombre de lignes : 101.
Le nombre d'invocation n'est pas du tout en rapport avec le nombre de lignes (100 puis 101) et de renderers (1). Pour le click sur le bouton, la nouvelle ligne n'est pas visible (à priori, selon la taille de police par défaut), et, alors qu'on ajoute qu'une seule ligne, le renderer est réinvoqué pour toutes les lignes, sur mon environnement en tout cas).
Partager