Returns the JTable tooltip value in HTML format. - Java Swing

Java examples for Swing:JTable

Description

Returns the JTable tooltip value in HTML format.

Demo Code


import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.swing.AbstractCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;
import javax.swing.event.TableModelEvent;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

public class Main{
    public static final int WRAP_MAX_CHAR = 150;
    /** Tooltip infinite time value */
    public static final int INFINITE_TIME = 0x7fffffff;
    /**/*www  . j  a va  2s  .  c o  m*/
     * Returns the tooltip value in HTML format.
     * @param table The table.
     * @param event The MouseEvent.
     * @return the tooltip value in HTML format.
     */
    public static String getToolTipInHTML(JTable table, MouseEvent event) {
        ToolTipManager.sharedInstance().setDismissDelay(INFINITE_TIME);

        TableModel model = table.getModel();
        int rowCount = model.getRowCount();
        int colCount = model.getColumnCount();
        for (int r = 0; r < rowCount; r++) {
            for (int c = 0; c < colCount; c++) {
                Rectangle rect = table.getCellRect(r, c, false);
                if (rect.contains(event.getPoint())) {
                    String value = model.getValueAt(r, c).toString();
                    if (value.length() <= 0)
                        return null;
                    value = StringUtil.wrap(WRAP_MAX_CHAR, value);
                    return StringUtil.convertToHTML(value);
                }
            }
        }
        return null;
    }
}

Related Tutorials