JTable中单元格的工具提示位置

JTable中单元格的工具提示位置

本文介绍了JTable中单元格的工具提示位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JTable ,我想在 JTable 。我想在其他地方显示单元格工具提示,而不是在同一单元格的顶部。基本上我想完全控制该工具提示的行为。有什么建议吗?

I have a JTable, and I want to change the default position of the tool-tip of a cell in JTable. I want to display the cell tool-tip at some place else instead on the top of the same cell. Basically I want to fully control the behavior of that tool-tip. Any suggestions?

推荐答案

这比我想象的要容易得多。

That worked surprisingly easier then I thought.

基本上,如果你覆盖 JTable getToolTipLocation ,你可以指定工具提示的位置,即使该提示来自单元格渲染器,例如

Basically, if you override the getToolTipLocation of the JTable, you can specify the location of the tool tip, even if that tip comes from a cell renderer, for example

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    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) {
                @Override
                public Point getToolTipLocation(MouseEvent event) {
                    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);
            }

        }

    }

}

这篇关于JTable中单元格的工具提示位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:44