so i am working on this table that have buttons combobox and checkbox...
i did all of that and i am left whit one this i button that can add a icon in IMAGE column in the same row
her is my main
private JTable tb;
private String[] comboData = {"Très bien", "Bien", "Mal"};
private String[] comboData2 = {};
private JComboBox combo;
private Boolean t=false ;
private JPanel panel;
public Demobutton(){
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("JTable");
setSize(600, 250);
content();
}
public void content()
{
Object[][] data = {
{"ajout","Cysboy", "6boy", comboData[0],t, new ImageIcon("C:/Users/ayoub/Desktop")}
};
String header[] = {"ajout","Pseudo", "Age", "Taille","OK ?","image"};
combo = new JComboBox(comboData);
Modeldemo model=new Modeldemo(data,header);
this.tb = new JTable(model);
this.tb.setRowHeight(30);
this.tb.getColumn("Age").setCellRenderer(new Buttonr());
this.tb.getColumn("Age").setCellEditor(new Buttone(new JCheckBox()));
this.tb.getColumn("ajout").setCellRenderer(new Buttonr2());
this.tb.getColumn("ajout").setCellEditor(new Buttone2(new JCheckBox()));
this.tb.getColumn("Taille").setCellEditor(new DefaultCellEditor(combo));
// DefaultTableCellRenderer dcr = new DefaultTableCellRenderer();
//this.tb.getColumn("Taille").setCellRenderer(dcr);
this.getContentPane().add(new JScrollPane(tb), BorderLayout.CENTER);
}
her is the model
package t;
import javax.swing.ImageIcon;
import javax.swing.table.AbstractTableModel;
public class Modeldemo extends AbstractTableModel{
private Object[][] data;
private String[] title;
//Constructeur
public Modeldemo(Object[][] data, String[] title){
this.data = data;
this.title = title;
}
public String getColumnName(int col) {
return this.title[col];
}
public int getColumnCount() {
return this.title.length;
}
public int getRowCount() {
return this.data.length;
}
public Object getValueAt(int row, int column) {
return this.data[row][column];
}
public Class getColumnClass(int col){
if (col==5) return ImageIcon.class;
return this.data[0][col].getClass();
}
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("ajout"))
this.data[row][col] = value;
}
public boolean isCellEditable(int row, int col){
return true;
}
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ées
//ont été modifiées, ce qui permet une mise à jour complète du tableau
this.fireTableDataChanged();
}
public void addphoto()
{
}
}
and her is the button rendere
public class Buttone extends DefaultCellEditor{
JLabel label;
ImageIcon photo;
WritableRaster raster;
DataBufferByte data;
File image;
String s;
JPanel jPanel2 ;
protected JButton button;
private ButtonListener bListener = new ButtonListener();
private static BufferedImage resizeImage(BufferedImage originalImage, int type) {
BufferedImage resizedImage = new BufferedImage(435, 435, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 435, 435, null);
g.dispose();
return resizedImage;
}
public Image toImage(BufferedImage bufferedImage) {
return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}
public Buttone(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(bListener);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column){
/*
bListener.setRow(row);
bListener.setColumn(column);
bListener.setTable(table);
*/
button.setText( (value ==null) ? "" : value.toString() );
return button;
}
class ButtonListener implements ActionListener {
private int column, row;
private JTable table;
private Boolean nbre;
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(java.awt.event.ActionEvent event) {
JFileChooser chooser= new JFileChooser();
FileNameExtensionFilter filter= new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.setCurrentDirectory(image);
chooser.addChoosableFileFilter(filter);
image = chooser.getSelectedFile();
int i = chooser.showOpenDialog(button);
if (i == JFileChooser.APPROVE_OPTION) {
image = chooser.getSelectedFile();
s=image.getAbsolutePath();
}
// ImageIcon image = new ImageIcon(s);
//SetIcon(image);
// table.getModel().setValueAt(image.getImageObserver(), this.row, 5);
// ((AbstractTableModel)table.getModel()).fireTableCellUpdated(this.row, 5);
ImageIcon icon = new ImageIcon(s);
table.getColumn("image").setCellRenderer(new mcr());
DefaultTableModel Tmodel=(DefaultTableModel) table.getModel();
table.setRowHeight(80);
Tmodel.setValueAt(icon, this.row, 5);
/*
System.out.println("coucou du bouton : "+
((JButton)event.getSource()).getText() +"what up");
if(table.getModel().getValueAt(this.row, 4).equals(false))
{table.getModel().setValueAt(nbre=true, this.row, 4);}
else
{table.getModel().setValueAt(nbre=false, this.row, 4);}
((AbstractTableModel)table.getModel()).fireTableCellUpdated(this.row, 4);
*/
this.button = ((JButton)event.getSource());
}
}
and finaly the image renere
class mcr extends DefaultTableCellRenderer{
JLabel lbl = new JLabel();
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
lbl.setIcon((ImageIcon)value);
return lbl;
}
}
thabnk you in advance