0

当有人编辑单元格时,我想在 JTable 单元格上方显示文本。文本与工具提示几乎相同,但只有在编辑单元格时才应显示文本,并且文本应保留到编辑完成。

我怎么能做到这样的行为?

到目前为止,我尝试的是覆盖的getCellEditor方法,JTable但这只会设置标准工具提示,但我需要在编辑时永久显示文本。

@Override
public TableCellEditor getCellEditor(int row, int column) {
    TableCellEditor editor = super.getCellEditor(row, column);
    Component component = editor.getTableCellEditorComponent(this, getValueAt(row, column), isCellSelected(row, column), row, column);
    if(component instanceof JTextField) {
        JTextField textfield = (JTextField) component;
        textfield.setToolTipText("tooltip");
    }
    return editor;
}
4

1 回答 1

5

另一种选择是在您开始编辑单元格时也添加一个 JLabel(或任何组件)。每当它停止时,您就删除该组件。

因为您希望工具提示/标签出现在编辑的单元格上方,所以第一行需要一些小技巧。

这是一个显示我的意思的示例:

import java.awt.Container;
import java.awt.Point;
import java.util.EventObject;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;

public class TestTable {

    public JFrame f;
    private JLabel tooltip;
    private MyTable table;

    public class MyTable extends JTable {

        @Override
        public boolean editCellAt(int row, int column, EventObject e) {
            boolean editCellAt = super.editCellAt(row, column, e);
            if (editCellAt) {
                Point editedCellLocation = getCellRect(getEditingRow(), getEditingColumn(), true).getLocation();
                if (tooltip != null) {
                    removeTooltip();
                }
                tooltip = new JLabel("Hello some nice tooltip");
                tooltip.setOpaque(true);
                tooltip.setSize(tooltip.getPreferredSize());
                if (getEditingRow() == 0) {
                    tooltip.setLocation(editedCellLocation.x, getTableHeader().getHeight() - tooltip.getHeight());
                    getTableHeader().add(tooltip);
                } else {
                    tooltip.setLocation(editedCellLocation.x, editedCellLocation.y - tooltip.getHeight());
                    add(tooltip);
                }
                ((JComponent) tooltip.getParent()).repaint(tooltip.getBounds());
            }
            return editCellAt;
        }

        @Override
        public void editingStopped(ChangeEvent e) {
            super.editingStopped(e);
            removeTooltip();
        }

        protected void removeTooltip() {
            if (tooltip != null) {
                Container parent = tooltip.getParent();
                parent.remove(tooltip);
                ((JComponent) parent).repaint(tooltip.getBounds());
                tooltip = null;
            }
        }
    }

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

            @Override
            public void run() {
                new TestTable().initUI();
            }
        });
    }

    protected void initUI() {
        table = new MyTable();
        table.setModel(new TestTableModel());
        f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.add(new JScrollPane(table));
        f.setVisible(true);
    }

    public class TestTableModel extends DefaultTableModel {

        public TestTableModel() {
            super(new String[] { "DATA" }, 3);
            setValueAt(Double.valueOf(-0.1), 0, 0);
            setValueAt(Double.valueOf(+0.1), 1, 0);
            setValueAt(Double.valueOf(0), 2, 0);
        }
    }

}
于 2012-05-23T08:37:47.560 回答