我在JTable单元格中有一个工具提示问题。在我的自定义单元渲染器中,对setToolTipText
进行调用。但是当将鼠标悬停在表格单元格上时,工具提示没有显示在正确的位置,尽管文本是正确的。关于如何为单元格渲染器类设置表格单元格的提示位置,有什么想法吗?
p。我不能在这里张贴实际的代码。由于
编辑:示例代码
import java.awt.Color;
import bibliothek.extension.gui.dock.theme.EclipseTheme;
import bibliothek.gui.DockController;
import bibliothek.gui.dock.DefaultDockable;
import bibliothek.gui.dock.SplitDockStation;
import bibliothek.gui.dock.common.CControl;
import bibliothek.gui.dock.common.CGrid;
import bibliothek.gui.dock.common.DefaultSingleCDockable;
import bibliothek.gui.dock.common.intern.CDockable;
import bibliothek.gui.dock.common.theme.ThemeMap;
import bibliothek.gui.dock.station.split.SplitDockProperty;
import bibliothek.gui.dock.station.stack.tab.DefaultMenuLineLayoutFactory;
import bibliothek.gui.dock.station.stack.tab.MenuLineLayout;
import bibliothek.gui.dock.station.stack.tab.MenuLineLayoutOrder;
import bibliothek.gui.dock.station.stack.tab.TabPane;
import bibliothek.gui.dock.station.stack.tab.MenuLineLayoutOrder.Item;
import bibliothek.gui.dock.util.AppletWindowProvider;
import javax.swing.*;
public class aaa {
public static void main( String[] args ){
try {
JFrame frame = new JFrame();
AppletWindowProvider window = new AppletWindowProvider(frame);
CControl control = new CControl(window,true);
frame.add(control.getContentArea());
control.setTheme(ThemeMap.KEY_ECLIPSE_THEME);
CGrid grid = new CGrid(control);
grid.add(10, 0, 62, 100, new DefaultSingleCDockable("AAA"));
grid.add(10, 0, 62, 100, new MyDock());
grid.add(0, 0, 38, 100, new DefaultSingleCDockable("CCC"));
control.getContentArea().deploy(grid);
frame.setBounds(20, 20, 400, 400);
frame.setVisible(true);
} catch(Exception e) {
e.printStackTrace();
}
}
private static class MyDock extends DefaultSingleCDockable {
TestPane pane;
public MyDock() {
super("BBB");
pane = new TestPane();
add(pane);
}
}
}
和TestPane类
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseEvent;
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
DefaultTableModel model = new DefaultTableModel(0, 10);
for (int row = 0; row < 10; row++) {
Object[] data = new Object[10];
for (int col = 0; col < 10; col++) {
data[col] = row + "x" + col;
}
model.addRow(data);
}
JTable table = new JTable(model) {
public Point getToolTipLocation(MouseEvent event) {
Point p = this.getMousePosition();
try{
int x = event.getX();//p.x;
int y = event.getY();//p.y;
//System.out.println("$$$$$$$$$$$$$x :- "+x+" , y :- "+y);
return new Point(x,y);
}catch(Exception e)
{
System.out.println("Exception "+e.getMessage());
}
return new Point(10, 10);
}
};
table.setDefaultRenderer(Object.class, new TestCellRenderer());
add(new JScrollPane(table));
}
public class TestCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setToolTipText("Banana @ " + value.toString());
return super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
}
}
}
在不提供任何代码示例的情况下,我可以这样说:
使用set toolTipText如下:
JTable table = new JTable() {
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);
if (c instanceof JComponent) {
JComponent jc = (JComponent) c;
jc.setToolTipText(getValueAt(row, column).toString());
}
return c;
}
};
或者您可以使用此处建议的MouseEvent
:
JTable auditTable = new JTable(){
//Implement table cell tool tips.
public String getToolTipText(MouseEvent e) {
String tip = null;
java.awt.Point p = e.getPoint();
int rowIndex = rowAtPoint(p);
int colIndex = columnAtPoint(p);
try {
tip = getValueAt(rowIndex, colIndex).toString();
} catch (RuntimeException e1) {
//catch null pointer exception if mouse is over an empty line
}
return tip;
}
};
有关此代码片段的源代码,请访问this and this SO questions.