001package jmri.util.table;
002
003import java.awt.event.MouseEvent;
004import javax.swing.JTable;
005import javax.swing.table.JTableHeader;
006
007/**
008 * Extends JTable with Column Header ToolTips.
009 * @author Steve Young Copyright (C) 2020
010 */
011public class JTableWithColumnToolTips extends JTable {
012    
013        private final String[] colTips;
014        
015        /**
016         * Create a JTable with Column ToolTips.
017         * @param model JTable Model to base table on.
018         * @param tips String Array of Column Tool tips.
019         */
020        public JTableWithColumnToolTips(javax.swing.table.TableModel model, String[] tips){
021            super(model);
022            colTips = tips;
023        }
024    
025        // Override JTable Header to implement table header tool tips.
026        @Override
027        protected JTableHeader createDefaultTableHeader() {
028            return new JTableHeader(columnModel) {
029                @Override
030                public String getToolTipText(MouseEvent e) {
031                    try {
032                        java.awt.Point p = e.getPoint();
033                        int index = columnModel.getColumnIndexAtX(p.x);
034                        int realIndex = columnModel.getColumn(index).getModelIndex();
035                        return colTips[realIndex];    
036                    } catch (RuntimeException e1) { }
037                    return null;
038                }
039            };
040        }
041    }