使用JTable创建Java表

Java provides a useful class called JTable that enables you to create tables when developing graphical user interfaces using the components of Java's Swing API. You can enable your users to edit the data or just view it. Note that the table doesn't actually contain data — it's entirely a display mechanism.

Java提供了一个称为JTable的有用类,该类使您可以在使用Java的Swing API组件开发图形用户界面时创建表。 您可以使用户能够编辑数据或仅查看数据。 请注意,该表实际上并不包含数据,它完全是一种显示机制。

This step-by-step guide will show how to use the class

本分步指南将显示如何使用该课程

to create a simple table.

创建一个简单的表。

Note: Like any Swing GUI, you'll need to make a container in which to display the 

注意:与任何Swing GUI一样,您需要创建一个容器以在其中显示

. If you're unsure how to do this then look at

。 如果不确定如何执行此操作,请查看

.

使用数组存储表数据 ( Using Arrays to Store the Table Data )

A simple way to provide data for the

为数据提供数据的一种简单方法

class is to use two arrays. The first holds the column names in a

类是使用两个数组。 第一个将列名称保存在

array:

数组:

The second array is a two-dimensional object array that holds the data for the table. This array, for example, includes six Olympic swimmers:

第二个数组是保存表数据的二维对象数组。 例如,此阵列包括六名奥运会游泳运动员:

The key here is to make sure the two arrays have the same number of columns.

这里的关键是确保两个数组具有相同的列数。

构造JTable ( Constructing the JTable )

Once you have the data in place, it's a simple task to create the table. Just call the

放置好数据后,创建表是一个简单的任务。 只需致电


JTable
constructor and pass it the two arrays:You will probably want to add scroll bars to ensure the user can see all the da 构造函数并将其传递给两个数组:您可能需要添加滚动条以确保用户可以看到所有

JTable into a 
JScrollPane:Now when the table is displayed, you will see the columns and rows of data and will have the capability

The JTable object provides an interactive table. If you double-click on any of the cells, you will be able to edit the contents — although any editing affects only the GUI, not the underlying data. (An event listener would need to be implemented to handle the changing of data.).

JTable对象提供了一个交互式表。 如果双击任何一个单元格,您将能够编辑内容-尽管任何编辑都只会影响GUI,而不会影响基础数据。 (将需要实现一个事件侦听器来处理数据更改。)

To change the widths of the columns, hover the mouse on the edge of a column header and drag it back and forth. To change the order of the columns, click and hold a column header, then drag it to the new position.

要更改列的宽度,请将鼠标悬停在列标题的边缘,然后前后拖动它。 要更改列的顺序,请单击并按住列标题,然后将其拖动到新位置。

排序栏 ( Sorting Columns )

To add the ability to sort the rows, call the

要添加对行进行排序的功能,请调用


setAutoCreateRowSorter method:When this method is set to true, you can click on a column header to sort the rows according to the contents of the c

更改表格的外观 ( Changing the Appearance of the Table )

To control the visibility of the grid lines, use the

要控制网格线的可见性,请使用


setShowGrid method:To change the color of the ta


setBackground and 
setGridColor methods:The column widths of the table are equal by default. If the container the table is in is re-sizeable, then the widths of the columns will expand and shrink and the container grows bigger or smaller. If a user resizes the column, then the width of columns to the right will change to accommoda

The initial column widths can be set using the setPreferredWidth method or a column. Use the TableColumn class to first get a reference to the column, and then the setPreferredWidth method to set the size:

可以使用setPreferredWidth方法或列来设置初始列宽。 使用TableColumn类首先获取对该列的引用,然后使用setPreferredWidth方法设置大小:

选择行 ( Selecting Rows )

By default, the user can select the rows of the table in one of three ways:

