简体   繁体   English

在具有自定义行背景颜色的JTable中使用intercellSpacing会导致意外结果

[英]Using intercellSpacing in a JTable with a custom row background color causes unexpected results

I have a JTable where I want to highlight some rows using a custom background-color. 我有一个JTable,我想使用自定义背景颜色突出显示一些行。 This I have done with the following class: 我已经完成了以下课程:

public class MyTable extends JTable {

    private List<RefData> data = null;

    public List<RefData> getData() {
        return data;
    }

    public void setData(List<RefData> data) {
        this.data = data;
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component comp = super.prepareRenderer(renderer, row, column);

        if (this.data == null || row < 0 || row > this.data.size()-1){
            return comp;
        }

        RefData rowData = this.data.get(row);
        if (rowData.getStatus() < 3000){
            comp.setBackground(Color.YELLOW);
        } else {
            comp.setBackground(Color.WHITE);
        }

        return comp;
    }

}

All this works like a charm, and I get exactly what I want. 所有这一切都像一个魅力,我得到了我想要的。 Next, while looking at the resulting GUI, I realise that the table looks way too condensed. 接下来,在查看生成的GUI时,我意识到该表看起来过于浓缩。 Everything looks squished together. 一切看起来都挤得水泄不通。 As always with the default JTable settings ;) 与使用默认的JTable设置一样;)

Well, that's easily solved I thought: 嗯,这很容易解决我想:

myTable.setIntercellSpacing(new java.awt.Dimension(10, 1));

Now, the cells are nicely spaced but , the added cell margins are now in the default table background color, which in my case is white. 现在,单元格间隔很好但是 ,添加的单元格边距现在是默认的表格背景颜色,在我的情况下是白色。 This looks butt-ugly. 这看起来很丑陋。

I assume that the intercell spacing adds spacing between the cell border and the component returned by prepareRenderer. 我假设单元间距增加了单元格边框和prepareRenderer返回的组件之间的间距。 This would explain the result. 这可以解释结果。 But how can I get it to change the background of the cell itself? 但是我怎样才能让它改变细胞本身的背景呢?

Is my prepareRenderer solution is not suited for this task? 我的prepareRenderer解决方案不适合这项任务吗? Or is there another solution? 还是有其他解决方案吗?

The Border approach is correct, but it is better to do it the cell renderer, not the prepareRenderer() method: Border方法是正确的,但最好是单元格渲染器,而不是prepareRenderer()方法:

    JTable.setDefaultRenderer(Object.class, new DefaultCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            JComponent component = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            component.setBorder(...);
            return component;
        }
    });

Can someone please confirm that this is the correct way of doing it in Swing, please? 有人可以确认这是在Swing中这样做的正确方法吗?

Overriding prepareRenderer() has the advantage of applying to all cells, and "the internal implementations always use this method to prepare renderers so that this default behavior can be safely overridden by a subclass." 覆盖prepareRenderer()具有应用于所有单元格的优势,“内部实现总是使用此方法来准备渲染器,以便子类可以安全地覆盖此默认行为。” I don't see a problem, even if you later use setDefaultRenderer() for a particular class. 我没有看到任何问题,即使您稍后将setDefaultRenderer()用于特定类。

Addendum: Examples may be found in the article Table Row Rendering and this related answer . 附录:示例可以在文章表格渲染和相关答案中找到

I now came up with the following solution. 我现在想出了以下解决方案。 It seems OK to me. 对我来说似乎没问题。 But can someone please confirm that this is the correct (or the right way) of doing it in wing please? 但有人可以确认这是正确的(或正确的方式)在翼中做吗?

public class MyTable extends JTable {

    // [snip]

    private Border paddingBorder = BorderFactory.createEmptyBorder(2, 3, 2, 3);

    // [snip]

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component comp = super.prepareRenderer(renderer, row, column);

        if (JComponent.class.isInstance(comp)){
            ((JComponent)comp).setBorder(paddingBorder);
        }

        // [snip]
    }

    // [snip]

}

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

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