import java.awt.Component; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.util.Vector; import javax.swing.BorderFactory; import javax.swing.JComponent; 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.DefaultTableModel; import javax.swing.table.TableCellRenderer; public class TableCellToolTips extends JPanel { JTable table1; JTable table2; public TableCellToolTips() { // TODO Auto-generated constructor stub this.setLayout(new GridBagLayout()); this.initTable1(); this.initTable2(); JScrollPane s1 = new JScrollPane(this.table1); JScrollPane s2 = new JScrollPane(this.table2); s1.setBorder(BorderFactory.createTitledBorder("Table 1: override prepareRenderer")); s2.setBorder(BorderFactory.createTitledBorder("Table 2: add mouse move event handler")); s1.setPreferredSize(new Dimension(600, 200)); s2.setPreferredSize(new Dimension(600, 200)); this.add(s1, new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); this.add(s2, new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0)); } void initTable1() { DefaultTableModel model = new DefaultTableModel(); this.table1 = new JTable(model) { @Override 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(String.valueOf(getValueAt(row, column))); } return c; } }; this.initColumn(model); this.initData(model); } void initTable2() { DefaultTableModel model = new DefaultTableModel(); this.table2 = new JTable(model); this.table2.addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { Point p = e.getPoint(); int row = table2.rowAtPoint(p); int column = table2.columnAtPoint(p); table2.setToolTipText(String.valueOf(table2.getValueAt(row, column))); } }); this.initColumn(model); this.initData(model); } void initColumn(DefaultTableModel model) { model.addColumn("x"); model.addColumn("1"); model.addColumn("2"); model.addColumn("3"); model.addColumn("4"); model.addColumn("5"); model.addColumn("6"); model.addColumn("7"); model.addColumn("8"); model.addColumn("9"); } void initData(DefaultTableModel model) { model.getDataVector().removeAllElements(); for (int i = 1; i < 10; i++) { Vector<Object> row = new Vector<Object>(); row.add(i); for (int j = 1; j < 10; j++) { row.add(String.format("%d x %d = %d", i, j, i * j)); } model.addRow(row); } } /** * @param args */ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setTitle("Table Cell ToolTips"); frame.setContentPane(new TableCellToolTips()); frame.pack(); Dimension sc = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((sc.width - frame.getWidth()) / 2, (sc.height - frame.getHeight()) / 2); frame.setVisible(true); } }