2

按照这里的垃圾神出色的例子,我整理了一个小演示,它以一种可能令人费解的方式完成了一项简单的任务。下面显示的 GUI 显示一列代表真/假值的图标。如果您单击一个图标,它会将值更改为与之相反的值。很像一个复选框,但外观不同,而且可扩展性更强(例如,我将来可以将其更改为循环遍历十几个符号,而不仅仅是两个布尔符号)。

在此处输入图像描述

我通过使用自定义编辑器来做到这一点,该编辑器是 JComponent 的虚拟扩展。不过,您甚至都看不到这个虚拟组件,因为一旦它接收到一个MousePressed事件,它就会导致编辑器fireEditingStopped(). 它工作得很好,除了我发现的一个奇怪的错误。如果您单击一个符号来更改它,然后将鼠标移动到屏幕上的其他位置并按下键盘键,它会在最后一个单击的单元格中弹出虚拟编辑器(这实际上是空白单元格),它会一直停留在那里,直到您将鼠标移入单元格或单击其他单元格。

作为对此的 hacky 修复,我在渲染器中添加了一行,它在渲染后总是取消选择整个表格。这很好用,我已经验证了整个表确实被取消了。但是,尽管如此,如果您按下键盘键,它仍会为最后编辑的单元格执行编辑器。如何防止这种行为?我的应用程序中有其他键盘侦听器,如果未选择任何单元格,我认为不应执行任何编辑器。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.imageio.ImageIO;
import javax.swing.AbstractCellEditor;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class JTableBooleanIcons {

    private JFrame frame;
    private DefaultTableModel tableModel;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JTableBooleanIcons window = new JTableBooleanIcons();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JTableBooleanIcons() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tableModel = new DefaultTableModel(new Object[]{"Words", "Pictures"},0);
        table = new JTable(tableModel);
        table.setRowHeight(40);
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});

        frame.getContentPane().add(table, BorderLayout.CENTER);

        table.getColumn("Pictures").setCellRenderer(new BooleanIconRenderer());
        table.getColumn("Pictures").setCellEditor(new BooleanIconEditor());

    }

    @SuppressWarnings("serial")
    private class BooleanIconRenderer extends DefaultTableCellRenderer implements TableCellRenderer {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                                                                        boolean hasFocus, int row, int col) {
            String iconFilename = null;
            if ((boolean) value) {
                iconFilename = "yes.png";
                value = true;
            } else {
                iconFilename = "no.png";
                value = false;
            }
            try {
                setIcon(new ImageIcon(ImageIO.read(BooleanIconRenderer.class.getResourceAsStream(iconFilename))));
            } catch (Exception e) {
                System.err.println("Failed to load the icon.");
            }
            if (isSelected) {
                this.setBackground(Color.white);
            } else {
                this.setBackground(Color.white);
            }
            table.getSelectionModel().clearSelection();
            return this;
        }
    }

    @SuppressWarnings("serial")
    private class BooleanIconEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {

        private BooleanComponent boolComp;

        public BooleanIconEditor() {
            boolComp = new BooleanComponent(false);
            boolComp.addMouseListener(this);
        }

        @Override
        public Object getCellEditorValue() {
            return boolComp.getValue();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            boolComp.setValue(! (boolean) value);
            return boolComp;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            this.fireEditingStopped();

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            this.fireEditingStopped();
        }

    }

    @SuppressWarnings("serial")
    private class BooleanComponent extends JComponent {
        private boolean value;

        public BooleanComponent(boolean value) {
            this.value = value;
        }

        public boolean getValue() {
            return value;
        }

        public void setValue(boolean value) {
            this.value = value;
        }
    }
}
4

2 回答 2

5

您的问题的原因是您实际上没有在编辑器中渲染任何内容。您依赖于用户只会“单击”单元格以更改其值的事实。我认为这有点短视,但是,这是你的程序。

要立即修复它,只需添加...

@Override
public boolean isCellEditable(EventObject e) {
    return (e instanceof MouseEvent);
}

给你BooleanIconEditor

在旁注中。

在您的单元格渲染器中,您不应该加载图像。这些应该作为构造函数的一部分预先缓存,或者更好的是,作为static字段变量。您可能正在这样做,但以防万一。

更新

当我谈到这个话题时。您应该避免从渲染器中更改表格的状态。这真的很不安全,并且可能会导致您陷入无限循环,因为表格会一次又一次地尝试重新渲染您所做的更改......

