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 80
| public class TableObjetAbstrait {
private JTableModel myModel;
public JTable table;
public JScrollPane scrollPane;
private int selectedRowCourant = -1;
private Object[][] data={{"toto", "tata" , "null"},};
public TableObjetAbstrait() {
myModel = new JTableModel();
TableSorter sorter = new TableSorter(myModel);
table = new JTable(sorter);
sorter.addMouseListenerToHeaderInTable(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
table.setBackground( Constantes.PANEACTIVE );
table.setSelectionBackground( Constantes.SELECTIONACTIVE );
table.setPreferredScrollableViewportSize(new Dimension(300, 90));
table.addMouseListener( myMouseListener );
//Create the scroll pane and add the table to it.
scrollPane = new JScrollPane(table);
scrollPane.setOpaque(true);
JViewport j = scrollPane.getViewport();
j.setBackground( Constantes.PANEACTIVE );
//Add the scroll pane to this window.
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSelectionAllowed(true);
}
public void changedata(){
System.out.println("Change données de la table...");
Object[][] datatmp = {{"titi", "toto" , "null"},};
data=datatmp;
System.out.println(data[0][0]); //celui ci affiche titi
//myModel.fireTableRowsInserted( data.length-1, data.length-1 );
//myModel.fireTableDataChanged() ;
}
class JTableModel extends AbstractTableModel{
static final long serialVersionUID = 1510L;
final String[] columnNames = {"Nom", "Observation" };//, "oid"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
System.out.println("getValueAt = " +data[row][col]);
return data[row][col];
}
}//fin JTableModel
//ce main fonctionne correctement....
public static void main(String[] args) {
TableObjetAbstrait panel = new TableObjetAbstrait();
JFrame frame = new JFrame("TableObjetAbstrait");
panel.changedata();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(panel.getJScrollPane(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
} |
Partager