简体   繁体   English

每当在Jtable中选择新单元格时,如何更改单元格的边框?

[英]How to change cell's border whenever new cell is selected in jtable?

I want to change border of cell whenever it is selected regardless by mouse or by keyboard. 我想无论何时通过鼠标或键盘更改单元格的边框。 It is hard to find smth on net. 很难在网上找到水烟。 I tried to use ListSelectionListener but this doesn't work. 我尝试使用ListSelectionListener,但这不起作用。

If you know some good way to change cell's border, please, reply. 如果您知道更改单元格边框的一些好方法,请回复。 I welcome any ideas. 我欢迎任何想法。

Thank you! 谢谢!

Use a customized TableCellRenderer to do something different when the cell is selected. 选择单元格时,请使用自定义的TableCellRenderer进行其他操作。

http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#renderer

Looking at the code from the above example you can see how you would need to look at the isSelected boolean parameter. 查看上面示例中的代码,您可以看到需要如何查看isSelected布尔参数。

 public Component getTableCellRendererComponent(
                        JTable table, Object color,
                        boolean isSelected, boolean hasFocus,
                        int row, int column) {
    Color newColor = (Color)color;
    setBackground(newColor);
    if (isBordered) {
        if (isSelected) {
            ...
            //selectedBorder is a solid border in the color
            //table.getSelectionBackground().
            setBorder(selectedBorder);
        } else {
            ...
            //unselectedBorder is a solid border in the color
            //table.getBackground().
            setBorder(unselectedBorder);
        }
    }

However, in your implementation just extend DefaultTableCellRenderer and call super() version of getTableCellRendererComponent first and just change the cell color. 但是,在您的实现中,只需扩展DefaultTableCellRenderer并首先调用getTableCellRendererComponent super()版本,然后更改单元格颜色即可。

This is the default behaviour. 这是默认行为。 The cell border is set based on the Table.focusCellHighlightBorder property of the table. 基于表格的Table.focusCellHighlightBorder属性设置单元格边框。 So you can change the default Border by using the UIManager. 因此,您可以使用UIManager更改默认边框。 See UIManager Defaults for more information. 有关更多信息,请参见UIManager默认值

If for some reason this doesn't meet your requirements then I would check out Table Row Renderering which will allow you do this in one place instead of creating a custom renderer for every data type in your table. 如果由于某种原因这不能满足您的要求,那么我将签出Table Row Renderering ,它可以让您在一个地方进行此操作,而不是为表中的每种数据类型创建自定义渲染器。

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

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