编辑时如何在JTable单元格上方显示文本 [英] How to display a text above a JTable cell when editing
问题描述
当某人正在编辑单元格时,我想在JTable单元格上方显示文本.文本与工具提示几乎相同,不同的是,仅在编辑单元格时才显示文本,并且文本应保留到编辑完成为止.
I want to display a text above a JTable cell when someone is editing the cell. The text is pretty much the same as a tooltip with the exception, that the text should only be displayed when editing a cell and the text should stay until the editing is finished.
我如何实现这种行为?
到目前为止,我试图覆盖JTable
的getCellEditor
方法,但这只会设置标准的工具提示,但是在编辑时我需要永久显示文本.
What I have tried so far is to override the getCellEditor
method of JTable
but that will only set the standard tooltip, but I need to display the text permanently for the time of the editing.
@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;
}
推荐答案
另一种选择是在开始编辑单元格时也添加JLabel(或任何组件).每当它停止时,就删除该组件.
Another option would be to also add a JLabel (or any component) when you start editing a cell. Whenever it stops, you remove the component.
由于您希望工具提示/标签出现在已编辑的单元格上方,因此第一行需要一些技巧.
Because you want the tooltip/label to appear above the edited cell, it requires a little trick for the first row.
这是一个说明我的意思的例子:
Here is an example showing what I mean:
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);
}
}
}
这篇关于编辑时如何在JTable单元格上方显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!