0

这是一个简单的例子,当鼠标进入/退出时,我的 JTable 的内部 JPanels 没有被通知。为什么?

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import net.miginfocom.layout.CC;
import net.miginfocom.swing.MigLayout;


public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new MigLayout("debug"));

        // JTABLE
        String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};
        Object[][] data = {
                {"Kathy", "Smith",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
            };


        JTable table = new JTable(data, columnNames);
        table.setDefaultRenderer(String.class, new TableCellRenderer() {

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                JPanel panel = new JPanel(new MigLayout("fill"));

                JLabel label = new JLabel(value.toString());
                label.setFont(new Font("SansSerif", Font.PLAIN, 13));
                label.setHorizontalAlignment(JLabel.CENTER);
                panel.add(label, new CC().growY());

                final JLabel close = new JLabel(" x ");
                close.setHorizontalAlignment(JLabel.CENTER);
                close.setVerticalAlignment(JLabel.CENTER);
                panel.add(close, new CC().growY().alignX("right"));

                panel.addMouseListener(new MouseAdapter() {

                    @Override
                    public void mouseEntered(MouseEvent event) {
                        System.out.println("CALLED");
                        close.setForeground(Color.red);
                    }

                    @Override
                    public void mouseExited(MouseEvent event) {
                        System.out.println("GRR");
                        close.setForeground(Color.black);
                    }

                });

                return panel;
            }

        });

        panel.add(table, new CC().width("50%"));

        frame.setContentPane(panel);

        frame.pack();
        frame.setVisible(true);

    }

}
4

1 回答 1

2

渲染器组件旨在为每个单元重用,以提高性能。所以 make panel, label, 和closefields 并且只是 do label.setText(String,valueOf(value))

然后将鼠标(运动)侦听器添加到组件通常没有多大意义。而是将它们添加到 JTable 并使用columnAtPoint、 rowAtPoint。

于 2012-12-03T00:03:28.490 回答