如何在JTable中创建翻转效果?

时间:2014-11-22 08:26:59

标签: java swing jtable

在我的应用程序中,我想用JTable的行创建一个翻转效果。例如,当鼠标进入行时,该行的背景颜色将改变;相反,当鼠标退出该行时,该行的颜色将恢复正常。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

使用下面的代码,它将为您提供有关使用JTable行应用翻转效果的建议。

import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.*;

public class RollOverTable extends JTable {

    private int rollOverRowIndex = -1;

    public RollOverTable(TableModel model) {
        super(model);
        RollOverListener lst = new RollOverListener();
        addMouseMotionListener(lst);
        addMouseListener(lst);
    }

    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if( isRowSelected(row) || (row == rollOverRowIndex) ) {
            c.setForeground(getSelectionForeground());
            c.setBackground(getSelectionBackground());
        }
        else {
            c.setForeground(getForeground());
            c.setBackground(getBackground());
        }
        return c;
    }


    private class RollOverListener extends MouseInputAdapter {

        public void mouseExited(MouseEvent e) {
            rollOverRowIndex = -1;
            repaint();
        }

        public void mouseMoved(MouseEvent e) {
            int row = rowAtPoint(e.getPoint());
            if( row != rollOverRowIndex ) {
                rollOverRowIndex = row;
                repaint();
            }
        }
    }
}

答案 1 :(得分:0)

如果表格有很多行,我们使用鼠标滚轮滚动表格,上面的答案将不起作用。鼠标将在新行上,但翻转效果仍将在旧行上。

以下是我对解决问题的修改。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.*;

public class RollOverTable extends JTable {

    private Color rollOverBackground = new Color(233,239,248);
    private Color rollOverForeground = UIManager.getDefaults().getColor("windowText");

    private int rollOverRowIndex = -1;
    private boolean dispatchedEvent = false;
    private RollOverListener lst = null;
    private JScrollPane scp = null;

    public RollOverTable(TableModel model) {
        super(model);
        lst = new RollOverListener();
        addMouseMotionListener(lst);
        addMouseListener(lst);
        addMouseWheelListener(lst);
    }

    public Color getRollOverBackground() {
        return rollOverBackground;
    }

    public void setRollOverBackground(Color value) {
        rollOverBackground = value;
    }

    public Color getRollOverForeground() {
        return rollOverForeground;
    }

    public void setRollOverForeground(Color value) {
        rollOverForeground = value;
    }

    @Override
    public void addNotify() {
        super.addNotify();
        for (Component c = this; c != null; c = c.getParent())
            if (c instanceof JScrollPane) {
                scp = (JScrollPane) c;
                scp.addMouseWheelListener(new RollOverMouseWheelListener(this));
                break;    
            }
    }

    @Override
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
        Component c = super.prepareRenderer(renderer, row, column);
        if (isRowSelected(row)) {
            c.setForeground(getSelectionForeground());
            c.setBackground(getSelectionBackground());
        } else if (row == rollOverRowIndex) {
            c.setForeground(getRollOverForeground());
            c.setBackground(getRollOverBackground());
        } else {
            c.setForeground(getForeground());
            c.setBackground(getBackground());
        }
        return c;
    }

    protected void repaintRow(int row) {
        if (row == -1) return;
        int modelRow = convertRowIndexToModel(row);
        ((AbstractTableModel)getModel()).fireTableRowsUpdated(modelRow, modelRow);
    }

    private class RollOverListener extends MouseInputAdapter {

        @Override
        public void mouseExited(MouseEvent e) {
            if (rollOverRowIndex != -1) {
                repaintRow(rollOverRowIndex);
                rollOverRowIndex = -1;
            }
        }

        @Override
        public void mouseMoved(MouseEvent e) {
            int row = rowAtPoint(e.getPoint());
            if (row != rollOverRowIndex) {
                repaintRow(rollOverRowIndex);
                rollOverRowIndex = row;
                repaintRow(rollOverRowIndex);
            }
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (rollOverRowIndex != -1) {
                repaintRow(rollOverRowIndex);
                rollOverRowIndex = -1;
                dispatchedEvent = true;
                scp.dispatchEvent(SwingUtilities.convertMouseEvent(
                        e.getComponent(), e, scp));
            }
        }
    }

    private class RollOverMouseWheelListener extends MouseInputAdapter {

        private final RollOverTable table;

        public RollOverMouseWheelListener(RollOverTable table) {
            this.table = table;
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            if (dispatchedEvent) {
                dispatchedEvent = false;
                Point point = SwingUtilities.convertPoint(
                        e.getComponent(), e.getPoint(), table);
                int row = table.rowAtPoint(point);
                if (rollOverRowIndex != row) {
                    rollOverRowIndex = row;
                    table.repaintRow(rollOverRowIndex);
                }
            }
        }
    }

}