File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes DataTip for a cell in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "DataTip for a cell in JTable" Watch "DataTip for a cell in JTable" New topic
Author

DataTip for a cell in JTable

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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

Joined: Aug 17, 2006
Posts: 52
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

Joined: Jun 09, 2003
Posts: 4632
not sure what you're trying to do, but is DataTip a multi-line tooltip?

if so, try this

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Aug 17, 2006
Posts: 52
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

Joined: Jun 09, 2003
Posts: 4632
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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

Joined: Aug 17, 2006
Posts: 52
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

Joined: Jun 09, 2003
Posts: 4632
> How to read the value in the cell?

read my previous replies.
Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
Thank you so much. That was quite stupid of me for not having seen the solution properly.
Have a nice day
 
 
subject: DataTip for a cell in JTable