当鼠标进入时,如何改变一行中所有jtable列的颜色?

ux6nzvsh  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(520)

当鼠标在表格中输入一行时,我希望该行中的每一列颜色都变为蓝色。
我这样做了,它改变了,但它只改变时,我按下我的鼠标,而不是当我的鼠标进入它。

private void changeColorTableCell(JTable tbl, int index) {
    tbl.getColumnModel().getColumn(index).setCellRenderer(new DefaultTableCellRenderer(){
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            final Component c =  super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 
            c.setBackground(Color.BLUE);
            return c;
        }      
    });
}

private void tabelPesananMouseEntered(java.awt.event.MouseEvent evt) {                                          

    int count = tabelPesanan.getColumnModel().getColumnCount();
    for(int i = 0; i < count; i++) {
        changeColorTableCell(tabelPesanan, i);
    }        
}

如何改变一切 JTable 鼠标输入时列的颜色?

bz4sfanl

bz4sfanl1#

getColumn(index).setCellRenderer(…) 不起作用。如果要更改整行的颜色,不能只更改一列的渲染器。
虽然可以设置默认渲染器,也可以设置每列的单元格渲染器,但更好的选择是覆盖preparerenderer,这样除了背景色之外,其他任何渲染器都不会受到影响。

private int hoveredRow = -1;

// ...

    JTable tabelPesanan = new JTable() {
        private static final long serialVersionUID = 1;

        @Override
        public Component prepareRenderer(TableCellRenderer renderer,
                                         int row,
                                         int column) {
            Component component =
                super.prepareRenderer(renderer, row, column);

            if (row == hoveredRow) {
                component.setBackground(Color.BLUE);
            } else if (isCellSelected(row, column)) {
                component.setBackground(getSelectionBackground());
            } else {
                component.setBackground(getBackground());
            }

            return component;
        }
    };

    MouseInputListener hoveredRowUpdater = new MouseInputAdapter() {
        private void repaintRow(int row) {
            if (row >= 0) {
                Rectangle bounds = tabelPesanan.getCellRect(row, 0, true);
                int width = tabelPesanan.getWidth();
                tabelPesanan.repaint(0, bounds.y, width, bounds.height);
            }
        }

        private void updateHoveredRowFrom(MouseEvent event) {
            int oldHoveredRow = hoveredRow;
            hoveredRow = tabelPesanan.rowAtPoint(event.getPoint());
            repaintRow(oldHoveredRow);
            repaintRow(hoveredRow);
        }

        @Override
        public void mouseEntered(MouseEvent event) {
            updateHoveredRowFrom(event);
        }

        @Override
        public void mouseExited(MouseEvent event) {
            updateHoveredRowFrom(event);
        }

        @Override
        public void mouseMoved(MouseEvent event) {
            updateHoveredRowFrom(event);
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            updateHoveredRowFrom(event);
        }
    };

    tabelPesanan.addMouseListener(hoveredRowUpdater);
    tabelPesanan.addMouseMotionListener(hoveredRowUpdater);

相关问题