devxlogo

Adding a JButton to the Cells of a Jtable

Adding a JButton to the Cells of a Jtable

sing the Swing JTable class can quickly become a stickybusiness when you want to customize it to your specific needs. First you must become familiar with how the JTable class is organized. Individual cells are rendered by TableCellRenderer implementations. The table contents are represented by an implementation of the TableModel interface. By default, JTable uses DefaultTableCellRenderer to draw its cells. DefaultTableCellRenderer recognizes a few primitive types, rendering them as strings, and can even display Boolean types as checkboxes. But it defaults to displaying the value returned by toString() for types it does not specifically handle.

You have to provide your own TableCellRenderer implementation if you want to display buttons in a JTable. The TableCellRenderer interface contains only one method, getTableCellRendererComponent(…), which returns a java.awt.Component that knows how to draw the contents of a specific cell. Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid theunnecessary use of extra memory. But when the contents of a cell is itself a component, it is all right to return that component as the renderer. Therefore, the first step towards having JButtons display correctly in a JTable is to create a TableCellRenderer implementation that returns the JButton contained in the cell being rendered. In the accompanying code listing, JTableButtonRenderer demonstrates how to do this.

Even after creating a custom TableCellRenderer, you’re still not done. The TableModel associated with a given JTable does not only keep track of the contents of each cell, but it also keeps track of the class of data stored in each column. DefaultTableModel is designed to work with DefaultTableCellRenderer and will return java.lang.String.class for columns containing data types that it does not specifically handle. The exact method that does this isgetColumnClass(int column). Your second step is to create a TableModel implementation that returns JButton.class for cells that contain JButtons. JTableButtonModel shows one way to do this. It just returns the result of getClass() for each piece of cell data.

At this point, you’re almost done, but not quite. What’s the use of putting a JButton in a JTable if you can’t press the darn thing?By default, JTable will not forward mouse events to componentscontained in its cells. If you want to be able to press the buttons you add to JTable, you have to create your own MouseListener that forwards events to the JButton cells. JTableButtonMouseListener demonstrates how you could do this.

Yep, you’re done now. The code listing shows how to tie all these pieces together.

import com.sun.java.swing.*;import com.sun.java.swing.table.*;import java.awt.*;import java.awt.event.*;class JTableButtonRenderer implements TableCellRenderer {  private TableCellRenderer __defaultRenderer;  public JTableButtonRenderer(TableCellRenderer renderer) {    __defaultRenderer = renderer;  }  public Component getTableCellRendererComponent(JTable table, Object value,						 boolean isSelected,						 boolean hasFocus,						 int row, int column)  {    if(value instanceof Component)      return (Component)value;    return __defaultRenderer.getTableCellRendererComponent(	   table, value, isSelected, hasFocus, row, column);  }}class JTableButtonModel extends AbstractTableModel {  private Object[][] __rows = {      { "One", new JButton("Button One") },      { "Two", new JButton("Button Two") },      { "Three", new JButton("Button Three") },      { "Four", new JButton("Button Four") }    };  private String[] __columns = { "Numbers", "Buttons" };  public String getColumnName(int column) {     return __columns[column];  }  public int getRowCount() {    return __rows.length;  }  public int getColumnCount() {    return __columns.length;  }  public Object getValueAt(int row, int column) {       return __rows[row][column];  }  public boolean isCellEditable(int row, int column) {    return false;  }  public Class getColumnClass(int column) {    return getValueAt(0, column).getClass();  }}class JTableButtonMouseListener implements MouseListener {  private JTable __table;  private void __forwardEventToButton(MouseEvent e) {    TableColumnModel columnModel = __table.getColumnModel();    int column = columnModel.getColumnIndexAtX(e.getX());    int row    = e.getY() / __table.getRowHeight();    Object value;    JButton button;    MouseEvent buttonEvent;    if(row >= __table.getRowCount() || row = __table.getColumnCount() || column 
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist