【问题标题】:JTable: Remove border around cell when clearing row selectionJTable:清除行选择时删除单元格周围的边框
【发布时间】:2017-04-16 14:06:50
【问题描述】:

我有一个JTable 并希望允许通过单击表格的空白部分来取消选择所有行。到目前为止,这工作正常。但是,即使我调用table.clearSelection();,表格仍然会在先前启用的单元格周围显示边框(请参见示例中的单元格 5):

我也想去掉这个边框(它在 Mac 的原生外观上看起来特别不合适,单元格突然变黑)。

完全工作的最小示例代码:

public class JTableDeselect extends JFrame {
    public JTableDeselect() {
        Object rowData[][] = { { "1", "2", "3" }, { "4", "5", "6" } };
        Object columnNames[] = { "One", "Two", "Three" };
        JTable table = new JTable(rowData, columnNames);
        table.setFillsViewportHeight(true);
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (table.rowAtPoint(e.getPoint()) == -1) {
                    table.clearSelection();
                }
            }
        });
        add(new JScrollPane(table));
        setSize(300, 150);
    }
    public static void main(String args[]) throws Exception {
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        new JTableDeselect().setVisible(true);
    }
}

[编辑] 尝试添加这里提到的table.getColumnModel().getSelectionModel().clearSelection();。但这也无济于事。

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    您的问题:即使选择丢失,您的表格单元格仍然具有焦点,因此它通过显示加粗边框来绘制自身以显示这一点。会心 一种可能的解决方案是创建自己的渲染器,当单元格失去选择时移除单元格的焦点。例如:

    table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value,
                boolean isSelected, boolean hasFocus, int row, int column) {
            if (!isSelected) {
                hasFocus = false;
            }
            return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        }
    });
    

    【讨论】:

    • 谢谢!简单有效!
    • @qqilihq,这假设表中的所有数据都使用相同的渲染器。对于更通用的解决方案,除了清除选择之外,您还需要重置选择模型的锚/引导索引。那么你就不需要自定义渲染器了。
    • @camickr 太好了。这种方法更加一致。它还解决了这种情况,即在取消选择后按下一个键将使用上述解决方案跳回到先前选择的单元格。我已将“已接受”标签切换为您的答案,因为它更完整和通用。无论如何,谢谢你的初始指针,气垫船!
    • @qqilihq:您将接受的答案更改为 camickr 是合适的。
    【解决方案2】:

    尝试添加table.getColumnModel().getSelectionModel().clearSelection();

    table.clearSelection() 方法调用该方法和TableColumnModelclearSelection() 方法。

    除了清除选择之外,您还需要重置选择模型的“anchor and lead”索引:

    table.clearSelection();
    
    ListSelectionModel selectionModel = table.getSelectionModel();
    selectionModel.setAnchorSelectionIndex(-1);
    selectionModel.setLeadSelectionIndex(-1);
    
    TableColumnModel columnModel = table.getColumnModel();
    columnModel.getSelectionModel().setAnchorSelectionIndex(-1);
    columnModel.getSelectionModel().setLeadSelectionIndex(-1);
    

    现在,如果您使用箭头键,焦点将转到 (0, 0),因此您确实会丢失有关上次单击的单元格的信息。

    如果您只清除选择模型,那么您将丢失行信息但会保留列信息。

    尝试清除一个或两个模型以获得您想要的效果。

    【讨论】:

      猜你喜欢
      • 2011-03-11
      • 2015-12-31
      • 2016-05-20
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2015-11-23
      相关资源
      最近更新 更多