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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
public class TableCompnent extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column)
{
if(value instanceof JButton)
{
return(JButton)value;
}
else
return this;
}
}
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
public class Fenetre extends JFrame
{
private JTable tableau;
private JButton change = new JButton("Changer la taille");
private Object[][] data;
public Fenetre()
{
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("JTable");
this.setSize(600, 140);
Object[][] data =
{
{"Cysboy", new JButton("6boy"),new Double(1.80),new Boolean(true)},
{"BZHHydde",new JButton("BZH"),new Double(1.78),new Boolean(false)},
{"IamBow",new JButton("Bon"),new Double(1.90),new Boolean(false)},
{"FunMan", new JButton("Year"),new Double(1.85),new Boolean(true)}
};
String title[] = {"Pseudo", "Age", "Taille","OK?"};
ZModel model=new ZModel(data,title);
System.out.println("Nombre de colonne:"+model.getColumnCount());
System.out.println("Nombre de ligne:"+model.getRowCount());
this.tableau = new JTable((TableModel) model);
this.getContentPane().add(new JScrollPane(tableau), BorderLayout.CENTER);
}
class ZModel extends AbstractTableModel
{
private Object[][]data;
private String[]title;
/*Constructeur
* @param data
* @param title
*/
public ZModel(Object[][]data,String[]title)
{
this.data=data;
this.title=title;
}
/*Retourne le nombre de colonnes */
public int getColumnCount()
{
return this.title.length;
}
/* Retourne le nombre de lignes */
public int getRowCount()
{
return this.data.length;
}
/* Retourne le titre de la colonne à l'indice spécifié */
public String getColumnName(int col)
{
return this.title[col];
}
}
/* Retourne la classe de la donnée de la colonne*/
public Class getColumnClass(int col)
{
return this.data[0][col].getClass();
}
/* Retourne vrai si la cellule éditable:celle-ci sera donc éditable
* @return boolean
* */
public boolean isCellEditable(int row,int col)
{
if(getValueAt(0,col)instanceof JButton)
return false;
return true;
}
/* Retourne la valeur à l'emplacement spécifié*/
public Object getValueAt(int row,int col)
{
return this.data[row][col];
}
public static void main(String[]args)
{
Fenetre fen=new Fenetre();
fen.setVisible(true);
}
} |
Partager