简体   繁体   English

使用自定义TableCellRenderer摇摆JTable

[英]Swing JTable with custom TableCellRenderer

In my java application I want to set the color and also the behavior when it is selected. 在我的Java应用程序中,我想设置颜色以及选择颜色时的行为。 For this I wrote a custom implementation of the TableCellRenderer and it is working as I want. 为此,我编写了TableCellRenderer的自定义实现,并且可以根据需要运行。 But there is something I'm still confused about... 但是我仍然有些困惑...

Here is the implementation of the 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;
    }
}

And here the creation of the JTable : 这里是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;
    }
};

I create only one instance of my CellRenderer but I was expecting one CellRenderer per cell and I was surprised it is working this way... The content and color is for every cell different but it uses always the same instance of the CellRenderer, so how could this work? 我只创建了一个CellRenderer实例,但是我期望每个单元格一个CellRenderer,但我很惊讶它以这种方式工作...每个单元格的内容和颜色都是不同的,但是它始终使用相同的CellRenderer实例,因此能行吗?

The Component that TableCellRenderer.prepareRenderer returns is reused to render the contents of the JTable - in your case your renderer extends JLabel (you can could have just extended DefaultTableCellRenderer ) - this JLabel is used to paint the contents of the JTable . TableCellRenderer.prepareRenderer返回的Component被重用以呈现JTable的内容-在您的情况下,您的呈现器扩展了JLabel (您可能已经扩展了DefaultTableCellRenderer )-此JLabel用于绘制JTable的内容。 The prepareRenderer method is used to customize the JLabel for each cell before rendering. prepareRenderer方法用于在渲染之前为每个单元格定制JLabel。 To quote Oracle's tutorial on the JTables 引用有关JTables的Oracle教程

You might expect each cell in a table to be a component. 您可能希望表中的每个单元格都是一个组件。 However, for performance reasons, Swing tables are implemented differently. 但是,出于性能原因,Swing表的实现方式有所不同。

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. 当用户开始编辑单元格的数据时,单元格编辑器将接管该单元格,从而控制该单元格的编辑行为。

单个渲染器实例会根据上面的代码为每个单元格创建一个唯一的自身图像 ,每个图像都将反映该单元格的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM