Module java.desktop
Package javax.swing

Class JTable

所有已实现的接口:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, CellEditorListener, ListSelectionListener, RowSorterListener, TableColumnModelListener, TableModelListener, Scrollable

@JavaBean(defaultProperty="UI", description="A component which displays data in a two dimensional grid.") public class JTable extends JComponent implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible, RowSorterListener
JTable 用于显示和编辑常规的二维单元格表。请参阅The Java Tutorial中的如何使用表格,了解关于使用JTable的任务导向文档和示例。

JTable 具有许多功能,使得可以自定义其渲染和编辑,但提供了这些功能的默认值,以便可以轻松设置简单的表格。例如,要设置一个包含10行和10列数字的表格:

      TableModel dataModel = new AbstractTableModel() {
          public int getColumnCount() { return 10; }
          public int getRowCount() { return 10;}
          public Object getValueAt(int row, int col) { return Integer.valueOf(row*col); }
      };
      JTable table = new JTable(dataModel);
      JScrollPane scrollpane = new JScrollPane(table);
 

JTable 通常放置在JScrollPane内。默认情况下,JTable 会调整其宽度,以便不需要水平滚动条。要允许水平滚动条,请使用AUTO_RESIZE_OFF调用setAutoResizeMode(int)。请注意,如果希望在独立视图中使用JTable并希望显示标题,可以使用getTableHeader()获取并单独显示。

要启用行排序和过滤,请使用RowSorter。可以通过以下两种方式之一设置行排序器:

  • 直接设置RowSorter。例如:table.setRowSorter(new TableRowSorter(model))
  • autoCreateRowSorter属性设置为true,这样JTable会为您创建一个RowSorter。例如:setAutoCreateRowSorter(true)

在设计使用JTable的应用程序时,值得密切关注将表示表格数据的数据结构。DefaultTableModel是一个模型实现,它使用VectorVectorObject来存储单元格值。除了将数据从应用程序复制到DefaultTableModel中,还可以将数据包装在TableModel接口的方法中,以便数据可以直接传递给JTable,就像上面的示例一样。这通常会导致更高效的应用程序,因为模型可以选择最适合数据的内部表示。决定是使用AbstractTableModel还是DefaultTableModel的一个好的经验法则是将AbstractTableModel用作创建子类的基类,将DefaultTableModel用作不需要子类化的情况。

源分发的演示区域中的"TableExample"目录提供了一些完整的JTable使用示例,涵盖了如何使用JTable提供从数据库中获取的可编辑数据的视图,以及如何修改显示中的列以使用专门的渲染器和编辑器。

JTable 专门使用整数来引用其显示的模型的行和列。JTable 简单地获取一系列单元格,并在绘制过程中使用getValueAt(int, int)从模型中检索值。重要的是要记住,各种JTable方法返回的列和行索引是基于JTable(视图)的,并不一定与模型使用的索引相同。

默认情况下,可以在JTable中重新排列列,以使视图的列以与模型中的列不同的顺序显示。这不会影响模型的实现:当列重新排序时,JTable 在内部保持列的新顺序,并在查询模型之前转换其列索引。

因此,在编写TableModel时,不需要监听列重新排序事件,因为无论视图中发生什么,模型都将在其自己的坐标系中查询。在示例区域中有一个演示排序算法的演示,利用了这种技术插入另一个坐标系,其中行的顺序发生变化,而不是列的顺序。

类似地,当使用RowSorter提供的排序和过滤功能时,底层的TableModel不需要知道如何进行排序,而是由RowSorter处理。在使用基于行的JTable方法与底层TableModel时,将需要进行坐标转换。所有JTable的基于行的方法都是基于RowSorter的,这不一定与底层TableModel的方法相同。例如,选择始终是基于JTable的,因此在使用RowSorter时,您需要使用convertRowIndexToViewconvertRowIndexToModel进行转换。以下显示了如何将坐标从JTable转换为底层模型的坐标:

   int[] selection = table.getSelectedRows();
   for (int i = 0; i < selection.length; i++) {
     selection[i] = table.convertRowIndexToModel(selection[i]);
   }
   // selection 现在是基于底层 TableModel 的
 

默认情况下,如果启用了排序,JTable 将在排序时以模型为基础保留选择和可变行高。例如,如果当前选择了底层模型中的行 0,在排序后,底层模型中的行 0 仍将被选择。在视觉上,选择可能会发生变化,但在底层模型中,它将保持不变。唯一的例外是如果模型索引不再可见或已删除。例如,如果模型中的行 0 被过滤掉,排序后选择将为空。

J2SE 5 添加了一些方法到JTable,以提供方便的访问一些常见的打印需求。简单的新print()方法允许快速轻松地向应用程序添加打印支持。此外,还提供了一个新的getPrintable(javax.swing.JTable.PrintMode, java.text.MessageFormat, java.text.MessageFormat)方法,用于更高级的打印需求。

对于所有JComponent类,可以使用InputMapActionMapAction对象与KeyStroke关联,并在指定条件下执行操作。

警告: Swing 不是线程安全的。有关更多信息,请参阅Swing的线程策略

警告: 该类的序列化对象将不兼容未来的 Swing 版本。当前的序列化支持适用于短期存储或在运行相同版本的 Swing 应用程序之间的 RMI。从 1.4 版开始,已将所有 JavaBeans 的长期存储支持添加到java.beans包中。请参阅XMLEncoder

