Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a tooltip to a cell in a jtable?

I have a table where each row represents a picture. In the column Path I store its absolute path. The string being kinda long, I would like that when I hover the mouse over the specific cell, a tooltip should pop-up next to the mouse containing the information from the cell.

like image 553
Pantaziu Cristian Avatar asked Feb 27 '12 14:02

Pantaziu Cristian


People also ask

Which method is used to add tooltip text?

We can add tooltip text to almost all the components of Java Swing by using the following method setToolTipText(String s). This method sets the tooltip of the component to the specified string s. When the cursor enters the boundary of that component a popup appears and text is displayed.

How do I highlight a cell in JTable?

You can select cells by simulating clicks. To simulate clicks on JTable cells, you can use the ClickCell , ClickCellR , DblClickCell and similar actions of the JTable object. All of these actions have parameters that specify the row and the column that contain the needed cell.

How do you make a JTable cell editable?

jTableAssignments = new javax. swing. JTable() { public boolean isCellEditable(int rowIndex, int colIndex) { return editable; }};

How can we add insert a JButton to JTable cell in Java?

We can add or insert a JButton to JTable cell by customizing the code either in DefaultTableModel or AbstractTableModel and we can also customize the code by implementing TableCellRenderer interface and need to override getTableCellRendererComponent() method.


1 Answers

Just use below code while creation of JTable object.

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;             }         }; 
like image 89
user3223087 Avatar answered Oct 05 '22 00:10

user3223087