我正在使用TableCellEditor:

    package gui.table;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.EventObject;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;

import org.jdom2.Document;
import org.jdom2.Element;

import event.TableClickEvent;
import event.TestController;
import gui.DaimlerColor;
import gui.MainWindow;

@SuppressWarnings("serial")
public class TestcaseCellEditor extends TestcasePanel implements TableCellEditor {

    int row;
    List<String> data;
    TestController tgc;
    transient protected TableClickEvent changeEvent = null;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public TestcaseCellEditor(final JTable table, MainWindow gui, List<String> data, TestController tgc) {

        super();
        super.gui = gui;
        this.data = data;
        this.tgc = tgc;

        MouseListener ml = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {

                ButtonModel m = ((JButton) e.getSource()).getModel();
                setRow(table.getEditingRow());
                if (m.isPressed() && table.isRowSelected(table.getEditingRow()) && e.isControlDown()) {
                    setBackground(DaimlerColor.LIGHT_BLUE);
                }
            }

        };
        playButton.addMouseListener(ml);

        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireEditingStopped();
            }
        });

        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {

                setRow(table.getEditingRow());
                fireEditingCanceled();

            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

        this.setToolTipText("Click play to rerun this Testcase");
        this.setName(data.get(row));

        setPlaybuttonIcon(gui.getTestcasetableModelIcon(row));

        setClickable(tgc.clickable);

        if (row <= gui.currentTestcase) {
            Document doc = tgc.getDoc();
            List<Element> cases = doc.getRootElement().getChildren();
            List<Element> teststeps = cases.get(row).getChildren();
            List<String> attributes = new LinkedList<String>();
            for (Element teststep : teststeps) {

                try {
                    if (teststep.getChildren("result").get(gui.sutPaths.getSelectedIndex()).getAttributeValue("status") != null) {
                        attributes.add(teststep.getChildren("result").get(gui.sutPaths.getSelectedIndex())
                                .getAttributeValue("status"));
                    }

                    else {
                        attributes.add("empty");
                    }
                } catch (Exception e) {
                    attributes.add("empty");
                }

            }

            if (attributes.contains("empty")) {
                setButton("");
            } else if (attributes.contains("failed")) {

                setButton("failed");

            } else {
                setButton("passed");
            }

        } else {
            setButton("");
        }

        this.setBorder(BorderFactory.createEmptyBorder());
        this.setBackground(isSelected ? DaimlerColor.LIGHT_BLUE : (row % 2 == 1 ? DaimlerColor.DARK_WHITE
                : DaimlerColor.WHITE));

        System.out.println("CLCIKED");

        return this;

    }

    @Override
    public Object getCellEditorValue() {
        return "";
    }

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

    @Override
    public boolean shouldSelectCell(EventObject anEvent) {
        return true;
    }

    @Override
    public boolean stopCellEditing() {
        return true;
    }

    @Override
    public void cancelCellEditing() {
        fireEditingCanceled();

    }

    @Override
    public void addCellEditorListener(CellEditorListener l) {
        listenerList.add(CellEditorListener.class, l);
    }

    @Override
    public void removeCellEditorListener(CellEditorListener l) {
        listenerList.remove(CellEditorListener.class, l);
    }

    public CellEditorListener[] getCellEditorListeners() {
        return listenerList.getListeners(CellEditorListener.class);
    }

    protected void fireEditingStopped() {

        Object[] listeners = listenerList.getListenerList();

        for (int i = listeners.length - 2; i >= 0; i -= 2) {

            if (listeners[i] == CellEditorListener.class) {

                changeEvent = new TableClickEvent(this.playButton, getRow());
                ((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
            }
        }
    }

    protected void fireEditingCanceled() {

        Object[] listeners = listenerList.getListenerList();

        for (int i = listeners.length - 2; i >= 0; i -= 2) {

            if (listeners[i] == CellEditorListener.class) {

                changeEvent = new TableClickEvent(this, getRow());
                ((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
            }
        }
    }
}


和一个TableCellrenderer:

    package gui.table;

import event.TestController;
import gui.DaimlerColor;
import gui.MainWindow;

import java.awt.Component;
import java.util.LinkedList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import org.jdom2.Document;
import org.jdom2.Element;

@SuppressWarnings("serial")
public class TestcaseCellRenderer extends TestcasePanel implements TableCellRenderer {

    public List<String> data;
    MainWindow gui = null;
    public TestController tgc = null;

    public TestcaseCellRenderer() {
        super();
        setName("Table.cellRenderer");
    }

    public TestcaseCellRenderer(List<String> names, MainWindow gui2, TestController tgc) {
        super();
        this.data = names;
        this.gui = gui2;
        this.tgc = tgc;
        setName("Table.cellRenderer");
    }

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

        this.setToolTipText("Click play to rerun this Testcase");
        System.out.println("Rendering your ass!");
        this.setName(data.get(row));
        setPlaybuttonIcon(gui.getTestcasetableModelIcon(row));
        setClickable(tgc.clickable);

        if (row <= gui.currentTestcase) {
            Document doc = tgc.getDoc();
            List<Element> cases = doc.getRootElement().getChildren();
            List<Element> teststeps = cases.get(row).getChildren();
            List<String> attributes = new LinkedList<String>();
            for (Element teststep : teststeps) {

                try {
                    if (teststep.getChildren("result").get(gui.sutPaths.getSelectedIndex()).getAttributeValue("status") != null) {
                        attributes.add(teststep.getChildren("result").get(gui.sutPaths.getSelectedIndex())
                                .getAttributeValue("status"));
                    }

                    else {
                        attributes.add("empty");
                    }
                } catch (Exception e) {
                    attributes.add("empty");
                }

            }

            if (attributes.contains("empty")) {
                setButton("");
            } else if (attributes.contains("failed")) {

                setButton("failed");

            } else {
                setButton("passed");
            }

        } else {
            setButton("");
        }

        if (tgc.isRerunning() && tgc.getEventRow() == row) {
            setButton("");
        }

        this.setBorder(BorderFactory.createEmptyBorder());
        this.setBackground(isSelected ? DaimlerColor.LIGHT_BLUE : (row % 2 == 1 ? DaimlerColor.DARK_WHITE
                : DaimlerColor.WHITE));

        return this;
    }

}


以某种方式,如果我单击表中的文本,它只会设置第二次单击的焦点?它也仅每第二次触发一次fireEdizingCanceled事件吗?这是为什么?

感谢您的任何想法。

最佳答案

不知何故,如果我单击表格中的文本,只会设置焦点
每秒点击一次?这是为什么?



正确,这是从XxxTableCellEditor调用的MouseEvents的默认值
您可以更改myEditor.setClickCountToStart(1);
从API中实现的所有XxxCellRendererMouse和内部方法中调用Key,您测试Renderer(也是Editor)内部的怪异事物,诸如此类的艰苦工作之类的大笑会导致重新绘制冻结,并且仅在mouse_over_hover上完成单元刷新


编辑


我需要在哪里打电话?我的TableCellEditor没有这个
方法。


伪码

JTable table = new JTable(model);
DefaultCellEditor editor = new DefaultCellEditor(......); // abstract or custom name
editor.setClickCountToStart(2); // for Compound JComponents (JComboBox) is more userfriendly invoke Editor on second click
table.getColumnModel().getColumn(1).setCellEditor(editor);

10-08 13:28