自从:
1.2
参见:
  • Field Details

    • AUTO_RESIZE_OFF

      public static final int AUTO_RESIZE_OFF
      不自动调整列宽度;使用水平滚动条。
      参见:
    • AUTO_RESIZE_NEXT_COLUMN

      public static final int AUTO_RESIZE_NEXT_COLUMN
      在UI中调整列时,以相反的方式调整下一列。
      参见:
    • AUTO_RESIZE_SUBSEQUENT_COLUMNS

      public static final int AUTO_RESIZE_SUBSEQUENT_COLUMNS
      在UI调整期间,更改后续列以保持总宽度;这是默认行为。
      参见:
    • AUTO_RESIZE_LAST_COLUMN

      public static final int AUTO_RESIZE_LAST_COLUMN
      在所有调整大小操作期间,仅应用于最后一列的调整。
      参见:
    • AUTO_RESIZE_ALL_COLUMNS

      public static final int AUTO_RESIZE_ALL_COLUMNS
      在所有调整大小操作期间,按比例调整所有列。
      参见:
    • dataModel

      protected TableModel dataModel
      表格的TableModel
    • columnModel

      protected TableColumnModel columnModel
      表格的TableColumnModel
    • selectionModel

      protected ListSelectionModel selectionModel
      表格的ListSelectionModel,用于跟踪行选择。
    • tableHeader

      protected JTableHeader tableHeader
      与表格一起工作的TableHeader
    • rowHeight

      protected int rowHeight
      表格中每行的像素高度。
    • rowMargin

      protected int rowMargin
      每行中单元格之间的边距的像素高度。
    • gridColor

      protected Color gridColor
      网格的颜色。
    • showHorizontalLines

      protected boolean showHorizontalLines
      如果showHorizontalLines为true,则表格在单元格之间绘制水平线。
    • showVerticalLines

      protected boolean showVerticalLines
      如果showVerticalLines为true,则表格在单元格之间绘制垂直线。
    • autoResizeMode

      protected int autoResizeMode
      确定表格是否自动调整表格列的宽度以占据整个表格的宽度,并确定调整方式。
    • autoCreateColumnsFromModel

      protected boolean autoCreateColumnsFromModel
      如果为true,则表格将查询TableModel以构建默认列集。
    • preferredViewportSize

      protected Dimension preferredViewportSize
      用于确定初始可见区域的Scrollable接口。
    • rowSelectionAllowed

      protected boolean rowSelectionAllowed
      如果在此表格中允许行选择,则为True。
    • cellSelectionEnabled

      protected boolean cellSelectionEnabled
      从Java 2平台v1.3开始已过时。请改用rowSelectionAllowed属性和columnSelectionAllowed属性的columnModel,或使用getCellSelectionEnabled方法。
    • editorComp

      protected transient Component editorComp
      如果正在编辑,则为处理编辑的Component
    • cellEditor

      protected transient TableCellEditor cellEditor
      活动的单元格编辑器对象,覆盖当前单元格占用的屏幕空间,允许用户更改其内容。如果表格当前未处于编辑状态,则为null
    • editingColumn

      protected transient int editingColumn
      标识正在编辑的单元格的列。
    • editingRow

      protected transient int editingRow
      标识正在编辑的单元格的行。
    • defaultRenderersByColumnClass

      protected transient Hashtable<Object,Object> defaultRenderersByColumnClass
      一个对象表,按照在TableModel接口中声明的getColumnClass的类索引显示单元格的内容。
    • defaultEditorsByColumnClass

      protected transient Hashtable<Object,Object> defaultEditorsByColumnClass
      一个对象表,按照在TableModel接口中声明的getColumnClass的类索引显示和编辑单元格的内容。
    • selectionForeground

      protected Color selectionForeground
      选定单元格的前景色。
    • selectionBackground

      protected Color selectionBackground
      选定单元格的背景色。
  • Constructor Details

    • JTable

      public JTable()
      构造一个默认的JTable,初始化为默认数据模型、默认列模型和默认选择模型。
      参见:
    • JTable

      public JTable(TableModel dm)
      构造一个JTable,使用dm作为数据模型,一个默认的列模型和一个默认的选择模型进行初始化。
      参数:
      dm - 表格的数据模型
      参见:
    • JTable

      public JTable(TableModel dm, TableColumnModel cm)
      构造一个JTable,使用dm作为数据模型,cm作为列模型和一个默认的选择模型进行初始化。
      参数:
      dm - 表格的数据模型
      cm - 表格的列模型
      参见:
    • JTable

      public JTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm)
      构造一个JTable,使用dm作为数据模型,cm作为列模型,sm作为选择模型进行初始化。如果任何参数为null,此方法将使用相应的默认模型初始化表格。如果cm为非null,则autoCreateColumnsFromModel标志设置为false,否则设置为true,并且列模型将使用适当的TableColumns填充dm中的列。
      参数:
      dm - 表格的数据模型
      cm - 表格的列模型
      sm - 表格的行选择模型
      参见:
    • JTable

      public JTable(int numRows, int numColumns)
      使用DefaultTableModel构造一个具有numRowsnumColumns个空单元格的JTable。列将以"A"、"B"、"C"等形式命名。
      参数:
      numRows - 表格包含的行数
      numColumns - 表格包含的列数
      参见:
    • JTable

      public JTable(Vector<? extends Vector> rowData, Vector<?> columnNames)
      使用VectorVectors rowData中的值和列名columnNames构造一个JTable。包含在rowData中的Vectors应包含该行的值。换句话说,可以使用以下代码获取第1行第5列的单元格的值:
      ((Vector)rowData.elementAt(1)).elementAt(5);
      参数:
      rowData - 新表格的数据
      columnNames - 每列的名称
    • JTable

      public JTable(Object[][] rowData, Object[] columnNames)
      使用二维数组rowData中的值和列名columnNames构造一个JTablerowData是一个行数组,因此可以使用以下代码获取第1行第5列的单元格的值:
       rowData[1][5]; 

      所有行的长度必须与columnNames相同。

      参数:
      rowData - 新表格的数据
      columnNames - 每列的名称
  • Method Details

    • addNotify

      public void addNotify()
      调用configureEnclosingScrollPane方法。
      覆盖:
      addNotify 在类 JComponent
      参见:
    • configureEnclosingScrollPane

      protected void configureEnclosingScrollPane()
      如果此JTable是封闭JScrollPaneviewportView(通常情况),则通过在其他操作中将表格的tableHeader安装为滚动窗格的columnHeaderView来配置此ScrollPane。当以通常方式向JScrollPane添加JTable时,使用new JScrollPane(myTable),在JTable中调用addNotify(当将表格添加到视口时)时会调用JTableaddNotify方法。JTableaddNotify方法反过来调用此方法,该方法受保护,以便子类可以覆盖此默认安装过程。
      参见:
    • removeNotify

      public void removeNotify()
      调用unconfigureEnclosingScrollPane方法。
      覆盖:
      removeNotify 在类 JComponent
      参见:
    • unconfigureEnclosingScrollPane

      protected void unconfigureEnclosingScrollPane()
      通过将封闭滚动窗格的columnHeaderView替换为null来撤消configureEnclosingScrollPane的效果。JTableremoveNotify方法调用此方法,该方法受保护,以便子类可以覆盖此默认卸载过程。
      自:
      1.3
      参见:
    • createScrollPaneForTable

      @Deprecated public static JScrollPane createScrollPaneForTable(JTable aTable)
      Deprecated.
      As of Swing version 1.0.2, replaced by new JScrollPane(aTable).
      等同于new JScrollPane(aTable)
      参数:
      aTable - 用于滚动窗格的JTable
      返回:
      使用aTable创建的JScrollPane
    • setTableHeader

      @BeanProperty(description="The JTableHeader instance which renders the column headers.") public void setTableHeader(JTableHeader tableHeader)
      将与此JTable一起使用的tableHeader设置为newHeader。允许nulltableHeader
      参数:
      tableHeader - 新的tableHeader
      参见:
    • getTableHeader

      public JTableHeader getTableHeader()
      返回此JTable使用的tableHeader
      返回:
      此表格使用的tableHeader
      参见:
    • setRowHeight

      @BeanProperty(description="The height of the specified row.") public void setRowHeight(int rowHeight)
      将所有单元格的高度设置为rowHeight(以像素为单位),重新验证并重绘。单元格的高度将等于行高减去行边距。
      参数:
      rowHeight - 新的行高
      抛出:
      IllegalArgumentException - 如果rowHeight小于1
      参见:
    • getRowHeight

      public int getRowHeight()
      返回表格行的高度,以像素为单位。
      返回:
      表格行的像素高度
      参见:
    • setRowHeight

      @BeanProperty(description="The height in pixels of the cells in <code>row</code>") public void setRowHeight(int row, int rowHeight)
      row的高度设置为rowHeight(以像素为单位),重新验证并重绘。此行中单元格的高度将等于行高减去行边距。
      参数:
      row - 正在更改高度的行
      rowHeight - 新的行高,以像素为单位
      抛出:
      IllegalArgumentException - 如果rowHeight小于1
      自:
      1.3
    • getRowHeight

      public int getRowHeight(int row)
      返回row中单元格的高度,以像素为单位。
      参数:
      row - 要返回其高度的行
      返回:
      行中单元格的像素高度
      自:
      1.3
    • setRowMargin

      @BeanProperty(description="The amount of space between cells.") public void setRowMargin(int rowMargin)
      设置相邻行中单元格之间的空白空间量。
      参数:
      rowMargin - 行之间的像素数
      参见:
    • getRowMargin

      public int getRowMargin()
      获取单元格之间的空白空间量,以像素为单位。等同于:getIntercellSpacing().height
      返回:
      行之间的像素数
      参见:
    • setIntercellSpacing

      @BeanProperty(bound=false, description="The spacing between the cells, drawn in the background color of the JTable.") public void setIntercellSpacing(Dimension intercellSpacing)
      设置rowMargincolumnMargin -- 单元格之间的高度和宽度空间 -- 为intercellSpacing
      参数:
      intercellSpacing - 一个指定单元格之间新宽度和高度的Dimension
      参见:
    • getIntercellSpacing

      public Dimension getIntercellSpacing()
      返回单元格之间的水平和垂直间距。默认间距取决于外观。
      返回:
      单元格之间的水平和垂直间距
      参见:
    • setGridColor

      @BeanProperty(description="The grid color.") public void setGridColor(Color gridColor)
      将用于绘制网格线的颜色设置为gridColor并重新显示。默认颜色取决于外观。
      参数:
      gridColor - 网格线的新颜色
      抛出:
      IllegalArgumentException - 如果gridColornull
      参见:
    • getGridColor

      public Color getGridColor()
      返回用于绘制网格线的颜色。默认颜色取决于外观。
      返回:
      用于绘制网格线的颜色
      参见:
    • setShowGrid

      @BeanProperty(description="The color used to draw the grid lines.") public void setShowGrid(boolean showGrid)
      设置表格是否在单元格周围绘制网格线。如果showGrid为true,则绘制;如果为false,则不绘制。没有getShowGrid方法,因为此状态保存在两个变量中 -- showHorizontalLinesshowVerticalLines -- 每个变量都可以单独查询。
      参数:
      showGrid - 如果表视图应该绘制网格线,则为true
      参见:
    • setShowHorizontalLines

      @BeanProperty(description="Whether horizontal lines should be drawn in between the cells.") public void setShowHorizontalLines(boolean showHorizontalLines)
      设置表格是否在单元格之间绘制水平线。如果showHorizontalLines为true,则绘制;如果为false,则不绘制。
      参数:
      showHorizontalLines - 如果表视图应该绘制水平线,则为true
      参见:
    • setShowVerticalLines

      @BeanProperty(description="Whether vertical lines should be drawn in between the cells.") public void setShowVerticalLines(boolean showVerticalLines)
      设置表格是否在单元格之间绘制垂直线。如果showVerticalLines为true,则绘制;如果为false,则不绘制。
      参数:
      showVerticalLines - 如果表视图应该绘制垂直线,则为true
      参见:
    • getShowHorizontalLines

      public boolean getShowHorizontalLines()
      返回true如果表格在单元格之间绘制水平线,false如果不绘制。默认值取决于外观。
      返回:
      如果表格在单元格之间绘制水平线则为true,否则为false
      参见:
    • getShowVerticalLines

      public boolean getShowVerticalLines()
      返回true如果表格在单元格之间绘制垂直线,false如果不绘制。默认值取决于外观。
      返回:
      如果表格在单元格之间绘制垂直线则为true,否则为false
      参见:
    • setAutoResizeMode

      @BeanProperty(enumerationValues={"JTable.AUTO_RESIZE_OFF","JTable.AUTO_RESIZE_NEXT_COLUMN","JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS","JTable.AUTO_RESIZE_LAST_COLUMN","JTable.AUTO_RESIZE_ALL_COLUMNS"}, description="Whether the columns should adjust themselves automatically.") public void setAutoResizeMode(int mode)
      设置表格在调整大小时的自动调整模式。有关不同调整模式的工作原理的更多信息,请参见doLayout()
      参数:
      mode - 5个合法值之一:AUTO_RESIZE_OFF, AUTO_RESIZE_NEXT_COLUMN, AUTO_RESIZE_SUBSEQUENT_COLUMNS, AUTO_RESIZE_LAST_COLUMN, AUTO_RESIZE_ALL_COLUMNS
      参见:
    • getAutoResizeMode

      public int getAutoResizeMode()
      返回表格的自动调整模式。默认模式为AUTO_RESIZE_SUBSEQUENT_COLUMNS。
      返回:
      表格的autoResizeMode
      参见:
    • setAutoCreateColumnsFromModel

      @BeanProperty(description="Automatically populates the columnModel when a new TableModel is submitted.") public void setAutoCreateColumnsFromModel(boolean autoCreateColumnsFromModel)
      设置此表格的autoCreateColumnsFromModel标志。如果autoCreateColumnsFromModel从false更改为true,则此方法将调用createDefaultColumnsFromModel
      参数:
      autoCreateColumnsFromModel - 如果JTable应自动创建列,则为true
      参见:
    • getAutoCreateColumnsFromModel

      public boolean getAutoCreateColumnsFromModel()
      确定表格是否将从模型创建默认列。如果为true,则setModel将清除任何现有列,并从新模型创建新列。此外,如果tableChanged通知中的事件指定整个表格已更改,则将重建列。默认值为true。
      返回:
      表格的autoCreateColumnsFromModel
      参见:
    • createDefaultColumnsFromModel

      public void createDefaultColumnsFromModel()
      从数据模型使用TableModel接口中定义的getColumnCount方法为表格创建默认列。

      在根据模型信息创建新列之前,清除任何现有列。

      参见:
    • setDefaultRenderer

      public void setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer)
      设置在TableColumn中未设置渲染器时要使用的默认单元格渲染器。如果渲染器为null,则删除此列类的默认渲染器。
      参数:
      columnClass - 为此columnClass设置默认单元格渲染器
      renderer - 用于此columnClass的默认单元格渲染器
      参见:
    • getDefaultRenderer

      public TableCellRenderer getDefaultRenderer(Class<?> columnClass)
      返回在TableColumn中未设置渲染器时要使用的单元格渲染器。在渲染单元格期间,根据列中单元格的类从条目的Hashtable中获取渲染器。如果没有为此columnClass的条目,则该方法返回最具体超类的条目。 JTableObjectNumberBoolean安装条目,所有这些条目都可以修改或替换。
      参数:
      columnClass - 返回此columnClass的默认单元格渲染器
      返回:
      此columnClass的渲染器
      参见:
    • setDefaultEditor

      public void setDefaultEditor(Class<?> columnClass, TableCellEditor editor)
      设置在TableColumn中未设置编辑器时要使用的默认单元格编辑器。如果在表格中不需要编辑,或者在表格中的特定列中不需要编辑,使用TableModel接口中的isCellEditable方法来确保JTable不会在这些列中启动编辑器。如果编辑器为null,则删除此列类的默认编辑器。
      参数:
      columnClass - 为此columnClass设置默认单元格编辑器
      editor - 用于此columnClass的默认单元格编辑器
      参见:
    • getDefaultEditor

      public TableCellEditor getDefaultEditor(Class<?> columnClass)
      Returns the editor to be used when no editor has been set in a TableColumn. During the editing of cells the editor is fetched from a Hashtable of entries according to the class of the cells in the column. If there is no entry for this columnClass the method returns the entry for the most specific superclass. The JTable installs entries for Object, Number, and Boolean, all of which can be modified or replaced.
      Parameters:
      columnClass - return the default cell editor for this columnClass
      Returns:
      the default cell editor to be used for this columnClass
      See Also:
    • setDragEnabled

      @BeanProperty(bound=false, description="determines whether automatic drag handling is enabled") public void setDragEnabled(boolean b)
      Turns on or off automatic drag handling. In order to enable automatic drag handling, this property should be set to true, and the table's TransferHandler needs to be non-null. The default value of the dragEnabled property is false.

      The job of honoring this property, and recognizing a user drag gesture, lies with the look and feel implementation, and in particular, the table's TableUI. When automatic drag handling is enabled, most look and feels (including those that subclass BasicLookAndFeel) begin a drag and drop operation whenever the user presses the mouse button over an item (in single selection mode) or a selection (in other selection modes) and then moves the mouse a few pixels. Setting this property to true can therefore have a subtle effect on how selections behave.

      If a look and feel is used that ignores this property, you can still begin a drag and drop operation by calling exportAsDrag on the table's TransferHandler.

      Parameters:
      b - whether or not to enable automatic drag handling
      Throws:
      HeadlessException - if b is true and GraphicsEnvironment.isHeadless() returns true
      Since:
      1.4
      See Also:
    • getDragEnabled

      public boolean getDragEnabled()
      Returns whether or not automatic drag handling is enabled.
      Returns:
      the value of the dragEnabled property
      Since:
      1.4
      See Also:
    • setDropMode

      public final void setDropMode(DropMode dropMode)
      Sets the drop mode for this component. For backward compatibility, the default for this property is DropMode.USE_SELECTION. Usage of one of the other modes is recommended, however, for an improved user experience. DropMode.ON, for instance, offers similar behavior of showing items as selected, but does so without affecting the actual selection in the table.

      JTable supports the following drop modes:

      • DropMode.USE_SELECTION
      • DropMode.ON
      • DropMode.INSERT
      • DropMode.INSERT_ROWS
      • DropMode.INSERT_COLS
      • DropMode.ON_OR_INSERT
      • DropMode.ON_OR_INSERT_ROWS
      • DropMode.ON_OR_INSERT_COLS

      The drop mode is only meaningful if this component has a TransferHandler that accepts drops.

      Parameters:
      dropMode - the drop mode to use
      Throws:
      IllegalArgumentException - if the drop mode is unsupported or null
      Since:
      1.6
      See Also:
    • getDropMode

      public final DropMode getDropMode()
      Returns the drop mode for this component.
      Returns:
      the drop mode for this component
      Since:
      1.6
      See Also:
    • getDropLocation

      @BeanProperty(bound=false) public final JTable.DropLocation getDropLocation()
      Returns the location that this component should visually indicate as the drop location during a DnD operation over the component, or null if no location is to currently be shown.

      This method is not meant for querying the drop location from a TransferHandler, as the drop location is only set after the TransferHandler's canImport has returned and has allowed for the location to be shown.

      When this property changes, a property change event with name "dropLocation" is fired by the component.

      Returns:
      the drop location
      Since:
      1.6
      See Also:
    • setAutoCreateRowSorter

      @BeanProperty(preferred=true, description="Whether or not to turn on sorting by default.") public void setAutoCreateRowSorter(boolean autoCreateRowSorter)
      Specifies whether a RowSorter should be created for the table whenever its model changes.

      When setAutoCreateRowSorter(true) is invoked, a TableRowSorter is immediately created and installed on the table. While the autoCreateRowSorter property remains true, every time the model is changed, a new TableRowSorter is created and set as the table's row sorter. The default value for the autoCreateRowSorter property is false.

      Parameters:
      autoCreateRowSorter - whether or not a RowSorter should be automatically created
      Since:
      1.6
      See Also:
    • getAutoCreateRowSorter

      public boolean getAutoCreateRowSorter()
      Returns true if whenever the model changes, a new RowSorter should be created and installed as the table's sorter; otherwise, returns false.
      Returns:
      true if a RowSorter should be created when the model changes
      Since:
      1.6
    • setUpdateSelectionOnSort

      @BeanProperty(expert=true, description="Whether or not to update the selection on sorting") public void setUpdateSelectionOnSort(boolean update)
      Specifies whether the selection should be updated after sorting. If true, on sorting the selection is reset such that the same rows, in terms of the model, remain selected. The default is true.
      Parameters:
      update - whether or not to update the selection on sorting
      Since:
      1.6
    • getUpdateSelectionOnSort

      public boolean getUpdateSelectionOnSort()
      Returns true if the selection should be updated after sorting.
      Returns:
      whether to update the selection on a sort
      Since:
      1.6
    • setRowSorter

      @BeanProperty(description="The table\'s RowSorter") public void setRowSorter(RowSorter<? extends TableModel> sorter)
      Sets the RowSorter. RowSorter is used to provide sorting and filtering to a JTable.

      This method clears the selection and resets any variable row heights.

      This method fires a PropertyChangeEvent when appropriate, with the property name "rowSorter". For backward-compatibility, this method fires an additional event with the property name "sorter".

      If the underlying model of the RowSorter differs from that of this JTable undefined behavior will result.

      Parameters:
      sorter - the RowSorter; null turns sorting off
      Since:
      1.6
      See Also:
    • getRowSorter

      public RowSorter<? extends TableModel> getRowSorter()
      Returns the object responsible for sorting.
      Returns:
      the object responsible for sorting
      Since:
      1.6
    • setSelectionMode

      @BeanProperty(enumerationValues={"ListSelectionModel.SINGLE_SELECTION","ListSelectionModel.SINGLE_INTERVAL_SELECTION","ListSelectionModel.MULTIPLE_INTERVAL_SELECTION"}, description="The selection mode used by the row and column selection models.") public void setSelectionMode(int selectionMode)
      Sets the table's selection mode to allow only single selections, a single contiguous interval, or multiple intervals.

      Note: JTable provides all the methods for handling column and row selection. When setting states, such as setSelectionMode, it not only updates the mode for the row selection model but also sets similar values in the selection model of the columnModel. If you want to have the row and column selection models operating in different modes, set them both directly.

      Both the row and column selection models for JTable default to using a DefaultListSelectionModel so that JTable works the same way as the JList. See the setSelectionMode method in JList for details about the modes.

      Parameters:
      selectionMode - the mode used by the row and column selection models
      See Also:
    • setRowSelectionAllowed

      @BeanProperty(visualUpdate=true, description="If true, an entire row is selected for each selected cell.") public void setRowSelectionAllowed(boolean rowSelectionAllowed)
      Sets whether the rows in this model can be selected.
      Parameters:
      rowSelectionAllowed - true if this model will allow row selection
      See Also:
    • getRowSelectionAllowed

      public boolean getRowSelectionAllowed()
      Returns true if rows can be selected.
      返回值:
      如果可以选择行,则为true,否则为false
      参见:
    • setColumnSelectionAllowed

      @BeanProperty(visualUpdate=true, description="If true, an entire column is selected for each selected cell.") public void setColumnSelectionAllowed(boolean columnSelectionAllowed)
      设置此模型中的列是否可以被选择。
      参数:
      columnSelectionAllowed - 如果此模型允许选择列,则为true
      参见:
    • getColumnSelectionAllowed

      public boolean getColumnSelectionAllowed()
      如果可以选择列,则返回true。
      返回值:
      如果可以选择列,则为true,否则为false
      参见:
    • setCellSelectionEnabled

      @BeanProperty(visualUpdate=true, description="Select a rectangular region of cells rather than rows or columns.") public void setCellSelectionEnabled(boolean cellSelectionEnabled)
      设置此表格是否允许同时存在列选择和行选择。当设置时,表格将把行和列选择模型的交集视为选定的单元格。覆盖isCellSelected以更改此默认行为。此方法等同于将columnModelrowSelectionAllowed属性和columnSelectionAllowed属性同时设置为提供的值。
      参数:
      cellSelectionEnabled - 如果允许同时选择行和列,则为true
      参见:
    • getCellSelectionEnabled

      public boolean getCellSelectionEnabled()
      返回true如果同时启用了行选择模型和列选择模型。等同于getRowSelectionAllowed() && getColumnSelectionAllowed()
      返回值:
      如果同时启用了行选择模型和列选择模型,则为true
      参见:
    • selectAll

      public void selectAll()
      选择表格中的所有行、列和单元格。
    • clearSelection

      public void clearSelection()
      取消选择所有选定的列和行。
    • setRowSelectionInterval

      public void setRowSelectionInterval(int index0, int index1)
      选择从index0index1的行,包括两端。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getRowCount()-1]之外
    • setColumnSelectionInterval

      public void setColumnSelectionInterval(int index0, int index1)
      选择从index0index1的列,包括两端。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getColumnCount()-1]之外
    • addRowSelectionInterval

      public void addRowSelectionInterval(int index0, int index1)
      将从index0index1的行添加到当前选择中。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getRowCount()-1]之外
    • addColumnSelectionInterval

      public void addColumnSelectionInterval(int index0, int index1)
      将从index0index1的列添加到当前选择中。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getColumnCount()-1]之外
    • removeRowSelectionInterval

      public void removeRowSelectionInterval(int index0, int index1)
      取消选择从index0index1的行,包括两端。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getRowCount()-1]之外
    • removeColumnSelectionInterval

      public void removeColumnSelectionInterval(int index0, int index1)
      取消选择从index0index1的列,包括两端。
      参数:
      index0 - 区间的一端
      index1 - 区间的另一端
      抛出:
      IllegalArgumentException - 如果index0index1在[0, getColumnCount()-1]之外
    • getSelectedRow

      @BeanProperty(bound=false) public int getSelectedRow()
      返回第一个选定行的索引,如果没有选定行则返回-1。
      返回值:
      第一个选定行的索引
    • getSelectedColumn

      @BeanProperty(bound=false) public int getSelectedColumn()
      返回第一个选定列的索引,如果没有选定列则返回-1。
      返回值:
      第一个选定列的索引
    • getSelectedRows

      @BeanProperty(bound=false) public int[] getSelectedRows()
      返回所有选定行的索引。
      返回值:
      一个包含所有选定行索引的整数数组,如果没有选定行则返回空数组
      参见:
    • getSelectedColumns

      @BeanProperty(bound=false) public int[] getSelectedColumns()
      返回所有选定列的索引。
      返回值:
      一个包含所有选定列索引的整数数组,如果没有选定列则返回空数组
      参见:
    • getSelectedRowCount

      @BeanProperty(bound=false) public int getSelectedRowCount()
      返回选定行的数量。
      返回值:
      选定行的数量,如果没有选定行则为0
    • getSelectedColumnCount

      @BeanProperty(bound=false) public int getSelectedColumnCount()
      返回选定列的数量。
      返回值:
      选定列的数量,如果没有选定列则为0
    • isRowSelected

      public boolean isRowSelected(int row)
      如果指定的索引在有效的行范围内,并且该索引处的行已选定,则返回true。
      参数:
      row - 行模型中的一行
      返回值:
      如果row是有效索引并且该索引处的行已选定(其中0为第一行),则返回true
    • isColumnSelected

      public boolean isColumnSelected(int column)
      如果指定的索引在有效的列范围内,并且该索引处的列已选定,则返回true。
      参数:
      column - 列模型中的列
      返回值:
      如果column是有效索引并且该索引处的列已选定(其中0为第一列),则返回true
    • isCellSelected

      public boolean isCellSelected(int row, int column)
      如果指定的索引在有效的行和列范围内,并且该位置处的单元格已选定,则返回true。
      参数:
      row - 正在查询的行
      column - 正在查询的列
      返回值:
      如果rowcolumn是有效索引,并且索引为(row, column)的单元格已选定(其中第一行和第一列的索引为0),则返回true
    • changeSelection

      public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend)
      根据两个标志toggleextend的状态更新表格的选择模型。大多数由UI接收的键盘或鼠标事件导致的选择更改都通过此方法传递,以便子类可以覆盖行为。一些UI可能需要比此方法提供的更多功能,例如在处理不连续选择的主导时,并且可能不会为某些选择更改调用此方法。

      此实现使用以下约定:

      • toggle: falseextend: false。清除先前的选择并确保新单元格被选定。
      • toggle: falseextend: true。从锚点扩展先前的选择到指定单元格,清除所有其他选择。
      • toggle: trueextend: false。如果指定的单元格已选定,则取消选择它。如果未选定,则选定它。
      • toggle: trueextend: true。将锚点的选择状态应用于其与指定单元格之间的所有单元格。
      参数:
      rowIndex - 影响row处的选择
      columnIndex - 影响column处的选择
      toggle - 参见上述描述
      extend - 如果为true,则扩展当前选择
      自版本:
      1.3
    • getSelectionForeground

      public Color getSelectionForeground()
      返回选定单元格的前景色。
      返回值:
      前景属性的Color对象
      参见:
    • setSelectionForeground

      @BeanProperty(description="A default foreground color for selected cells.") public void setSelectionForeground(Color selectionForeground)
      设置选定单元格的前景色。单元格渲染器可以使用此颜色为选定单元格渲染文本和图形。

      此属性的默认值由外观实现定义。

      这是一个JavaBeans绑定属性。

      Parameters:
      selectionForeground - the Color to use in the foreground for selected list items
      See Also:
    • getSelectionBackground

      public Color getSelectionBackground()
      Returns the background color for selected cells.
      Returns:
      the Color used for the background of selected list items
      See Also:
    • setSelectionBackground

      @BeanProperty(description="A default background color for selected cells.") public void setSelectionBackground(Color selectionBackground)
      Sets the background color for selected cells. Cell renderers can use this color to the fill selected cells.

      The default value of this property is defined by the look and feel implementation.

      This is a JavaBeans bound property.

      Parameters:
      selectionBackground - the Color to use for the background of selected cells
      See Also:
    • getColumn

      public TableColumn getColumn(Object identifier)
      Returns the TableColumn object for the column in the table whose identifier is equal to identifier, when compared using equals.
      Parameters:
      identifier - the identifier object
      Returns:
      the TableColumn object that matches the identifier
      Throws:
      IllegalArgumentException - if identifier is null or no TableColumn has this identifier
    • convertColumnIndexToModel

      public int convertColumnIndexToModel(int viewColumnIndex)
      Maps the index of the column in the view at viewColumnIndex to the index of the column in the table model. Returns the index of the corresponding column in the model. If viewColumnIndex is less than zero, returns viewColumnIndex.
      Parameters:
      viewColumnIndex - the index of the column in the view
      Returns:
      the index of the corresponding column in the model
      See Also:
    • convertColumnIndexToView

      public int convertColumnIndexToView(int modelColumnIndex)
      Maps the index of the column in the table model at modelColumnIndex to the index of the column in the view. Returns the index of the corresponding column in the view; returns -1 if this column is not being displayed. If modelColumnIndex is less than zero, returns modelColumnIndex.
      Parameters:
      modelColumnIndex - the index of the column in the model
      Returns:
      the index of the corresponding column in the view
      See Also:
    • convertRowIndexToView

      public int convertRowIndexToView(int modelRowIndex)
      Maps the index of the row in terms of the TableModel to the view. If the contents of the model are not sorted the model and view indices are the same.
      Parameters:
      modelRowIndex - the index of the row in terms of the model
      Returns:
      the index of the corresponding row in the view, or -1 if the row isn't visible
      Throws:
      IndexOutOfBoundsException - if sorting is enabled and passed an index outside the number of rows of the TableModel
      Since:
      1.6
      See Also:
    • convertRowIndexToModel

      public int convertRowIndexToModel(int viewRowIndex)
      Maps the index of the row in terms of the view to the underlying TableModel. If the contents of the model are not sorted the model and view indices are the same.
      Parameters:
      viewRowIndex - the index of the row in the view
      Returns:
      the index of the corresponding row in the model
      Throws:
      IndexOutOfBoundsException - if sorting is enabled and passed an index outside the range of the JTable as determined by the method getRowCount
      Since:
      1.6
      See Also:
    • getRowCount

      @BeanProperty(bound=false) public int getRowCount()
      Returns the number of rows that can be shown in the JTable, given unlimited space. If a RowSorter with a filter has been specified, the number of rows returned may differ from that of the underlying TableModel.
      Returns:
      the number of rows shown in the JTable
      See Also:
    • getColumnCount

      @BeanProperty(bound=false) public int getColumnCount()
      Returns the number of columns in the column model. Note that this may be different from the number of columns in the table model.
      Returns:
      the number of columns in the table
      See Also:
    • getColumnName

      public String getColumnName(int column)
      Returns the name of the column appearing in the view at column position column.
      Parameters:
      column - the column in the view being queried
      Returns:
      the name of the column at position column in the view where the first column is column 0
    • getColumnClass

      public Class<?> getColumnClass(int column)
      Returns the type of the column appearing in the view at column position column.
      Parameters:
      column - the column in the view being queried
      Returns:
      the type of the column at position column in the view where the first column is column 0
    • getValueAt

      public Object getValueAt(int row, int column)
      Returns the cell value at row and column.

      Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.

      Parameters:
      row - the row whose value is to be queried
      column - the column whose value is to be queried
      Returns:
      the Object at the specified cell
    • setValueAt

      public void setValueAt(Object aValue, int row, int column)
      Sets the value for the cell in the table model at row and column.

      Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering. aValue is the new value.

      Parameters:
      aValue - the new value
      row - the row of the cell to be changed
      column - the column of the cell to be changed
      See Also:
    • isCellEditable

      public boolean isCellEditable(int row, int column)
      Returns true if the cell at row and column is editable. Otherwise, invoking setValueAt on the cell will have no effect.

      Note: The column is specified in the table view's display order, and not in the TableModel's column order. This is an important distinction because as the user rearranges the columns in the table, the column at a given index in the view will change. Meanwhile the user's actions never affect the model's column ordering.

      Parameters:
      row - the row whose value is to be queried
      column - the column whose value is to be queried
      Returns:
      true if the cell is editable
      See Also:
    • addColumn

      public void addColumn(TableColumn aColumn)
      aColumn追加到此JTable列模型持有的列数组的末尾。如果aColumn的列名为null,则将aColumn的列名设置为getModel().getColumnName()返回的名称。

      要向此JTable添加列以显示模型中第modelColumn列的数据,并具有给定的widthcellRenderercellEditor,您可以使用:

            addColumn(new TableColumn(modelColumn, width, cellRenderer, cellEditor));
      
        
      [可以使用任何TableColumn构造函数代替此构造函数。] 模型列号存储在TableColumn内部,并在渲染和编辑期间用于定位模型中的适当数据值。当在视图中重新排序列时,模型列号不会更改。
      参数:
      aColumn - 要添加的TableColumn
      参见:
    • removeColumn

      public void removeColumn(TableColumn aColumn)
      从此JTable的列数组中移除aColumn。注意:此方法不会从模型中删除数据列;它只会删除负责显示数据列的TableColumn
      参数:
      aColumn - 要移除的TableColumn
      参见:
    • moveColumn

      public void moveColumn(int column, int targetColumn)
      将列column移动到视图中当前由列targetColumn占据的位置。位于targetColumn处的旧列将左移或右移以腾出空间。
      参数:
      column - 要移动的列的索引
      targetColumn - 列的新索引
    • columnAtPoint

      public int columnAtPoint(Point point)
      返回point所在的列索引,如果结果不在范围[0,getColumnCount()-1]内,则返回-1。
      参数:
      point - 感兴趣的位置
      返回:
      point所在的列索引,如果结果不在范围[0,getColumnCount()-1]内,则返回-1
      参见:
    • rowAtPoint

      public int rowAtPoint(Point point)
      返回point所在的行索引,如果结果不在范围[0,getRowCount()-1]内,则返回-1。
      参数:
      point - 感兴趣的位置
      返回:
      point所在的行索引,如果结果不在范围[0,getRowCount()-1]内,则返回-1
      参见:
    • getCellRect

      public Rectangle getCellRect(int row, int column, boolean includeSpacing)
      返回位于rowcolumn交汇处的单元格的矩形。如果includeSpacing为true,则返回的值具有指定行和列的完整高度和宽度。如果为false,则返回的矩形将被插入到单元间距中,以返回在渲染期间将设置的渲染或编辑组件的真实边界。

      如果列索引有效,但行索引小于零,则该方法返回一个具有适当设置yheight值的矩形,xwidth值都设置为零。通常,当行或列索引指示单元格超出适当范围时,该方法返回描绘最接近单元格边缘的最接近单元格的矩形。当行和列索引都超出范围时,返回的矩形覆盖最接近单元格的最接近点。

      在所有情况下,使用此方法沿一个轴计算结果的计算不会因为另一个轴上的计算异常而失败。当单元格无效时,将忽略includeSpacing参数。

      参数:
      row - 所需单元格所在的行索引
      column - 所需单元格所在的列索引在显示中;这不一定与表格数据模型中的列索引相同;可以使用convertColumnIndexToView(int)方法将数据模型列索引转换为显示列索引
      includeSpacing - 如果为false,则返回真实单元格边界 - 通过从列和行模型的高度和宽度中减去单元间距来计算
      返回:
      包含位置rowcolumn的单元格的矩形
      参见:
    • doLayout

      public void doLayout()
      使此表格布局其行和列。重写以便可以调整列大小以适应包含父级大小的更改。调整表格中一个或多个列的大小,以使所有此JTable的列的总宽度等于表格的宽度。

      在布局开始之前,该方法获取tableHeaderresizingColumn。当由于调整包含窗口大小而调用该方法时,resizingColumnnull。这意味着调整已在JTable“外部”进行,变化或“增量”应分布到所有列,而不管此JTable的自动调整大小模式如何。

      如果resizingColumn不为null,则它是表格中已更改大小的列之一,而不是表格本身。在这种情况下,自动调整大小模式控制额外(或不足)空间在可用列之间的分配方式。

      模式包括:

      • AUTO_RESIZE_OFF:根本不自动调整列宽。当列的总和超过Viewport的宽度时,使用水平滚动条来容纳列。如果JTable未包含在JScrollPane中,这可能会导致表格的某些部分不可见。
      • AUTO_RESIZE_NEXT_COLUMN:仅使用调整大小列后面的列。这导致相邻单元格之间的“边界”或分隔符是独立可调节的。
      • AUTO_RESIZE_SUBSEQUENT_COLUMNS:使用调整大小列后的所有列来吸收更改。这是默认行为。
      • AUTO_RESIZE_LAST_COLUMN:仅自动调整最后一列的大小。如果最后一列的边界阻止分配所需大小,则将最后一列的宽度设置为适当的限制,并不再进行调整。
      • AUTO_RESIZE_ALL_COLUMNS:将增量分布到JTable中的所有列,包括正在调整的列。

      注意:JTable调整列宽度时,它绝对尊重它们的最小值和最大值。因此,即使调用此方法后,列的总宽度仍然不等于表格的宽度,JTable也不会将自身置于AUTO_RESIZE_OFF模式以显示滚动条,或违反其当前自动调整大小模式的其他承诺 - 相反,它允许其边界设置得比列的最小值或最大值大(或小),这意味着可能没有足够的空间来显示所有列,或者列将不填充JTable的边界。这分别导致某些列被裁剪或在绘制期间在JTable的背景颜色中绘制区域。

      JTable类中提供了用于在可用列之间分配增量的私有方法:

         adjustSizes(long targetSize, final Resizable3 r, boolean inverse)
       
      其解释在以下部分中提供。 Resizable3是一个私有接口,允许包含具有大小、首选大小、最大大小和最小大小的元素集合的任何数据结构通过该算法进行操作。

      分配增量

      概述
      定义

                size[i] = pref[i] + delta[i]
       
      其中每个单独的delta[i]根据以下方式计算: < 0),我们处于收缩模式,其中:

                              DELTA
                delta[i] = ------------ * (pref[i] - min[i])
                           (PREF - MIN)
       
      如果(DELTA > 0),我们处于扩展模式,其中:
                              DELTA
                delta[i] = ------------ * (max[i] - pref[i])
                            (MAX - PREF)
       

      详细信息
      targetSize,并通过将舍入误差均匀分布到给定元素上来实现这一点。

      当达到最大和最小边界时
      targetSize在[MIN,MAX]范围之外时,该算法将所有大小设置为适当的限制值(最大或最小)。

      覆盖:
      doLayout 在类 Container
      参见:
    • sizeColumnsToFit

      @Deprecated public void sizeColumnsToFit(boolean lastColumnOnly)
      Deprecated.
      As of Swing version 1.0.3, replaced by doLayout().
      调整表格列以适应可用空间。
      参数:
      lastColumnOnly - 确定是否仅调整最后一列
      参见:
    • sizeColumnsToFit

      public void sizeColumnsToFit(int resizingColumn)
      自Java 2平台v1.4起已过时。请改用doLayout()方法。
      参数:
      resizingColumn - 使此调整必要的列或-1(如果没有这样的列)
      参见:
    • getToolTipText

      public String getToolTipText(MouseEvent event)
      覆盖JComponentgetToolTipText方法,以允许使用渲染器的提示(如果已设置文本)。

      注意:为了使JTable正确显示其渲染器的工具提示,JTable必须是ToolTipManager中注册的组件。这在initializeLocalVars中自动完成,但如果稍后告知JTable setToolTipText(null),它将取消注册表组件,渲染器的提示将不再显示。

      覆盖:
      getToolTipText 在类 JComponent
      参数:
      event - 引发ToolTip显示的MouseEvent
      返回:
      包含工具提示的字符串
      参见:
    • setSurrendersFocusOnKeystroke

      public void setSurrendersFocusOnKeystroke(boolean surrendersFocusOnKeystroke)
      设置此JTable中的编辑器在由于JTable转发键盘事件而激活编辑器时是否获得键盘焦点。默认情况下,此属性为false,JTable保留焦点,除非单击了单元格。
      参数:
      surrendersFocusOnKeystroke - 如果编辑器应在按键导致编辑器激活时获得焦点,则为true
      自:
      1.4
      参见:
    • getSurrendersFocusOnKeystroke

      public boolean getSurrendersFocusOnKeystroke()
      如果编辑器应在按键导致编辑器激活时获得焦点,则返回true。
      返回:
      如果编辑器应在按键导致编辑器激活时获得焦点,则返回true
      自:
      1.4
      参见:
    • editCellAt

      public boolean editCellAt(int row, int column)
      如果索引在有效范围内且这些索引处的单元格可编辑,则以编程方式开始编辑rowcolumn处的单元格。请注意,这是editCellAt(int, int, null)的便利方法。
      参数:
      row - 要编辑的行
      column - 要编辑的列
      返回:
      如果由于任何原因无法编辑单元格,或者索引无效,则返回false
    • editCellAt

      public boolean editCellAt(int row, int column, EventObject e)
      如果索引在有效范围内且这些索引处的单元格可编辑,则以编程方式开始编辑rowcolumn处的单元格。要防止JTable编辑特定表、列或单元格值,请在TableModel接口中的isCellEditable方法中返回false。
      参数:
      row - 要编辑的行
      column - 要编辑的列
      e - 传递到shouldSelectCell的事件;请注意,自Java 2平台v1.2起,不再调用shouldSelectCell
      返回:
      如果由于任何原因无法编辑单元格,或者索引无效,则返回false
    • isEditing

      @BeanProperty(bound=false) public boolean isEditing()
      返回true如果正在编辑单元格。
      返回:
      如果表正在编辑单元格,则返回true
      参见:
    • getEditorComponent

      @BeanProperty(bound=false) public Component getEditorComponent()
      返回处理编辑会话的组件。如果没有正在编辑,则返回null。
      返回:
      处理编辑会话的组件
    • getEditingColumn

      public int getEditingColumn()
      返回包含当前正在编辑的单元格的列的索引。如果没有正在编辑,则返回-1。
      返回:
      包含当前正在编辑的单元格的列的索引;如果没有正在编辑,则返回-1
      参见:
    • getEditingRow

      public int getEditingRow()
      返回渲染此组件的L&F对象。
      Returns:
      the index of the row that contains the cell currently being edited; returns -1 if nothing being edited
      See Also:
    • getUI

      public TableUI getUI()
      Returns the L&F object that renders this component.
      覆盖:
      getUI 在类 JComponent
      返回:
      渲染此组件的TableUI对象
    • setUI

      @BeanProperty(hidden=true, visualUpdate=true, description="The UI object that implements the Component\'s LookAndFeel.") public void setUI(TableUI ui)
      设置渲染此组件的L&F对象并重绘。
      参数:
      ui - TableUI L&F对象
      参见:
    • updateUI

      public void updateUI()
      来自UIManager的通知,L&F已更改。用最新版本从UIManager替换当前UI对象。
      覆盖:
      updateUI 在类 JComponent
      参见:
    • getUIClassID

      @BeanProperty(bound=false) public String getUIClassID()
      返回用于构造用于渲染此组件的L&F类名称的后缀。
      覆盖:
      getUIClassID 在类 JComponent
      返回:
      字符串"TableUI"
      参见:
    • setModel

      @BeanProperty(description="The model that is the source of the data for this view.") public void setModel(TableModel dataModel)
      将此表的数据模型设置为dataModel并向其注册以接收来自新数据模型的监听器通知。
      参数:
      dataModel - 此表的新数据源
      抛出:
      IllegalArgumentException - 如果dataModelnull
      参见:
    • getModel

      public TableModel getModel()
      返回提供此显示的数据的TableModel
      返回:
      提供此显示的数据的TableModel
      参见:
    • setColumnModel

      @BeanProperty(description="The object governing the way columns appear in the view.") public void setColumnModel(TableColumnModel columnModel)
      将此表的列模型设置为columnModel并向其注册以接收来自新列模型的监听器通知。还将JTableHeader的列模型设置为columnModel
      参数:
      columnModel - 此表的新数据源
      抛出:
      IllegalArgumentException - 如果columnModelnull
      参见:
    • getColumnModel

      public TableColumnModel getColumnModel()
      返回包含此表的所有列信息的TableColumnModel
      返回:
      提供表的列状态的对象
      参见:
    • setSelectionModel

      @BeanProperty(description="The selection model for rows.") public void setSelectionModel(ListSelectionModel selectionModel)
      将此表的行选择模型设置为selectionModel并向其注册以接收来自新选择模型的监听器通知。
      参数:
      selectionModel - 新的选择模型
      抛出:
      IllegalArgumentException - 如果selectionModelnull
      参见:
    • getSelectionModel

      public ListSelectionModel getSelectionModel()
      返回用于维护行选择状态的ListSelectionModel
      返回:
      提供行选择状态的对象,如果不允许行选择则为null
      参见:
    • sorterChanged

      public void sorterChanged(RowSorterEvent e)
      RowSorterListener通知RowSorter以某种方式发生了更改。
      指定者:
      sorterChanged 在接口 RowSorterListener
      参数:
      e - 描述更改的RowSorterEvent
      抛出:
      NullPointerException - 如果enull
      自:
      1.6
    • tableChanged

      public void tableChanged(TableModelEvent e)
      当此表的TableModel生成TableModelEvent时调用。 TableModelEvent应在模型的坐标系中构造;当JTable接收到事件时,将执行到视图坐标系的适当映射。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      请注意,从1.3开始,此方法会清除选择(如果有)。

      指定者:
      tableChanged 在接口 TableModelListener
      参数:
      e - 一个TableModelEvent,通知监听器表模型已更改
    • columnAdded

      public void columnAdded(TableColumnModelEvent e)
      当向表列模型添加列时调用。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      columnAdded 在接口 TableColumnModelListener
      参数:
      e - 一个TableColumnModelEvent
      参见:
    • columnRemoved

      public void columnRemoved(TableColumnModelEvent e)
      当从表列模型中移除列时调用。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      columnRemoved 在接口 TableColumnModelListener
      参数:
      e - 一个TableColumnModelEvent
      参见:
    • columnMoved

      public void columnMoved(TableColumnModelEvent e)
      当列重新定位时调用。如果正在编辑单元格,则会停止编辑并重新绘制单元格。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      columnMoved 在接口 TableColumnModelListener
      参数:
      e - 接收到的事件
      参见:
    • columnMarginChanged

      public void columnMarginChanged(ChangeEvent e)
      当由于边距更改而移动列时调用。如果正在编辑单元格,则会停止编辑并重新绘制单元格。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      columnMarginChanged 在接口 TableColumnModelListener
      参数:
      e - 接收到的事件
      参见:
    • columnSelectionChanged

      public void columnSelectionChanged(ListSelectionEvent e)
      TableColumnModel的选择模型更改时调用。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      columnSelectionChanged 在接口 TableColumnModelListener
      参数:
      e - 接收到的事件
      参见:
    • valueChanged

      public void valueChanged(ListSelectionEvent e)
      当行选择更改时调用,重新绘制以显示新选择。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      valueChanged 在接口 ListSelectionListener
      参数:
      e - 接收到的事件
      参见:
    • editingStopped

      public void editingStopped(ChangeEvent e)
      当编辑完成时调用。更改将被保存,编辑器将被丢弃。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      editingStopped 在接口 CellEditorListener
      参数:
      e - 接收到的事件
      参见:
    • editingCanceled

      public void editingCanceled(ChangeEvent e)
      当编辑被取消时调用。编辑器对象将被丢弃,单元格将被重新渲染。

      应用代码不会显式使用这些方法,它们由JTable在内部使用。

      指定者:
      editingCanceled 在接口 CellEditorListener
      参数:
      e - 接收到的事件
      参见:
    • setPreferredScrollableViewportSize

      @BeanProperty(bound=false, description="The preferred size of the viewport.") public void setPreferredScrollableViewportSize(Dimension size)
      设置此表的视口的首选大小。
      参数:
      size - 指定JViewport视图的preferredSizeDimension对象
      参见:
    • getPreferredScrollableViewportSize

      public Dimension getPreferredScrollableViewportSize()
      返回此表的视口的首选大小。
      指定者:
      getPreferredScrollableViewportSize 在接口 Scrollable
      返回:
      包含显示此表的JViewportpreferredSizeDimension对象
      参见:
    • getScrollableUnitIncrement

      public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
      返回一个滚动增量(以像素为单位),完全暴露一个新行或列(取决于方向)。

      每次用户请求单位滚动时都会调用此方法。

      指定者:
      getScrollableUnitIncrement 在接口 Scrollable
      参数:
      visibleRect - 视口内可见的视图区域
      orientation - 为SwingConstants.VERTICALSwingConstants.HORIZONTAL
      direction - 小于零表示向上/左滚动,大于零表示向下/右滚动
      返回值:
      指定方向滚动的“单位”增量
      参见:
    • getScrollableBlockIncrement

      public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction)
      返回visibleRect.heightvisibleRect.width,取决于此表的方向。请注意,从Swing 1.1.1(Java 2 v 1.2.2)开始,返回的值将确保视口在行边界上对齐。
      指定者:
      getScrollableBlockIncrement 在接口 Scrollable
      参数:
      visibleRect - 视口内可见的视图区域
      orientation - 为SwingConstants.VERTICAL或SwingConstants.HORIZONTAL
      direction - 小于零表示向上/左滚动,大于零表示向下/右滚动
      返回值:
      根据方向返回visibleRect.heightvisibleRect.width
      参见:
    • getScrollableTracksViewportWidth

      @BeanProperty(bound=false) public boolean getScrollableTracksViewportWidth()
      如果autoResizeMode设置为AUTO_RESIZE_OFF,表示视口的宽度不确定表的宽度,则返回false。否则返回true。
      指定者:
      getScrollableTracksViewportWidth 在接口 Scrollable
      返回值:
      如果autoResizeMode设置为AUTO_RESIZE_OFF,返回false;否则返回true
      参见:
    • getScrollableTracksViewportHeight

      @BeanProperty(bound=false) public boolean getScrollableTracksViewportHeight()
      返回false以指示视口的高度不确定表的高度,除非getFillsViewportHeighttrue且表的首选高度小于视口的高度。
      指定者:
      getScrollableTracksViewportHeight 在接口 Scrollable
      返回值:
      如果getFillsViewportHeighttrue且需要拉伸表以填充视口,则返回false
      参见:
    • setFillsViewportHeight

      @BeanProperty(description="Whether or not this table is always made large enough to fill the height of an enclosing viewport") public void setFillsViewportHeight(boolean fillsViewportHeight)
      设置此表是否始终足够大以填充封闭视口的高度。如果表的首选高度小于视口的高度,则表将被拉伸以填充视口。换句话说,这确保表永远不会小于视口。此属性的默认值为false
      参数:
      fillsViewportHeight - 是否始终使此表足够大以填充封闭视口的高度
      自:
      1.6
      参见:
    • getFillsViewportHeight

      public boolean getFillsViewportHeight()
      返回此表是否始终足够大以填充封闭视口的高度。
      返回值:
      返回此表是否始终足够大以填充封闭视口的高度
      自:
      1.6
      参见:
    • createDefaultRenderers

      protected void createDefaultRenderers()
      为对象、数字、双精度数、日期、布尔值和图标创建默认单元格渲染器。
      参见:
    • createDefaultEditors

      protected void createDefaultEditors()
      为对象、数字和布尔值创建默认单元格编辑器。
      参见:
    • initializeLocalVars

      protected void initializeLocalVars()
      将表属性初始化为默认值。
    • createDefaultDataModel

      protected TableModel createDefaultDataModel()
      返回默认的表模型对象,即DefaultTableModel。子类可以重写此方法以返回不同的表模型对象。
      返回值:
      默认的表模型对象
      参见:
    • createDefaultColumnModel

      protected TableColumnModel createDefaultColumnModel()
      返回默认的列模型对象,即DefaultTableColumnModel。子类可以重写此方法以返回不同的列模型对象。
      返回值:
      默认的列模型对象
      参见:
    • createDefaultSelectionModel

      protected ListSelectionModel createDefaultSelectionModel()
      返回默认的选择模型对象,即DefaultListSelectionModel。子类可以重写此方法以返回不同的选择模型对象。
      返回值:
      默认的选择模型对象
      参见:
    • createDefaultTableHeader

      protected JTableHeader createDefaultTableHeader()
      返回默认的表头对象,即JTableHeader。子类可以重写此方法以返回不同的表头对象。
      返回值:
      默认的表头对象
      参见:
    • resizeAndRepaint

      protected void resizeAndRepaint()
      等同于revalidate后跟repaint
    • getCellEditor

      public TableCellEditor getCellEditor()
      返回活动的单元格编辑器,如果表当前未编辑,则返回null
      返回值:
      进行编辑的TableCellEditor,如果表当前未编辑则返回null
      参见:
    • setCellEditor

      @BeanProperty(description="The table\'s active cell editor.") public void setCellEditor(TableCellEditor anEditor)
      设置活动的单元格编辑器。
      参数:
      anEditor - 活动的单元格编辑器
      参见:
    • setEditingColumn

      public void setEditingColumn(int aColumn)
      设置editingColumn变量。
      参数:
      aColumn - 要编辑的单元格的列
      参见:
    • setEditingRow

      public void setEditingRow(int aRow)
      设置editingRow变量。
      参数:
      aRow - 要编辑的单元格的行
      参见:
    • getCellRenderer

      public TableCellRenderer getCellRenderer(int row, int column)
      返回指定行和列的单元格的适当渲染器。如果此列的TableColumn具有非空渲染器,则返回该渲染器。如果没有,则查找此列中数据的类(使用getColumnClass)并返回此类型数据的默认渲染器。

      注意:在整个表包中,内部实现始终使用此方法提供渲染器,以便子类可以安全地覆盖此默认行为。

      参数:
      row - 要渲染的单元格的行,其中0是第一行
      column - 要渲染的单元格的列,其中0是第一列
      返回值:
      分配的渲染器;如果为null,则返回此类型对象的默认渲染器
      参见:
    • prepareRenderer

      public Component prepareRenderer(TableCellRenderer renderer, int row, int column)
      准备渲染器,通过查询数据模型获取位于rowcolumn处的单元格的值和选择状态。返回事件位置下的组件(可能是ComponentJComponent)。

      在打印操作期间,此方法将配置渲染器,而不指示选择或焦点,以防止它们出现在打印输出中。要根据表格是否正在打印进行其他自定义设置,可以检查JComponent.isPaintingForPrint()的值,可以在此处或在自定义渲染器中进行检查。

      注意:在整个表格包中,内部实现始终使用此方法准备渲染器,以便可以安全地由子类覆盖此默认行为。

      参数:
      renderer - 要准备的TableCellRenderer
      row - 要渲染的单元格的行,其中0是第一行
      column - 要渲染的单元格的列,其中0是第一列
      返回:
      事件位置下的Component
    • getCellEditor

      public TableCellEditor getCellEditor(int row, int column)
      返回指定rowcolumn的单元格的适当编辑器。如果此列的TableColumn具有非空编辑器,则返回该编辑器。如果没有,则查找此列中数据的类(使用getColumnClass)并返回此类型数据的默认编辑器。

      注意:在整个表格包中,内部实现始终使用此方法提供编辑器,以便可以安全地由子类覆盖此默认行为。

      参数:
      row - 要编辑的单元格的行,其中0是第一行
      column - 要编辑的单元格的列,其中0是第一列
      返回:
      此单元格的编辑器;如果为null,则返回此类型单元格的默认编辑器
      参见:
    • prepareEditor

      public Component prepareEditor(TableCellEditor editor, int row, int column)
      通过查询数据模型获取位于rowcolumn处的单元格的值和选择状态来准备编辑器。

      注意:在整个表格包中,内部实现始终使用此方法准备编辑器,以便可以安全地由子类覆盖此默认行为。

      参数:
      editor - 要设置的TableCellEditor
      row - 要编辑的单元格的行,其中0是第一行
      column - 要编辑的单元格的列,其中0是第一列
      返回:
      正在编辑的Component
    • removeEditor

      public void removeEditor()
      丢弃编辑器对象并释放其用于单元格渲染的空间。
    • paramString

      protected String paramString()
      返回此表格的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null
      覆盖:
      paramString 在类 JComponent
      返回:
      此表格的字符串表示形式
    • print

      public boolean print() throws PrinterException
      一个方便的方法,显示打印对话框,然后以PrintMode.FIT_WIDTH模式打印此JTable,不包含页眉或页脚文本。在打印过程中将显示一个模态进度对话框,其中包含中止选项。

      注意:在无头模式下,不会显示对话框,并且打印将在默认打印机上进行。

      返回:
      除非用户取消打印,否则为true
      抛出:
      SecurityException - 如果此线程不允许发起打印作业请求
      PrinterException - 如果打印系统中的错误导致作业中止
      自:
      1.5
      参见:
    • print

      public boolean print(JTable.PrintMode printMode) throws PrinterException
      一个方便的方法,显示打印对话框,然后以给定的打印模式打印此JTable,不包含页眉或页脚文本。在打印过程中将显示一个模态进度对话框,其中包含中止选项。

      注意:在无头模式下,不会显示对话框,并且打印将在默认打印机上进行。

      参数:
      printMode - 可打印应使用的打印模式
      返回:
      除非用户取消打印,否则为true
      抛出:
      SecurityException - 如果此线程不允许发起打印作业请求
      PrinterException - 如果打印系统中的错误导致作业中止
      自:
      1.5
      参见:
    • print

      public boolean print(JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat) throws PrinterException
      一个方便的方法,显示打印对话框,然后以给定的打印模式打印此JTable,包含指定的页眉和页脚文本。在打印过程中将显示一个模态进度对话框,其中包含中止选项。

      注意:在无头模式下,不会显示对话框,并且打印将在默认打印机上进行。

      参数:
      printMode - 可打印应使用的打印模式
      headerFormat - 指定用于打印页眉的MessageFormat,如果没有则为null
      footerFormat - 指定用于打印页脚的MessageFormat,如果没有则为null
      返回:
      除非用户取消打印,否则为true
      抛出:
      SecurityException - 如果此线程不允许发起打印作业请求
      PrinterException - 如果打印系统中的错误导致作业中止
      自:
      1.5
      参见:
    • print

      public boolean print(JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintRequestAttributeSet attr, boolean interactive) throws PrinterException, HeadlessException
      以指定的打印模式打印此表格,具体取决于完全特色的print方法,将默认打印机指定为打印服务。
      参数:
      printMode - 可打印应使用的打印模式
      headerFormat - 指定用于打印页眉的MessageFormat,如果没有则为null
      footerFormat - 指定用于打印页脚的MessageFormat,如果没有则为null
      showPrintDialog - 是否显示打印对话框
      attr - 指定任何打印属性的PrintRequestAttributeSet,如果没有则为null
      interactive - 是否以交互模式打印
      返回:
      除非用户取消打印,否则为true
      抛出:
      HeadlessException - 如果要求显示打印对话框或以交互方式运行该方法,并且GraphicsEnvironment.isHeadless返回true
      SecurityException - 如果此线程不允许发起打印作业请求
      PrinterException - 如果打印系统中的错误导致作业中止
      自:
      1.5
      参见:
    • print

      public boolean print(JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat, boolean showPrintDialog, PrintRequestAttributeSet attr, boolean interactive, PrintService service) throws PrinterException, HeadlessException
      打印此JTable。采取大多数开发人员在打印JTable时会采取的步骤。简而言之,它准备表格,调用getPrintable来获取适当的Printable,然后将其发送到打印机。

      一个boolean参数允许您指定是否向用户显示打印对话框。当显示对话框时,用户可以使用对话框更改目标打印机或打印属性,甚至取消打印。另外两个参数允许指定PrintService和打印属性。这些参数可以用于提供打印对话框的初始值,或者在不显示对话框时指定值。

      第二个boolean参数允许您指定是否以交互模式执行打印。如果为true,则在打印过程中会显示一个带有中止选项的模态进度对话框。此对话框还会阻止可能影响表格的任何用户操作。但它无法阻止表格被代码修改(例如,使用SwingUtilities.invokeLater发布更新的另一个线程)。因此,开发人员有责任确保在打印过程中没有其他代码以任何方式修改表格(无效修改包括大小、渲染器或基础数据的更改)。当在打印过程中更改表格时,打印行为是未定义的。

      如果为此参数指定false,则不会显示对话框,并且打印将立即在事件分派线程上开始。这会阻止处理任何其他事件,包括重绘,直到打印完成。虽然这有效地防止表格被更改,但这并不提供良好的用户体验。因此,仅建议在从没有可见GUI的应用程序打印时指定false

      注意:在无头模式下尝试显示打印对话框或以交互方式运行将导致HeadlessException

      在获取可打印内容之前,如果有必要,此方法将优雅地终止编辑,以防止编辑器显示在打印结果中。此外,JTable将在打印过程中准备其渲染器,以便不指示选择和焦点。至于进一步定制打印输出中表格外观的方式,开发人员可以根据JComponent.isPaintingForPrint()的值提供自定义渲染器或绘制代码。

      更多关于表格打印方式的描述,请参见getPrintable(javax.swing.JTable.PrintMode, java.text.MessageFormat, java.text.MessageFormat)

      参数:
      printMode - 可打印内容应使用的打印模式
      headerFormat - 指定在打印页眉中使用的文本的MessageFormat,或null表示无
      footerFormat - 指定在打印页脚中使用的文本的MessageFormat,或null表示无
      showPrintDialog - 是否显示打印对话框
      attr - 指定任何打印属性的PrintRequestAttributeSet,或null表示无
      interactive - 是否以交互模式打印
      service - 目标PrintService,或null表示使用默认打印机
      返回:
      true,除非用户取消打印
      抛出:
      HeadlessException - 如果要求方法显示打印对话框或以交互方式运行,并且GraphicsEnvironment.isHeadless返回true
      SecurityException - 如果存在安全管理器且其SecurityManager.checkPrintJobAccess()方法禁止此线程创建打印作业请求
      PrinterException - 如果打印系统中的错误导致作业被中止
      自:
      1.6
      参见:
    • getPrintable

      public Printable getPrintable(JTable.PrintMode printMode, MessageFormat headerFormat, MessageFormat footerFormat)
      返回一个用于打印此JTable的Printable

      此方法适用于希望自定义JTableprint方法使用的默认Printable实现的开发人员。只想打印表格的开发人员应直接使用其中一个方法。

      Printable可以以两种打印模式之一请求。在两种模式中,它会自然地按顺序跨多个页面展开表格行,每页尽可能多地容纳行。 PrintMode.NORMAL指定以当前大小打印表格。在此模式下,可能需要以与表格的ComponentOrientation一致的顺序在页面上分布列。当需要时,列将按照表格的ComponentOrientation一致的顺序分布。 PrintMode.FIT_WIDTH指定如有必要,将输出缩小以适应表格的整个宽度(从而所有列)在每页上。宽度和高度等比例缩放,保持输出的纵横比。

      Printable在每页上的表格部分的开头使用适当的部分来自表格的JTableHeader

      通过提供MessageFormat参数,可以将页眉和页脚文本添加到输出中。打印代码从格式请求字符串,提供一个可能包含在格式化字符串中的单个项目:表示当前页码的Integer

      建议阅读MessageFormat的文档,因为某些字符(例如单引号)是特殊的,需要转义。

      以下是创建MessageFormat的示例,可用于打印“Duke's Table: Page - ”和当前页码:

           // 注意单引号的转义
           // 注意页码如何包含在“{0}”中
           MessageFormat format = new MessageFormat("Duke''s Table: Page - {0}");
       

      Printable将其绘制限制为打印每个页面的可打印区域。在某些情况下,可能会发现将页面的所有内容都放入该区域是不可能的。在这些情况下,输出可能会被裁剪,但实现会尽力做出合理的处理。以下是已知会发生这种情况的几种情况,以及此特定实现可能如何处理:

      • 在任何模式下,当页眉或页脚文本过宽而无法完全适应可打印区域时--从开头开始打印尽可能多的文本,由表格的ComponentOrientation确定。
      • 在任何模式下,当行太高而无法适应可打印区域时--打印行的最上部分,并且不在表格上绘制下边框。
      • PrintMode.NORMAL下,当列太宽而无法适应可打印区域时--打印列的中间部分,并且不在表格上绘制左右边框。

      完全有效的是将此Printable包装在另一个中,以创建复杂的报告和文档。甚至可以请求将不同页面呈现为不同大小的可打印区域。实现必须准备好处理这一点(可能通过即时进行布局计算)。但是,当必须在页面间分布列时,为每个页面提供不同的高度在使用PrintMode.NORMAL时可能效果不佳。

      至于定制打印结果中表格外观的方式,JTable本身将负责在打印过程中隐藏选择和焦点。对于其他自定义,您的渲染器或绘制代码可以根据JComponent.isPaintingForPrint()的值自定义外观。

      此方法调用之前您可能希望首先修改表格的状态,例如取消单元格编辑或让用户适当调整表格的大小。但在获取此Printable之后,不得修改表格的状态(无效修改包括大小或基础数据的更改)。一旦表格被更改,返回的Printable的行为是未定义的。

      参数:
      printMode - 可打印内容应使用的打印模式
      headerFormat - 指定在打印页眉中使用的文本的MessageFormat,或null表示无
      footerFormat - 指定在打印页脚中使用的文本的MessageFormat,或null表示无
      返回:
      用于打印此JTable的Printable
      自:
      1.5
      参见:
    • getAccessibleContext

      @BeanProperty(bound=false) public AccessibleContext getAccessibleContext()
      获取与此JTable关联的AccessibleContext。对于表格,AccessibleContext采用AccessibleJTable的形式。如果需要,将创建一个新的AccessibleJTable实例。
      指定者:
      getAccessibleContext 在接口 Accessible
      覆盖:
      getAccessibleContext 在类 Component
      返回:
      作为此JTable的AccessibleContext的AccessibleJTable