## tof

more see,more think,more practice,more summary

# oreilly java swing : JTable 之JTable

### 15.1 The JTable Class

#### 15.1.1 Table Columns

With Swing tables, the basic unit is not an individual cell but a column. Most columns in real-world tables represent a certain type of information that is consistent for all records. For example, a record containing a person's name is aString and might be the first column of the table. For every other record (row), the first cell is always aString.The columns do not need to all have the same data type. The same record could hold not only a person's name, but whether or not they owned a computer. That column would holdBoolean values, notString values. The models supportingJTable reflect this view of the world.There is aTableModel that handles the contents of each cell in the table. You will also find aTableColumnModel that tracks the state of the columns in the table (how many columns, the total width, whether or not you can select columns, etc.).

JTable基本的单元并不是一个单独的单元格，而是一列.TableModel负责每个单元格的内容。TableColumnModel跟踪表格中每个列的状态，比如多少列，总的宽度，是否你能选择这列等等。

each column has one data type and one class responsible for drawing it

#### 15.1.2 Properties

we'll break the properties up into three smaller tables: one for row, column, and cell properties; one for selection properties;and one for visual properties.Table 15-1 covers the row, column, and cell properties.

我们将JTable的属性分三块:第一;对于行，列，单元格;第二;选择属性;第三;外观属性

##### Table 15-1. JTable row, column, and cell properties

Property

Data type

get

is

set

Default value

autoCreateColumnsFromModel

boolean

·

·

false

autoResizeMode

int

·

·

AUTO_RESIZE_ALL_COLUMNS

columnCount

int

·

0

columnModel

TableColumnModel

·

·

DefaultTableColumnModel( )

model

TableModel

·

·

DefaultTableModel( )

rowCount

int

·

0

rowHeight

int

·

·

16

The autoCreateColumnsFromModelproperty determines whether the column model is loaded automatically with data from the table model. This value is set totrue if you don't supply a non-null ColumnModel to the constructor. (You can always replace an existing column model with thesetColumnModel( ) method.)The autoResizeMode property determines how the table reacts to being resized. Using the constants presented inTable 15-4, you can adjust all of the columns, only the last column, or shut resizing off altogether. You must turn off autoresizing if you want to use the horizontal scrollbar in your scroll pane. ThecolumnCount and rowCount properties allow you to ask the JTable object how many rows and columns it has. These values come from the models in place.ThecolumnModel property holds the current column model and can be replaced at runtime, if necessary. TherowHeight property dictates how tall rows are, in pixels. This property must be a number greater than or equal to one. Other values cause thesetRowHeight( ) method to throw anIllegalArgumentException. The value ofrowHeight includes the vertical intercell spacing.

Table 15-2 lists the selection-related properties of theJTable class.The selectionModel property holds theListSelectionModel object that handles row selections, and theselectionMode property applies to that model. (You can control the column selections with theselectionModel property of theTableColumnModel for your table.) ThecellSelectionEnabled,columnSelectionAllowed, androwSelectionAllowed properties determine whether you can select cells, columns, or rows. If cells are selectable, only cells can be selected, regardless of the row and column properties. With cell selection turned on and row and column selection turned off, you can still select a range of cells. With an active selection on your table, theselectedColumn,selectedColumnCount,selectedColumns, selectedRow, selectedRowCount, and selectedRows give you access to the various parts of that selection. TheselectedRow andselectedColumn properties store the anchor selection (i.e., the first selection) from their respective selection models.

##### Table 15-2. JTable selection properties

Property

Data type

get

is

set

Default value

cellSelectionEnabled

boolean

·

·

false

columnSelectionAllowed

boolean

·

·

false

rowSelectionAllowed

boolean

·

·

true

selectedColumn

int

·

-1

selectedColumnCount

int

·

0

selectedColumns

int[]

·

int[0]

selectedRow

int

·

-1

selectedRowCount

int

·

0

selectedRows

int[]

·

int[0]

selectionMode

int

·

MULTIPLE_INTERVAL_SELECTION

selectionModel

ListSelectionModel

·

·

DefaultListSelectionModel

