简体   繁体   English

在JTable中禁用单元格轮廓

[英]Disabling cell outline in JTable

I'm using a JTable to show some data. 我正在使用JTable来显示一些数据。 The user can only select entire rows of the JTable , not individual cells. 用户只能选择JTable整个行,而不能选择单个单元格。 Here's the code used to allow only rows selection: 以下是用于仅允许选择行的代码:

jTable1.setCellSelectionEnabled(false);
jTable1.setColumnSelectionEnabled(false);
jTable1.setRowSelectionAllowed(true);
jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

But when a user selects a row the cell in that row gets outlined (first column / last row in the image below): 但是,当用户选择一行时,该行中的单元格将被概述(下图中的第一列/最后一行):

在此处输入图片说明

How Can I disable this outline? 如何禁用此轮廓?

You could simply extend the DefaultTableCellRenderer and pretend, from the UI's side, that the cell isn't "focused". 您可以简单地扩展DefaultTableCellRenderer并从UI的角度假装该单元格不是“集中”的。

I removed the border by using the following renderer: 我使用以下渲染器删除了边框:

private static class BorderLessTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = 1L;

    public Component getTableCellRendererComponent(
            final JTable table,
            final Object value,
            final boolean isSelected,
            final boolean hasFocus,
            final int row,
            final int col) {

        final boolean showFocusedCellBorder = false; // change this to see the behavior change

        final Component c = super.getTableCellRendererComponent(
                table,
                value,
                isSelected,
                showFocusedCellBorder && hasFocus, // shall obviously always evaluate to false in this example
                row,
                col
        );
        return c;
    }
}

You can set it on your JTable like this: 您可以像这样在JTable上进行设置:

table.setDefaultRenderer( Object.class, new BorderLessTableCellRenderer() );

or, for Strings: 或者,对于字符串:

table.setDefaultRenderer( String.class, new BorderLessTableCellRenderer() );

It's a bit of an hack in that it's simply reusing the original renderer and pretending that the focused/selected cell isn't but it should get you started. 这有点hacker,因为它只是重复使用原始渲染器,并假装没有聚焦/选择的单元格,但是应该可以使您入门。

I just found a very simple trick to alter this behaviour. 我只是发现了一个非常简单的技巧来改变这种行为。 It turns out that cell focusing is an extension of the tables focus. 事实证明,单元格焦点是表焦点的扩展。 If the table can not be focused on neither can individual cells (though row selections still show up). 如果表格不能集中在单个单元格上(尽管仍然显示行选择)。

All you need is one simple line of code: 您只需要简单的一行代码即可:

jTable1.setFocusable(false);

Here is a picture from a project of mine that implements this same behaviour (Note: I am using windows LookAndFeel) 这是我的项目中实现相同行为的图片(注意:我使用的是Windows LookAndFeel)

不可聚焦细胞

Regardless of which column you click under the entire row is selected but the cell you clicked is not focused. 无论您单击整行下方的哪一列,都会被选中,但是您单击的单元格不会被聚焦。 I hope it helps! 希望对您有所帮助! :) :)

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

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