Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataTip for a cell in JTable

 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have JTable in which one particular column has many characters in each cell. How to implement DataTip (in box format-not in the same line)?

Please help me.

Thank you.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is the code i found on net. I kind of didnt understand what is happening and this doesnt seem to be working. The code is as shown below :

--------------------------------------------------------------------------

// JTableEx.java
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.plaf.*;

/**
* @author Zafir Anjum
*/

public class JTableEx extends JTable
{
public JTableEx() {
this(null, null, null);
}

public JTableEx(TableModel dm) {
this(dm, null, null);
}

public JTableEx(TableModel dm, TableColumnModel cm) {
this(dm, cm, null);
}

public JTableEx(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
super(dm,cm,sm);
}

public JTableEx(int numRows, int numColumns) {
this(new DefaultTableModel(numRows, numColumns));
}

public JTableEx(final Vector rowData, final Vector columnNames) {
super( rowData, columnNames );
}

public JTableEx(final Object[][] rowData, final Object[] columnNames) {
super( rowData, columnNames );
}

public String getToolTipText(MouseEvent event)
{
int row = rowAtPoint( event.getPoint() );
int col = columnAtPoint( event.getPoint() );
Object o = getValueAt(row,col);
if( o == null )
return null;
if( o.toString().equals("") )
return null;
return o.toString();
}

public Point getToolTipLocation(MouseEvent event)
{
int row = rowAtPoint( event.getPoint() );
int col = columnAtPoint( event.getPoint() );
Object o = getValueAt(row,col);
if( o == null )
return null;
if( o.toString().equals("") )
return null;
Point pt = getCellRect(row, col, true).getLocation();
pt.translate(-1,-2);
return pt;
}
} // End of Class JTableEx
-------------------------------------------------------------------------

I am lost in the codes. Please help me.
Thank you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure what you're trying to do, but is DataTip a multi-line tooltip?

if so, try this

 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for that Michael.
But, thats a tool tip. At the point of the cursor, it displays the message (hardcoded in html). Is there a way to display the content of the cell (without repeating it again in the html) ? As sometimes the no. of characters exceeds the cell capacity.

Thank you.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Is there a way to display the content of the cell (without repeating it again in the html)

one of the other parameters represents the contents of the cell.
Try setting that as the toolTipText (without the html)
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The return type of 'row' and 'column' in the method

getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column)

is of the type int. Therefore, the values that these parameters return are integers but not the string in the cell. As a result, the content of the cell is not being tapped. How to tap the contents of the cell in the method getToolTipText() ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when this piece of code is used,

lbl.setToolTipText(row);(instead of the html)

the error says - setToolTipText() is not applicable for arguments int.
Therefore only a string can be passed to the above method.
But, the 'row' and 'column' in the method:

getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column));

returns int.

I am a little confused. Have i understood it right? or am i wrong somewhere?
The 'row' and 'column' keeps a count of no. of rows n cols but not the value in it. How to read the value in the cell?
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when this piece of code is used,

lbl.setToolTipText(row);(instead of the html)

the error says - setToolTipText() is not applicable for arguments int.
Therefore only a string can be passed to the above method.
But, the 'row' and 'column' in the method:

getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column));

returns int.

I am a little confused. Have i understood it right? or am i wrong somewhere?
The 'row' and 'column' keeps a count of no. of rows n cols but not the value in it. How to read the value in the cell?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> How to read the value in the cell?

read my previous replies.
 
Vani Shastri
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much. That was quite stupid of me for not having seen the solution properly.
Have a nice day
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic