0

"I cannot change my icon to another icon using mouse click listener.Problem is that as i double click that icon it shows me the path of the icon and to change that icon to another icon i have to click another cell and click back to that cell and then its icon value changes to next icon.`

    private void studentMouseClicked(java.awt.event.MouseEvent evt) {                                     
        // TODO add your handling code here:
        //((JLabel)cell).addMouseListener(null;



        int row = student.getSelectedRow();

        int column  =student.getSelectedColumn();

         Object value = student.getValueAt(row, column);
         JLabel jl1;
        ImageIcon icon1 = new ImageIcon(getClass().getResource("dash.png"));
        if(value.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/dash.png")){

        //   "dash.png

            ImageIcon icon = new ImageIcon(getClass().getResource("1425438648_tick-button.png"));
            student.setValueAt(icon, row, column);
            student.getColumnModel().getColumn(column).setCellRenderer(new UpdateCellRenderer());
            ((AbstractTableModel)student.getModel()).fireTableDataChanged();
        }
        else if(value.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/1425438716_cross-button.png")){
           // "1425438716_cross-button.png"
            ImageIcon icon = new ImageIcon(getClass().getResource("dash.png"));
            student.setValueAt(icon, row, column);
            student.getColumnModel().getColumn(column).setCellRenderer(new UpdateCellRenderer());
            ((AbstractTableModel)student.getModel()).fireTableDataChanged();
        }

       else if(value.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/1425438648_tick-button.png")){

            ImageIcon icon = new ImageIcon(getClass().getResource("1425438716_cross-button.png"));
            student.setValueAt(icon, row, column);
            student.getColumnModel().getColumn(column).setCellRenderer(new UpdateCellRenderer());
            ((AbstractTableModel)student.getModel()).fireTableDataChanged();

        }
    }                            

package attendance;

/**
 *
 * @author Admin
 */

import java.awt.Color;
import java.awt.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.Icon;
import javax.swing.ImageIcon;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
public class UpdateCellRenderer extends DefaultTableCellRenderer{

    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement pst = null;
        @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) 
    {

         Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
               column);
      ((JLabel)cell).setIcon((Icon)value);
      ((JLabel)cell).setText("");
      ((JLabel)cell).setHorizontalAlignment(JLabel.CENTER);

      if (isSelected) {

         cell.setBackground(Color.white);
      } else {
         cell.setBackground(null);
      }
      //mysql query has to be pasted here.here change the value of particular row, columnin database.column will get us the day and row will get us the roll number. 
     /*        String value1 = (String)value;

            if(value1.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/1425438648_tick-button.png"))
    {

        Object name = table.getTableHeader().getColumnModel().getColumn(column).getHeaderValue();
         //below line will help me to get particular column.
         String namesql = (String)name;
        try{
            String sql = "update class_second set "+namesql+" = p where  roll_number = "+ (row+1);
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "akshay");
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();


        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

            else if(value1.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/dash.png"))
    {

        Object name = table.getTableHeader().getColumnModel().getColumn(column).getHeaderValue();
         //below line will help me to get particular column.
         String namesql = (String)name;
        try{
            String sql = "update class_second set "+namesql+"=h where  roll_number ="+ (row+1);
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "akshay");
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();


        }catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

                   else if(value1.equals("file:/C:/Users/Admin/Documents/NetBeansProjects/attendance/build/classes/attendance/1425438716_cross-button.png"))
    {

        Object name = table.getTableHeader().getColumnModel().getColumn(column).getHeaderValue();
         //below line will help me to get particular column.
         String namesql = (String)name;
        try{
            String sql = "update class_second set "+namesql+" = a where  roll_number ="+ (row+1);
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "akshay");
            pst = conn.prepareStatement(sql);
            pst.executeUpdate();


        }catch(Exception e)
        {
                ` JOptionPane.showMessageDialog(null, e);`
        }
    }
      */

        return cell;
}
}`
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Mar 18 '15 at 10:45
  • plz help if any further info is required ping it here! – Gandalf the white Mar 18 '15 at 11:09
  • *"if any further info is required"* You need further information on following the link to the MCVE?!? – Andrew Thompson Mar 18 '15 at 11:24
  • Why are you continually changing the renderer of the column? You should set the renderer when you create the table. Don't invoke fireTableChanged(...). It is the job of the TableModel to invoke that method, not your application code. – camickr Mar 18 '15 at 14:59
  • but i need to change the icon to another one as i click on the cell. – Gandalf the white Mar 18 '15 at 15:04
  • bro i really dunno how to edit that cell with another image.If any one can help please post its really very urgent. – Gandalf the white Mar 19 '15 at 14:02

0 Answers0