JTable在鼠标点击时更改单元格背景 - 发布后更改背景?

时间:2011-08-31 19:07:23

标签: java swing jtable

我的问题是,如何解决以下问题:

如果我点击JTable中的一个单元格,我想改变它的背景。如果我释放鼠标按钮,我希望背景更改回正常颜色。

这可能吗?

问候ayk

3 个答案:

答案 0 :(得分:5)

要在视觉领域进行,你需要

  • 一个MouseListener,它在按下的单元格坐标中设置一些状态,并重置已释放的状态
  • 一个PropertyChangeListener侦听到该状态,该状态在更改时重新绘制单元格
  • 自定义渲染器,为标记的单元格设置自定义背景

听众的一些代码

    MouseListener l = new MouseAdapter() {

        /** 
         * @inherited <p>
         */
        @Override
        public void mousePressed(MouseEvent e) {
            JTable table = (JTable) e.getComponent();
            int col = table.columnAtPoint(e.getPoint());
            int row = table.rowAtPoint(e.getPoint());
            if (col < 0 || row < 0) {
                table.putClientProperty("pressedCell", null);
            } else {
                table.putClientProperty("pressedCell", new Point(col, row));
            }
        }

        /** 
         * @inherited <p>
         */
        @Override
        public void mouseReleased(MouseEvent e) {
            ((JTable) e.getComponent()).putClientProperty("pressedCell", null);
        }

    };
    table.addMouseListener(l);
    PropertyChangeListener property = new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            JTable table = (JTable) evt.getSource();
            Point cell = evt.getNewValue() != null ? 
                    (Point) evt.getNewValue() : (Point) evt.getOldValue();
            if (cell != null) table.repaint(table.getCellRect(cell.y, cell.x, false));        
        }

    };
    table.addPropertyChangeListener("pressedCell", property);

在SwingX中突出显示单元格(无法抵抗:-)

    HighlightPredicate predicate = new HighlightPredicate() {

        @Override
        public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
            Point p = (Point) adapter.getComponent().getClientProperty("pressedCell");
            return p != null && p.x == adapter.column && p.y == adapter.row;

        }
    };
    table.addHighlighter(new ColorHighlighter(predicate, Color.YELLOW, null, Color.YELLOW, null));

对于核心Swing,要么实现自定义TableCellRenderer,要么子类化JTable并覆盖prepareRenderer以根据单元格标志设置背景颜色,如Rob的Table Row Rendering

中所述。

答案 1 :(得分:4)

在您的渲染器中

,您必须覆盖hasFocus

例如

enter image description here

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableRenderer extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;
    private String[] columnNames = {"Date", "String", "Centered", "Integer", "Boolean"};
    private Object[][] data = {
        {new Date(), "A", "A", new Integer(1), true},
        {new Date(), "B", "B", new Integer(2), false},
        {new Date(), "C", "C", new Integer(10), null},
        {new Date(), "D", "D", new Integer(4), false}
    };

    public TableRenderer() {
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;
            //  Returning the Class of each column will allow different
            //  renderers to be used based on Class

            @Override
            public Class getColumnClass(int column) {
                return getValueAt(0, column).getClass();
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);

        //  Override default renderer on a specific Class
        TableCellRenderer colorRenderer = new ColorRenderer();
        table.setDefaultRenderer(String.class, colorRenderer);

        //  Override default renderer for a specific column
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        table.getColumnModel().getColumn(2).setCellRenderer(centerRenderer);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableRenderer frame = new TableRenderer();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    /*
     **  Color the focused cell
     */
    private class ColorRenderer extends DefaultTableCellRenderer {

        private static final long serialVersionUID = 1L;

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            if (hasFocus) {
                setBackground(Color.cyan);
            } else if (isSelected) {
                setBackground(table.getSelectionBackground());
            } else {
                setBackground(table.getBackground());
            }
            return this;
        }
    }
}

答案 2 :(得分:1)

是的,这是可能的。 使用自定义ListSelectionModel并将您的JTable设置为使用:setSelectionModel()

这是一个example