显示位置的JTable工具提示问题

时间:2015-07-05 12:03:57

标签: java user-interface tooltip

我在JTable单元格中遇到了tooltip的问题。在我的自定义单元格渲染器中,调用了setToolTipText。但是当盘旋到表格单元格时,工具提示没有显示在正确的位置,尽管文本是正确的。关于如何为单元格渲染器类的表格单元设置tootip位置的任何想法。

P.S我无法在此发布实际代码。 感谢

编辑: 示例代码

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);
        }

    }

}

1 个答案:

答案 0 :(得分:0)

如果没有提供任何代码示例,我可以这样说:

使用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;
            }
        };

有关此代码段的来源,请访问thisthis SO问题。