且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JTable Renderer上获得完整的突出显示(带有边框)

更新时间:2022-03-22 23:33:41

您也许可以使用UIManager.参见 UIManager默认值. "Table.focusCellHighlightBorder"似乎是您想要的属性.

You might be able to use the UIManager. See UIManager Defaults. "Table.focusCellHighlightBorder" would appear to be the property you want.

由原始海报添加:

这是我根据camickr的信息提出的解决方案.优化/清理.

Here is the solution I came up with based on camickr's info. Optimizations/cleanup welcome.

  1. 设置静态边框,以便在任何需要的地方都可以使用(我将它们放在名为"UiUtils"的类中):

  1. Set up static borders so they are available wherever you need them (I put them in a class called "UiUtils"):

public static final Border focusedCellBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
public static final Border unfocusedCellBorder = createEmptyBorder();
private static Border createEmptyBorder()
{
    Insets i = focusedCellBorder.getBorderInsets(new JLabel());
    return BorderFactory.createEmptyBorder(i.top, i.left, i.bottom, i.right);
}

  • 渲染器

  • Renderer

    @Override public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column)
    {
        // [... set component values here ...]
        label.setBorder(hasFocus ? UiUtils.focusedCellBorder : UiUtils.unfocusedCellBorder);
        return label;
    }