How to add buttons inside a cell of jtable and give it action

17,518

You need both a renderer and and editor, as shown in this example. See How to Use Tables: Editors and Renderers for details. Tangentially, you should override the method isCellEditable() in your TableModel rather than extending JTable.

Share:
17,518
Ogre3dUser
Author by

Ogre3dUser

Updated on June 04, 2022

Comments

  • Ogre3dUser
    Ogre3dUser almost 2 years

    i know this has been answered before but considering im still a newbie i cant figure out a way to give actions and block the cell editing, i have tried it in several ways,i can successfully render the buttons however when i click on it, it edits the cell instead of pressing the button, i know that in order to avoid the cell editing i should create an abstract table and overide this method:

    public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 3) {
                return false;
            } else {
                return true;
            }
        }
    

    however im using a default JTable,so this is the code i am using for the Panel and cell renderer:

    class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
    
        public Component getTableCellRendererComponent(
                            final JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {
    
    
                if(column < 3)
                {
                 JLabel campo =   new JLabel(value.toString());
    
    
                this.add(campo);
    
                }
                if(column > 2)
                {
    
                     //this is a button
                     this.add(botaoteste);
    
    
    
    
    
                materialtable.revalidate();
                materialtable.repaint();
    
    
                }
                return this;
    
        }
    

    and this is the code i am using to retrieve data from sql to the Jtable(I customised the DefaultJTable code)

    String[] columnNames={"teste","abc","def"};                 
         Object[][] data = new Object[1][4];
                if(createConnection())
                {
                    try {
                        Statement statemt = conLogin.createStatement();
                        ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
                        //ResultSet rs = statemt.executeQuery("SELECT * FROM Materiais");
                        rsmtdata = rs.getMetaData();
    
                        //int columns = rsmtdata.getColumnCount();
                         columnNames = new String[]{rsmtdata.getColumnName(1),rsmtdata.getColumnName(2),rsmtdata.getColumnName(3),"Deletar"};
                         if(rs.next())
                         {
                            data[0][0] = rs.getString(1);
                            data[0][1] = rs.getString(2);
                            data[0][2] = rs.getString(3);
                            data[0][3] = new Boolean(false);
                         }
                         while (rs.next())
                         {
    
                            Object[][] temp = new Object[data.length+1][4];
                            for(int i=0;i < data.length;i++)
                            {
                                for(int j = 0;j < 4;j++)
                                {
                                    temp[i][j] = data[i][j];
                                }
    
                            }
                            temp[data.length][0] = rs.getString(1);
                            temp[data.length][1] = rs.getString(2);
                            temp[data.length][2] = rs.getString(3);
                            temp[data.length][3] = new Boolean(false);
                            data = temp;
    
    
    
                         }
    materialtable = new javax.swing.JTable(data, columnNames);
    materialtable = new javax.swing.JTable(data, columnNames){ 
                public TableCellRenderer getCellRenderer( int row, int column ) {
                    return new PlusMinusCellRenderer();
                }
             };
    
    materialtable.setRowHeight( 32 );
    
                    } catch (SQLException ex) {
                        Logger.getLogger(ProfessorForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
    
                     }
    
    //Create the scroll pane and add the table to it.
    materialtable.setBackground(new java.awt.Color(153, 255, 51));
    
    materialtable.setSelectionBackground(new java.awt.Color(255, 255, 51));
    
    materialtable.setSelectionForeground(new java.awt.Color(255, 102, 0));
    
    
    
    jScrollPane3.setViewportView(materialtable);
    

    so to render the button inside the table i based on this thread: Adding Buttons inside cell of JTable along with data?

    my question is very straight forward, how can i disable Row editing(just like the isCellEditable() method usage) and give action to the buttons? any help here is greatly appreciated and please take it to consideration im still novice so please detailed information or samples is needed ! Kind regards, Romulo Romero