CCB Premier Import
This commit is contained in:
6
TestTableau/.classpath
Normal file
6
TestTableau/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
TestTableau/.project
Normal file
17
TestTableau/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>TestTableau</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
11
TestTableau/.settings/org.eclipse.jdt.core.prefs
Normal file
11
TestTableau/.settings/org.eclipse.jdt.core.prefs
Normal file
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
BIN
TestTableau/bin/modele/ButtonEditor$ButtonListener.class
Normal file
BIN
TestTableau/bin/modele/ButtonEditor$ButtonListener.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/ButtonEditor.class
Normal file
BIN
TestTableau/bin/modele/ButtonEditor.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/ButtonRenderer.class
Normal file
BIN
TestTableau/bin/modele/ButtonRenderer.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/ComboRenderer.class
Normal file
BIN
TestTableau/bin/modele/ComboRenderer.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
TestTableau/bin/modele/DeleteButtonEditor.class
Normal file
BIN
TestTableau/bin/modele/DeleteButtonEditor.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/Fenetre$MoreListener.class
Normal file
BIN
TestTableau/bin/modele/Fenetre$MoreListener.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/Fenetre.class
Normal file
BIN
TestTableau/bin/modele/Fenetre.class
Normal file
Binary file not shown.
BIN
TestTableau/bin/modele/ZModel.class
Normal file
BIN
TestTableau/bin/modele/ZModel.class
Normal file
Binary file not shown.
66
TestTableau/src/modele/ButtonEditor.java
Normal file
66
TestTableau/src/modele/ButtonEditor.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package modele;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.DefaultCellEditor;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
//CTRL + SHIFT + O pour g<>n<EFBFBD>rer les imports
|
||||
public class ButtonEditor extends DefaultCellEditor {
|
||||
|
||||
protected JButton button;
|
||||
private ButtonListener bListener = new ButtonListener();
|
||||
|
||||
public ButtonEditor(JCheckBox checkBox) {
|
||||
//Par d<>faut, ce type d'objet travaille avec un JCheckBox
|
||||
super(checkBox);
|
||||
//On cr<63>e <20> nouveau notre bouton
|
||||
button = new JButton();
|
||||
button.setOpaque(true);
|
||||
//On lui attribue un listener
|
||||
button.addActionListener(bListener);
|
||||
}
|
||||
|
||||
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
|
||||
//On d<>finit le num<75>ro de ligne <20> notre listener
|
||||
bListener.setRow(row);
|
||||
//Idem pour le num<75>ro de colonne
|
||||
bListener.setColumn(column);
|
||||
//On passe aussi en param<61>tre le tableau pour des actions potentielles
|
||||
bListener.setTable(table);
|
||||
//On r<>affecte le libell<6C> au bouton
|
||||
button.setText( (value ==null) ? "" : value.toString() );
|
||||
//On renvoie le bouton
|
||||
return button;
|
||||
}
|
||||
|
||||
class ButtonListener implements ActionListener {
|
||||
|
||||
private int column, row;
|
||||
private JTable table;
|
||||
private int nbre = 0;
|
||||
private JButton button;
|
||||
|
||||
public void setColumn(int col){this.column = col;}
|
||||
public void setRow(int row){this.row = row;}
|
||||
public void setTable(JTable table){this.table = table;}
|
||||
public JButton getButton(){return this.button;}
|
||||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
//On affiche un message mais vous pourriez faire ce que vous voulez
|
||||
System.out.println("coucou du bouton : "+
|
||||
((JButton)event.getSource()).getText() );
|
||||
//On affecte un nouveau libell<6C> <20> une celulle de la ligne
|
||||
((AbstractTableModel)table.getModel()).setValueAt("New Value " + (++nbre), this.row, (this.column -1));
|
||||
//Permet de dire <20> notre tableau qu'une valeur a chang<6E>
|
||||
//<2F> l'emplacement d<>termin<69> par les valeurs pass<73>es en param<61>tres
|
||||
((AbstractTableModel)table.getModel()).fireTableCellUpdated(this.row, this.column - 1);
|
||||
this.button = ((JButton)event.getSource());
|
||||
}
|
||||
}
|
||||
}
|
||||
18
TestTableau/src/modele/ButtonRenderer.java
Normal file
18
TestTableau/src/modele/ButtonRenderer.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package modele;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
|
||||
//CTRL + SHIFT + O pour g<>n<EFBFBD>rer les imports
|
||||
public class ButtonRenderer extends JButton implements TableCellRenderer{
|
||||
|
||||
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean isFocus, int row, int col) {
|
||||
//On <20>crit dans le bouton avec la valeur de la cellule
|
||||
setText((value != null) ? value.toString() : "");
|
||||
//On retourne notre bouton
|
||||
return this;
|
||||
}
|
||||
}
|
||||
19
TestTableau/src/modele/ComboRenderer.java
Normal file
19
TestTableau/src/modele/ComboRenderer.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package modele;
|
||||
|
||||
import java.awt.Component;
|
||||
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
|
||||
//CTRL + SHIFT + O pour g<>n<EFBFBD>rer les imports
|
||||
public class ComboRenderer extends JComboBox implements TableCellRenderer {
|
||||
|
||||
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean isFocus, int row, int col) {
|
||||
|
||||
this.addItem("Tr<EFBFBD>s bien");
|
||||
this.addItem("Bien");
|
||||
this.addItem("Mal");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
57
TestTableau/src/modele/DeleteButtonEditor.java
Normal file
57
TestTableau/src/modele/DeleteButtonEditor.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package modele;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.DefaultCellEditor;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JTable;
|
||||
|
||||
//CTRL + SHIFT + O pour g<>n<EFBFBD>rer les imports
|
||||
public class DeleteButtonEditor extends DefaultCellEditor {
|
||||
|
||||
protected JButton button;
|
||||
private DeleteButtonListener bListener = new DeleteButtonListener();
|
||||
|
||||
public DeleteButtonEditor(JCheckBox checkBox) {
|
||||
//Par d<>faut, ce type d'objet travaille avec un JCheckBox
|
||||
super(checkBox);
|
||||
//On cr<63>e <20> nouveau notre bouton
|
||||
button = new JButton();
|
||||
button.setOpaque(true);
|
||||
//On lui attribue un listener
|
||||
button.addActionListener(bListener);
|
||||
}
|
||||
|
||||
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
||||
//On d<>finit le num<75>ro de ligne <20> notre listener
|
||||
bListener.setRow(row);
|
||||
//On passe aussi en param<61>tre le tableau pour des actions potentielles
|
||||
bListener.setTable(table);
|
||||
//On r<>affecte le libell<6C> au bouton
|
||||
button.setText( (value ==null) ? "" : value.toString() );
|
||||
//On renvoie le bouton
|
||||
return button;
|
||||
}
|
||||
|
||||
class DeleteButtonListener implements ActionListener {
|
||||
|
||||
private int row;
|
||||
private JTable table;
|
||||
|
||||
public void setRow(int row){this.row = row;}
|
||||
public void setTable(JTable table){this.table = table;}
|
||||
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
if(table.getRowCount() > 0){
|
||||
//On affiche un message mais vous pourriez faire ce que vous voulez
|
||||
System.out.println("coucou du bouton : "+ ((JButton)event.getSource()).getText() );
|
||||
//On affecte un nouveau libell<6C> <20> une celulle de la ligne
|
||||
((ZModel)table.getModel()).removeRow(this.row);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
83
TestTableau/src/modele/Fenetre.java
Normal file
83
TestTableau/src/modele/Fenetre.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package modele;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.DefaultCellEditor;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.table.DefaultTableCellRenderer;
|
||||
|
||||
//CTRL + SHIFT + O pour g<>n<EFBFBD>rer les imports
|
||||
public class Fenetre extends JFrame {
|
||||
|
||||
private JTable tableau;
|
||||
private JButton change = new JButton("Changer la taille");
|
||||
private String[] comboData = {"Tr<EFBFBD>s bien", "Bien", "Mal"};
|
||||
private String supp = "Supprimer la ligne";
|
||||
private JComboBox combo;
|
||||
|
||||
public Fenetre(){
|
||||
this.setLocationRelativeTo(null);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
this.setTitle("JTable");
|
||||
this.setSize(600, 250);
|
||||
this.createContent();
|
||||
}
|
||||
|
||||
private void createContent(){
|
||||
//Donn<6E>es de notre tableau
|
||||
Object[][] data = {
|
||||
{"Cysboy", "6boy", comboData[0], new Boolean(true), supp},
|
||||
{"BZHHydde", "BZH", comboData[0], new Boolean(false), supp},
|
||||
{"IamBow", "BoW", comboData[0], new Boolean(false), supp},
|
||||
{"FunMan", "Year", comboData[0], new Boolean(true), supp}
|
||||
};
|
||||
|
||||
String title[] = {"Pseudo", "Age", "Taille", "OK ?", "Suppression"};
|
||||
combo = new JComboBox(comboData);
|
||||
|
||||
//Notre mod<6F>le d'affichage sp<73>cifique destin<69> <20> pallier
|
||||
//les bugs d'affichage !
|
||||
ZModel zModel = new ZModel(data, title);
|
||||
|
||||
this.tableau = new JTable(zModel);
|
||||
this.tableau.setRowHeight(30);
|
||||
this.tableau.getColumn("Age").setCellRenderer(new ButtonRenderer());
|
||||
this.tableau.getColumn("Age").setCellEditor(new ButtonEditor(new JCheckBox()));
|
||||
|
||||
//On d<>finit l'<27>diteur par d<>faut pour la cellule
|
||||
//en lui sp<73>cifiant quel type d'affichage prendre en compte
|
||||
this.tableau.getColumn("Taille").setCellEditor(new DefaultCellEditor(combo));
|
||||
DefaultTableCellRenderer dcr = new DefaultTableCellRenderer();
|
||||
this.tableau.getColumn("Taille").setCellRenderer(dcr);
|
||||
|
||||
//On d<>finit un <20>diteur pour la colonne "supprimer"
|
||||
this.tableau.getColumn("Suppression").setCellEditor(new DeleteButtonEditor(new JCheckBox()));
|
||||
|
||||
//On ajoute le tableau
|
||||
this.getContentPane().add(new JScrollPane(tableau), BorderLayout.CENTER);
|
||||
|
||||
JButton ajouter = new JButton("Ajouter une ligne");
|
||||
ajouter.addActionListener(new MoreListener());
|
||||
this.getContentPane().add(ajouter, BorderLayout.SOUTH);
|
||||
}
|
||||
|
||||
class MoreListener implements ActionListener{
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
Object[] donnee = new Object[]
|
||||
{"Angelo", "Rennais", comboData[0], new Boolean(false), supp};
|
||||
((ZModel)tableau.getModel()).addRow(donnee);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
Fenetre fen = new Fenetre();
|
||||
fen.setVisible(true);
|
||||
}
|
||||
}
|
||||
96
TestTableau/src/modele/ZModel.java
Normal file
96
TestTableau/src/modele/ZModel.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package modele;
|
||||
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
class ZModel extends AbstractTableModel{
|
||||
private Object[][] data;
|
||||
private String[] title;
|
||||
|
||||
//Constructeur
|
||||
public ZModel(Object[][] data, String[] title){
|
||||
this.data = data;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
//Retourne le titre de la colonne <20> l'indice sp<73>cifi<66>
|
||||
public String getColumnName(int col) {
|
||||
return this.title[col];
|
||||
}
|
||||
|
||||
//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 la valeur <20> l'emplacement sp<73>cifi<66>
|
||||
public Object getValueAt(int row, int col) {
|
||||
return this.data[row][col];
|
||||
}
|
||||
|
||||
//D<>finit la valeur <20> l'emplacement sp<73>cifi<66>
|
||||
public void setValueAt(Object value, int row, int col) {
|
||||
//On interdit la modification sur certaines colonnes !
|
||||
if(!this.getColumnName(col).equals("Age")
|
||||
&& !this.getColumnName(col).equals("Suppression"))
|
||||
this.data[row][col] = value;
|
||||
}
|
||||
|
||||
//Retourne la classe de la donn<6E>e de la colonne
|
||||
public Class getColumnClass(int col){
|
||||
//On retourne le type de la cellule <20> la colonne demand<6E>e
|
||||
//On se moque de la ligne puisque les donn<6E>es sont les m<>mes
|
||||
//On choisit donc la premi<6D>re ligne
|
||||
return this.data[0][col].getClass();
|
||||
}
|
||||
|
||||
//M<>thode permettant de retirer une ligne du tableau
|
||||
public void removeRow(int position){
|
||||
|
||||
int indice = 0, indice2 = 0;
|
||||
int nbRow = this.getRowCount()-1;
|
||||
int nbCol = this.getColumnCount();
|
||||
Object temp[][] = new Object[nbRow][nbCol];
|
||||
|
||||
for(Object[] value : this.data){
|
||||
if(indice != position){
|
||||
temp[indice2++] = value;
|
||||
}
|
||||
System.out.println("Indice = " + indice);
|
||||
indice++;
|
||||
}
|
||||
this.data = temp;
|
||||
temp = null;
|
||||
//Cette m<>thode permet d'avertir le tableau que les donn<6E>es
|
||||
//ont <20>t<EFBFBD> modifi<66>es, ce qui permet une mise <20> jour compl<70>te du tableau
|
||||
this.fireTableDataChanged();
|
||||
}
|
||||
|
||||
//Permet d'ajouter une ligne dans le tableau
|
||||
public void addRow(Object[] data){
|
||||
int indice = 0, nbRow = this.getRowCount(), nbCol = this.getColumnCount();
|
||||
|
||||
Object temp[][] = this.data;
|
||||
this.data = new Object[nbRow+1][nbCol];
|
||||
|
||||
for(Object[] value : temp)
|
||||
this.data[indice++] = value;
|
||||
|
||||
|
||||
this.data[indice] = data;
|
||||
temp = null;
|
||||
//Cette m<>thode permet d'avertir le tableau que les donn<6E>es
|
||||
//ont <20>t<EFBFBD> modifi<66>es, ce qui permet une mise <20> jour compl<70>te du tableau
|
||||
this.fireTableDataChanged();
|
||||
}
|
||||
|
||||
|
||||
public boolean isCellEditable(int row, int col){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user