1

I'm adding buttons dynamically. when i click that button, action should perform and i need values of which button got clicked.

import java.awt.Component;
import java.awt.Font;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomCell {
    public static void main( String [] args ) { 
        Object [] columnNames = new Object[]{ "Id", "Quantity" };
        Object [][] data        = new Object[][]{ {"06", 1}, {"08", 2} };

        JTable table = new JTable( data, columnNames ) { 
            public TableCellRenderer getCellRenderer( int row, int column ) {
                return new PlusMinusCellRenderer();
            }
         };

        table.setRowHeight( 32 );
        showFrame( table );
    }

    private static void showFrame( JTable table ) {
        JFrame f = new JFrame("Custom Cell Renderer sample" );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.add( new JScrollPane( table ) );
        f.pack();
        f.setVisible( true );
    }
}

class PlusMinusCellRenderer extends JPanel implements TableCellRenderer {
        public Component getTableCellRendererComponent(
                            final JTable table, Object value,
                            boolean isSelected, boolean hasFocus,
                            int row, int column) {

                JButton jb= new JButton("Edit");
                this.add(jb);
                jb.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        System.out.println("Clicked !");
                    }
                });

                return this;
        }


}

in above action is not at all calling.Can some body help me on 2 things. 1) actionPerformed method should be called.. 2) And i need to get the values of clicked row.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1742919
  • 401
  • 4
  • 11

1 Answers1

2

You'll notice that not only is the ActionListener not being called, but the button never displays itself being pushed. The reason for this is that a JTable cell renderer only displays an image and nothing more, is not a Swing component, it cannot display a pushable JButton, and so there is no way to solve your problem by use of the cell renderer. A cell editor on the other hand can, and you may wish to go that route.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks for ur reply.http://stackoverflow.com/questions/2069641/adding-buttons-inside-cell-of-jtable-along-with-data...look at firsr ans..that code is working and able to give output..but i need to customise it..and am failing..btw in that case ur obsevation seems to be wrong to me – user1742919 Jul 07 '15 at 16:32
  • 1
    @user1742919: you read part of the answer to your link. It states that you have to use a cell renderer **and cell editor** just as I mention above. You're ignoring the second part, the use of a cell editor that uses the JButton. – Hovercraft Full Of Eels Jul 07 '15 at 16:35
  • oh..sorry ..yes..btw where we get all these conceptual info..any good link?? – user1742919 Jul 07 '15 at 16:38