带有自定义 TableCellRenderer 的 Swing JTable
Swing JTable with custom TableCellRenderer
在我的 java 应用程序中,我想设置颜色以及选中时的行为。为此,我编写了 TableCellRenderer 的自定义实现,它可以正常工作。但是有些事情我仍然很困惑...
下面是 TableCellRenderer
的实现:
public class AccountMovementTableCellRenderer extends JLabel implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
//My implementation here...
return this;
}
}
这里创建了 JTable
:
AccountMovementTableCellRenderer accountMovementCellRenderer = new AccountMovementTableCellRenderer();
entryTable = new JTable(entryModel){
private static final long serialVersionUID = 1L;
@Override
public TableCellRenderer getCellRenderer(int row, int column){
return accountMovementCellRenderer;
}
};
我只创建了一个 CellRenderer 实例,但我期望每个单元格有一个 CellRenderer,我很惊讶它以这种方式工作......每个单元格的内容和颜色都不同,但它始终使用相同的实例CellRenderer,那么它是如何工作的?
单个渲染器实例为每个单元格创建一个独特的 图像,每个图像根据您上面的代码反映该单元格的状态。
TableCellRenderer.prepareRenderer
returns 的组件被重新用于渲染 JTable
的内容 - 在你的情况下你的渲染器扩展 JLabel
(你可以只扩展DefaultTableCellRenderer
) - 此 JLabel 用于绘制 JTable
的内容。 prepareRenderer
方法用于在渲染前为每个单元格自定义 JLabel。引用 Oracle's tutorial on the JTables
You might expect each cell in a table to be a component. However, for performance reasons, Swing tables are implemented differently.
Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp appropriately formatted data onto each cell. When the user starts to edit a cell's data, a cell editor takes over the cell, controlling the cell's editing behavior.
在我的 java 应用程序中,我想设置颜色以及选中时的行为。为此,我编写了 TableCellRenderer 的自定义实现,它可以正常工作。但是有些事情我仍然很困惑...
下面是 TableCellRenderer
的实现:
public class AccountMovementTableCellRenderer extends JLabel implements TableCellRenderer{
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column){
//My implementation here...
return this;
}
}
这里创建了 JTable
:
AccountMovementTableCellRenderer accountMovementCellRenderer = new AccountMovementTableCellRenderer();
entryTable = new JTable(entryModel){
private static final long serialVersionUID = 1L;
@Override
public TableCellRenderer getCellRenderer(int row, int column){
return accountMovementCellRenderer;
}
};
我只创建了一个 CellRenderer 实例,但我期望每个单元格有一个 CellRenderer,我很惊讶它以这种方式工作......每个单元格的内容和颜色都不同,但它始终使用相同的实例CellRenderer,那么它是如何工作的?
单个渲染器实例为每个单元格创建一个独特的 图像,每个图像根据您上面的代码反映该单元格的状态。
TableCellRenderer.prepareRenderer
returns 的组件被重新用于渲染 JTable
的内容 - 在你的情况下你的渲染器扩展 JLabel
(你可以只扩展DefaultTableCellRenderer
) - 此 JLabel 用于绘制 JTable
的内容。 prepareRenderer
方法用于在渲染前为每个单元格自定义 JLabel。引用 Oracle's tutorial on the JTables
You might expect each cell in a table to be a component. However, for performance reasons, Swing tables are implemented differently.
Instead, a single cell renderer is generally used to draw all of the cells that contain the same type of data. You can think of the renderer as a configurable ink stamp that the table uses to stamp appropriately formatted data onto each cell. When the user starts to edit a cell's data, a cell editor takes over the cell, controlling the cell's editing behavior.