每当在Jtable中选择新单元格时

每当在Jtable中选择新单元格时

本文介绍了每当在Jtable中选择新单元格时,如何更改单元格的边框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想无论何时通过鼠标或键盘更改单元格的边框.很难在网上找到水烟.我尝试使用ListSelectionListener,但这不起作用.

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.

如果您知道更改单元格边界的一些好方法,请回复.我欢迎任何想法.

If you know some good way to change cell's border, please, reply.I welcome any ideas.

谢谢!

推荐答案

在选定单元格时,使用自定义的TableCellRenderer可以做一些不同的事情.

Use a customized TableCellRenderer to do something different when the cell is selected.

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

查看上面示例中的代码,您将看到需要如何查看isSelected布尔参数.

Looking at the code from the above example you can see how you would need to look at the isSelected boolean parameter.

 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);
        }
    }

但是,在您的实现中,只需扩展DefaultTableCellRenderer并首先调用getTableCellRendererComponent的super()版本,然后更改单元格颜色即可.

However, in your implementation just extend DefaultTableCellRenderer and call super() version of getTableCellRendererComponent first and just change the cell color.

这篇关于每当在Jtable中选择新单元格时,如何更改单元格的边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:21