java - actionEvent of dynamically generated JButton not working -
i'm adding buttons dynamically. when click button, action should perform , need values of 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 not @ calling.can body me on 2 things. 1) actionperformed method should called.. 2) , need values of clicked row.
you'll notice not actionlistener not being called, button never displays being pushed. reason jtable cell renderer displays image , nothing more, not swing component, cannot display pushable jbutton, , there no way solve problem use of cell renderer. cell editor on other hand can, , may wish go route.
Comments
Post a Comment