如果您真的想隐藏选择(我不确定您为什么要隐藏),您可以设置表格的选择以匹配表格背景颜色或使其成为透明颜色。不要忘记更改选择前景;)

更新#2

键盘支持的例子;) - 无法抗拒......

public class JTableBooleanIcons {

    private JFrame frame;
    private DefaultTableModel tableModel;
    private JTable table;
    private ImageIcon yesIcon;
    private ImageIcon noIcon;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JTableBooleanIcons window = new JTableBooleanIcons();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public JTableBooleanIcons() {
        try {
            yesIcon = (new ImageIcon(ImageIO.read(BooleanIconRenderer.class.getResourceAsStream("/yes.png"))));
            noIcon = (new ImageIcon(ImageIO.read(BooleanIconRenderer.class.getResourceAsStream("/no.png"))));
        } catch (Exception e) {
            e.printStackTrace();
        }
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        tableModel = new DefaultTableModel(new Object[]{"Words", "Pictures"}, 0);
        table = new JTable(tableModel);
        table.setRowHeight(40);
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});
        tableModel.addRow(new Object[]{"click icon to change", false});
        tableModel.addRow(new Object[]{"click icon to change", true});

        frame.getContentPane().add(table, BorderLayout.CENTER);

        table.getColumn("Pictures").setCellRenderer(new BooleanIconRenderer());
        table.getColumn("Pictures").setCellEditor(new BooleanIconEditor());

    }

    @SuppressWarnings("serial")
    private class BooleanIconRenderer extends DefaultTableCellRenderer implements TableCellRenderer {

        public BooleanIconRenderer() {
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int col) {
            super.getTableCellRendererComponent(table, null, isSelected, hasFocus, row, col);
            if ((boolean) value) {
                setIcon(yesIcon);
            } else {
                setIcon(noIcon);
            }
            return this;
        }
    }

    @SuppressWarnings("serial")
    private class BooleanIconEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {

        private BooleanComponent boolComp;
        private boolean isMouseEvent;

        public BooleanIconEditor() {
            boolComp = new BooleanComponent(false);
            boolComp.addMouseListener(this);
            InputMap im = boolComp.getInputMap(JComponent.WHEN_FOCUSED);
            ActionMap am = boolComp.getActionMap();

            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "click");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "click");

            am.put("click", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Clicked");
                    boolComp.setValue(!boolComp.getValue());
                }
            });

        }

        @Override
        public boolean isCellEditable(EventObject e) {
            isMouseEvent = e instanceof MouseEvent;
            return true; //(e instanceof MouseEvent);
        }

        @Override
        public Object getCellEditorValue() {
            return boolComp.getValue();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            boolean state = (boolean) value;
            if (isMouseEvent) {
                state = !state;
            }
            boolComp.setValue(state);
            boolComp.setOpaque(isSelected);
            boolComp.setBackground(table.getSelectionBackground());
            return boolComp;
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mousePressed(MouseEvent e) {
            this.fireEditingStopped();

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            this.fireEditingStopped();
        }

        @Override
        public void mouseExited(MouseEvent e) {
            this.fireEditingStopped();
        }
    }

    @SuppressWarnings("serial")
    private class BooleanComponent extends JLabel {

        private boolean value;

        public BooleanComponent(boolean value) {
            this.value = value;
        }

        public boolean getValue() {
            return value;
        }

        public void setValue(boolean value) {
            this.value = value;
            if (value) {
                setIcon(yesIcon);
            } else{
                setIcon(noIcon);
            }
        }
    }
}
于 2013-01-23T00:32:26.433 回答
4

虽然@Mad 的回答显示了如何实现鼠标处理和键绑定,但另一种方法是使用JToggleButton、 的父级JCheckBox和合适的Icon. 切换按钮已经知道如何显示选择Icon和处理事件。修改后ValueRenderer如下图;ValueEditor不变;文本是可选的。

图片

private static class ValueRenderer extends JToggleButton
    implements TableCellRenderer {

    private static final Color hilite = new Color(0xE8E8E8);
    private static final Icon YES = UIManager.getIcon("InternalFrame.maximizeIcon");
    private static final Icon NO = UIManager.getIcon("InternalFrame.closeIcon");

    public ValueRenderer() {
        this.setOpaque(true);
        this.setIcon(NO);
        this.setSelectedIcon(YES);
    }
    ...
}
于 2013-01-23T11:19:06.600 回答