javaswingjtablejbuttontablecelleditor

JTable with JButton as cell editor


I used a JButton as render and editor in JTable, with following problems:

  1. The JTable row is not selected when JButton clicked.
  2. When the JButton is clicked, how to get its location within JTable, in other words its columnIndex and rowIndex

Any help will be appreciated!


Solution

  • To display a button in a column you need to create:

    1. a custom renderer to display the JButton
    2. a custom editor to respond to the mouse click

    Read the section from the Swing tutorial on How to Use Tables. The section on:

    1. Using Custom Renders will explain the basics of using a renderer
    2. Using Other Editors will explain the basics of using an editor

    Working example are provided in the tutorial that you can download.

    You can check out Table Button Column for one approach.

    The code uses a single class to implement the custom renderer and editor that you will need for the column to display your text as a button.