Java Basic Problems

JButton in JTable Cell: How to add, assign or fill up JTable’s Cell with JButton and then add ActionListener to enable Click event for that JButtons in JTable cell.

JButton in JTable cellSometimes while working with software using java programming language we may face problem while working with JTable. In JTable Cell you may need to show JButton for various purposes. So, to show JButton in JTable Cell or to fill up JTable’s Column with JButton you will need to use ButtonRenderer and ButtonEditor and to add Click Event on the JButton in JTable cell you will need to add ActionListener for that JButton.

Just Copy the code Below and run it and change it according to your need.


Code Example to assign JButton in JTable’s Cell and ActionListener

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.TableModel;

import javax.swing.table.DefaultTableModel;

import javax.swing.table.*;

class ButtonInJTable extends JFrame


private JPanel topPanel,topPanel1;

private JTable table;

private JScrollPane scrollPane,scrollPane1;

private String[] columnNames= new String[3];

private String[][] dataValues=new String[3][3] ;

JButton button = new JButton();

public ButtonInJTable()


setTitle(“Button in JTable Cell”);


topPanel= new JPanel();

topPanel.setLayout(new BorderLayout());


columnNames=new String[] {“Column 1” , “Column 2” , “Column 3”};

 dataValues = new String[][]              {





TableModel model=new myTableModel(“owntable”);

table =new JTable( );


table.getColumn(“Column 3”).setCellRenderer(new ButtonRenderer());

table.getColumn(“Column 3″).setCellEditor(new ButtonEditor(new JCheckBox()));

scrollPane=new JScrollPane(table);



new ActionListener()


public void actionPerformed(ActionEvent event)


JOptionPane.showMessageDialog(null,”Button Clicked in JTable Cell”);





class ButtonRenderer extends JButton implements TableCellRenderer {

public ButtonRenderer() {



public Component getTableCellRendererComponent(JTable table, Object value,

boolean isSelected, boolean hasFocus, int row, int column) {

setText((value == null) ? “” : value.toString());

return this;



class ButtonEditor extends DefaultCellEditor {

private String label;

public ButtonEditor(JCheckBox checkBox) {



public Component getTableCellEditorComponent(JTable table, Object value,

boolean isSelected, int row, int column) {

label = (value == null) ? “” : value.toString();


return button;


public Object getCellEditorValue() {

return new String(label);



public class myTableModel extends DefaultTableModel


String dat;

JButton button=new JButton(“”);

myTableModel(String tname)





public boolean isCellEditable(int row,int cols)


if( dat==”owntable”)


if(cols==0){return false;}


return true;



public static void main(String args[])


ButtonInJTable mainFrame=new ButtonInJTable();




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s