默认情况下,用户可以通过以下三种方式之一选择表的行:

  • To select a single row, select a table cell in that row.

    要选择单行,请在该行中选择一个表格单元格。
  • To select continuous, multiple rows, drag the mouse over several rows or select the table cells with the shift cell pressed.

    要选择连续的多行,请将鼠标拖到几行上,或者在按住shift键的情况下选择表格单元格。
  • To select non-continuous, multiple rows, select table cells while holding down the control key (command key for Macs).

    要选择不连续的多行,请在按住Control键 (Mac机为Command键 )的同时选择表格单元格。

使用表格模型 ( Using a Table Model )

Using a couple of arrays for the data of a table can be useful if you want a simple String-based table which can be edited. If you look at the data array we created, it contains other data types than

如果您想要一个可以编辑的基于String的简单表,则对表的数据使用几个数组会很有用。 如果您查看我们创建的数据数组,它包含的数据类型除了

- the

-的

column contains

列包含

and the

column contains

列包含

. Yet both these columns are displayed as Strings. To change this behavior, create a table model.

。 但是,这两列都显示为字符串。 若要更改此行为,请创建一个表模型。

A table model manages the data to be displayed in the table. To implement a table model, you can create a class that extends the

表格模型管理要在表格中显示的数据。 要实现表格模型,您可以创建一个扩展

class:

类:

The six methods above are those used in this step-by-step guide, but there are more methods defined by the

上面的六种方法是本分步指南中使用的方法,但是本指南定义了更多方法

class that are useful in manipulating the data in a

在处理数据时非常有用的类

object. When extending a class to use the

目的。 在扩展类以使用

you are required to implement only the

您只需要实施

,

and

methods.

方法。

Create a new class implementing those five methods shown above:

创建一个新类,实现上述五个方法:

It makes sense in this example for the

在此示例中,对于

class to hold the two strings containing the table data. Then, the

类,用于保存包含表数据的两个字符串。 然后,

,

and

methods can use the arrays to provide the values for the table. Also, notice how the

方法可以使用数组来提供表的值。 另外,请注意

method has been written to disallow the first two columns to be edited.

方法已编写为不允许编辑前两列。

Now, instead of using the two arrays to create the

现在,不要使用两个数组来创建

object, we can use the

对象,我们可以使用

class:

类:

When the code runs, you will see that the

代码运行时,您将看到

object is using the table model because none of the table cells are editable, and the column names are being correctly used. If the

对象正在使用表模型,因为所有表单元格都不是可编辑的,并且列名已正确使用。 如果

method had not been implemented, then the column names on the table would display as the default names of A, B, C, D, etc.

方法尚未实现,则表上的列名称将显示为A,B,C,D等的默认名称。

Let's now consider the method 

现在让我们考虑一下方法

. This alone makes the table model worth the implementation because it provides the

。 单靠表模型值得实现,因为它提供了

object with the data type contained within each column. If you remember, the object data array has two columns that aren't

每个列中包含数据类型的对象。 如果您还记得的话,对象数据数组有两列

data types: the

数据类型:

column which contains ints, and the

包含整数的列,以及

column which contains

包含

. Knowing these data types changes the functionality provided by the

。 知道这些数据类型会改变

object for those columns. Running the sample table code with the table model implemented means the

这些列的对象。 在实现表模型的情况下运行示例表代码意味着

column will actually be a series of checkboxes.

列实际上是一系列复选框。

添加组合框编辑器 ( Adding a ComboBox Editor )

You can define custom editors for the cells in the table. For example, you could make a combo box an alternative to the standard text editing for a field.

您可以为表中的单元格定义自定义编辑器。 例如,您可以使组合框替代字段的标准文本编辑。

Here's an example using 

这是一个使用示例

the country field:

国家字段:

To set the default editor for the country column, use the

要为“国家/地区”列设置默认编辑器,请使用

class to get a reference to the country column, and the

类以获取对“国家/地区”列的引用,

method to set the

设置方法

as the cell editor:

作为单元格编辑器:

翻译自: https://www.thoughtco.com/how-to-create-a-simple-table-2033894

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值