如何在JTable的行中添加按钮?

问题描述:

我有一个包含JTable的基于Swing的应用程序.现在,我想使用唯一的行ID来更新或删除每一行.因此,我想向每行添加一个更新和删除按钮,它们具有支持ActionListener的功能.但是,我不知道如何使用NetBeans做到这一点.

I have a Swing based application containing a JTable. Now I would like to allow each row to be updated or deleted, using a unique row ID. So I want to add an update and delete button to each row, which have the capability to support an ActionListener. However, I have no idea how to do this using NetBeans.

要在列中显示按钮,您需要创建:

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

  1. 用于显示JButton的自定义渲染器
  2. 用于响应鼠标单击的自定义编辑器

如何使用表中阅读Swing教程中的部分.关于:

  1. Using Custom Renders将解释使用渲染器的基本知识
  2. Using Other Editors将解释使用编辑器的基本知识
  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.