来自cellrenderer视图的JTable复制值

时间:2014-08-19 12:44:17

标签: java swing jtable tablecellrenderer

我有一个带有cellrenderer的JTable,我的表格不可编辑。当我选择一个单元格并复制(ctrl-c)该值时,它的复制不是单元格中显示的,而是复制存储在模型中的值。我怎样才能复制表格中显示的单元格值。

public class TableDemo {

    public static void main(String[] args) {
        Vector<String> values = new Vector<String>();
        values.add("Val1");
        values.add("Val2");
        JTable table = new JTable(new Object[][] { { "key", values },
                { "key2", values } }, new String[] { "Col1", "Col2" });

        table.getColumnModel().getColumn(1).setCellRenderer(new MyTableCellRenderer());
        table.setCellSelectionEnabled(true);
        JFrame jf = new JFrame();
        jf.getContentPane().add(table);

        jf.setSize(500, 500);

        jf.setVisible(true);
    }
}

class MyTableCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                row, column);

        @SuppressWarnings("unchecked")
        Vector<String> values = (Vector<String>) value;
        setText(values.get(row));

        return this;
    }
}

1 个答案:

答案 0 :(得分:4)

您需要实现自己的复制命令。这是解决方案。

import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;


public class TableDemo {

    public static void main(String[] args) {
        final Vector<String> values = new Vector<String>();
        values.add("Val1");
        values.add("Val2");
        final JTable table = new JTable(new Object[][] { { "key", values },
                { "key2", values } }, new String[] { "Col1", "Col2" });

        table.getColumnModel().getColumn(1).setCellRenderer(new MyTableCellRenderer());
        table.registerKeyboardAction(new CopyAction(), KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK), JComponent.WHEN_FOCUSED);
        table.setCellSelectionEnabled(true);
        final JFrame jf = new JFrame();
        jf.getContentPane().add(table);

        jf.setSize(500, 500);

        jf.setVisible(true);
    }

    private static class CopyAction extends AbstractAction {

        /**
         * {@inheritDoc}
         */
        @Override
        public void actionPerformed(ActionEvent e) {
            final JTable tbl = (JTable) e.getSource();
            final int row = tbl.getSelectedRow();
            final int col = tbl.getSelectedColumn();
            if (row >= 0 && col >= 0) {
                final TableCellRenderer renderer = tbl.getCellRenderer(row, col);
                final Component comp = tbl.prepareRenderer(renderer, row, col);
                if (comp instanceof JLabel) {
                    final String toCopy = ((JLabel) comp).getText();
                    final StringSelection selection = new StringSelection(toCopy);
                    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                    clipboard.setContents(selection, selection);
                }
            }
        }

    }
}

@SuppressWarnings("serial")
class MyTableCellRenderer extends DefaultTableCellRenderer {
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
                row, column);

        @SuppressWarnings("unchecked")
        final
        Vector<String> values = (Vector<String>) value;
        setText(values.get(row));

        return this;
    }
}