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

Recommended Answers

All 9 Replies

OK, what eaxctly is your question?

commented: can you pleas look ate my code and tell me were did i go rong +0

That's a lot of code to look through with no idea what you are looking for.
Please explain exactly what is wrong - do you get an error message or exception? What happens when you try to run that code?
The more info you give us the easier it is to help

commented: i am trying to pute a imag in a cell in the same row after cliking the button in the column "Age" +0

Yes, you said that already.
Please explain exactly what is wrong - do you get an error message or exception? What happens when you try to run that code?
The more info you give us the easier it is to help

commented: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at t.Buttone$ButtonListener.actionPerformed(Buttone.java:107) at javax.swing.A +0

any way pleas help this is driving me crazy

Why didn't you post that error message with your first post?

You have a null pointer (unitialised variable) in your button handler. To diagnose it further we need to know which variable that was.
The line number (107) does not correspond to the numbering of the code as you posted it, so exactly which line is that?

commented: not shure still pretty new to java plus the code above is not the full version +0

her is the full code of the buttone

package t;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.DefaultCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;



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(image.getAbsolutePath());

                            table.getModel().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, 5);


          this.button = ((JButton)event.getSource());


         }

      }






}

Unless you can tell me the eaxct line where the null pointer exception happened I can't help.

commented: line 108 table.getModel().setValueAt(icon, this.row, 5); +0

the variable table is declared in the listener class but never initialised, so it's null.

commented: ther is a nother class that fil the table or i am rong can you pleas point a way +0

You need to delete the local table variable and make sure the real table variable is accessible in the ButtonListener class

commented: fixded thanks for the pointer +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.