Table 15-3 covers the remaining properties of theJTable class.The cellEditor property determines the cell editor currently in use. When you start editing, theJTable class looks up your column and asks it for an editor. If the column has one, that editor is used; otherwise, a default editor for the column's class type is used. If no cell is currently being edited, this property isnull. If you want your table to support automatic drag initiation, set thedragEnabled property totrue. The gridColor,selectionBackground, andselectionForeground properties determine the color used for the grid lines and selection text.TheintercellSpacing property determines the horizontal and vertical spacing around each cell in the table. ThepreferredScrollableViewportSize property determines the preferred size of the scrollpane for the table. ThescrollableTracksViewportHeightand scrollableTracksViewportWidth properties are alwaysfalse, which indicates that making the viewport around the table should not resize the table to fit the viewport (assuming you have placed the table in a scrollpane). You can control which lines show up on the table withshowGrid,showHorizontalLines, andshowVerticalLines. Use setShowGrid( ) as a convenient way to turn both horizontal and vertical lines on or off at the same time. ThetableHeader property is used to store aJTableHeader object for your table. This header can be used in aJScrollPane as the column header for your table. (No row header counterpart is provided, but you can see an example of creating one in the next chapter.) TherowMargin property determines the amount of empty space between rows. This is really just a more convenient way of getting at the height information in theintercellSpacing property.

##### Table 15-3. JTable visual and editing properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

JTable.AccessibleJTable

cellEditor

TableCellEditor

·

·

null

dragEnabled1.4

boolean

·

·

false

gridColor

Color

·

·

From L&F

intercellSpacing

Dimension

·

·

Dimension(1, 1)

preferredScrollableViewportSizeo

Dimension

·

·

Dimension(450, 400)

rowMargin

int

·

·

1

scrollableTracksViewportHeighto

boolean

·

false

scrollableTracksViewportWidtho

boolean

·

false

selectionBackgroundb

Color

·

·

From L&F

selectionForegroundb

Color

·

·

From L&F

showGrid

boolean

·

true

showHorizontalLines

boolean

·

·

true

showVerticalLines

boolean

·

·

true

·

·

UIb

TableUI

·

·

From L&F

UIClassIDo

String

·

"TableUI"

bbound, ooverridden, 1.4since 1.4

#### 15.1.3 Events

All table-specific events you would expect to see from theJTable class are routed through its data and column models. You must get a reference to these models and attach listeners to the models directly, with code like this example, which uses our custom EEL utility discussed inChapter 3:

TableModel myModel = new MyTableModel( );  // Some valid TableModel class
JTable table = new JTable(myModel);
EEL eel = EEL.getInstance( );
// Listen for added/removed/updated rows.  监听行的增加，移除，修改
TableColumnModel columnModel = table.getColumnModel( );
// Listen for added/removed/moved columns. 监听列的增加，移除，移动
// Listen for row selections.  监听表格行的选择
// Listen for column selections. 监听列的选择
columnModel.getSelectionModel.addListSelectionListener(eel);

You can see a more detailed example using the selection listeners later in this chapter. Examples using model listeners appear inChapter 16

#### 15.1.5 Constructors

public JTable( )

Create a new JTable object using aDefaultTableModel, aDefaultTableColumnModel, and a DefaultListSelectionModel for its models.

public JTable(TableModel dm)
public JTable(TableModel dm, TableColumnModel cm)
public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)

These constructors allow you to specify the exact table model, table column model, and (row) list selection model you want to use. If you want to specify only the column or list selection model, you can passnull as an argument for the other models, and the appropriate default model is created and used.

public JTable(int numRows, int numColumns)

This constructor builds a default table model with the specified number of rows and columns. Default table column and list selection models are also used.

public JTable(Vector data, Vector columnNames)
public JTable(Object data[][], Object columnNames[])

Populate tables by filling the custom table model with data and naming the columns withcolumnNames. In the case of the first constructor, it is assumed that the data vector is a vector containing other vectors, one for each row of data. The data argument can contain any type of object. The table does its best to render the objects in the array appropriately, using labels generated by callingtoString( ) on the objects if necessary. While thecolumnNames argument can also contain an array of any type of object, aString[] is the most common. The default table header renderer uses column names (or thetoString( ) result of nonstring objects) to label columns.

The table models used by these constructors are not instances ofDefaultTable-Model. If you retrieve the table model, you can interact with it only through theTableModel interface.

#### oreilly java swing : JTable 之TableColumn

2011-10-20 11:23:56

#### ng-grid options 一览

2016-12-14 16:05:04

#### ng-grid

2015-04-09 17:15:22

#### java swing 开发 -JTable

2017-01-19 20:15:43

#### Coursera课程Python for everyone：Quiz: Multi-Table Relational SQL

2016-02-17 11:16:18

#### SWING学习：JTable实现自定义的编辑单元格 （１）

2007-04-02 21:23:00

#### JavaSwingのJTable实例

2016-07-07 08:14:45

#### java swing jtable的使用(一)

2012-05-30 09:05:22

#### Java Swing 之JTable及其简单的用法

2015-10-26 21:08:29

#### swing中jTable的数据动态添加效果

2009-11-25 23:25:00