EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

DataTips


Author: Zafir Anjum

It is not unusual to see a JTable used in a dialog. However, it is unusual to see all the columns fit in the limited space of the dialog. The code shown below makes these narrow columns a little more useful.

When you hover the mouse cursor over a cell that is too narrow to show its text completely, a tooltip appears over the cell expanding the text in it so that the user can read it without widening the column. This feature is sometimes called a DataTip or TitleTip. It basically shows you the same text (and at the same location) that you would see if the control were big enough to display it all.


// 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

Tech Note: This is a quick and easy implementation but there are a number of things that can be improved upon. One aspect of this implementation is that the tooltip pops up over the cell even when the cell is wide enough to display all its content (this shouldn't be very difficult to fix). The other problems are caused because it uses a JToolTip. Like a regular tooltip, the datatip is also removed after a certain time of mouse inactivity.

Posted On: 29-Dec-1998

internet.commerce



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy