Author: Jeroen Zwartepoorte
Expand the list item so that the entire item is visible when the mouse hovers over a list item.
After reading the article on DataTips by Zafir Anjum i decided to write a component that provided this functionality in a JList. The result is JListEx. I had to write the class a little bit different from the JTableEx that Zafir wrote. I needed to know the size of the JScrollPane that contained the JList in order to determine wether a datatips was necessary. (if the width of the cell > the width of the JScrollPane then draw the datatip).
I welcome suggestions & bug reports to further improve the component.
/**
* SwingEx Project<BR>
*
* This class is part of the Swing Extension Project. The goal of this project
* is to provide Swing components with extra functionality that isn't available
* in the standard Swing components from Sun.
*
* @author Jeroen Zwartepoorte (Jeroen@xs4all.nl)
*/
package SwingEx;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.Vector;
// JFC 1.0.X
/*import com.sun.java.swing.*;
import com.sun.java.swing.event.ListSelectionListener;*/
// JFC 1.1.X
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
/**
* This component contains extended functionality that JList doesn't have. It's
* written so that you only have to change your JList declaration from JList to
* JListEx. The rest of the changes are transparent. These are the extra
* functionalities: <BR>
* <UL>
* <LI><B>DataTips</B>: When a cell isn't completely visible, it displays a
* JToolTip when the user holds the cursor over it.</LI>
* <LI><B>JScrollPane</B>: In order for the datatips to work, JListEx is already
* in a JScrollPane. You don't have to add JListEx to a scrollpane like you had
* to do for JList. You can get access to the scrollpane by using the
* getScrollPane() method.</LI></UL><BR>
*
* Any suggestions on extra functionality are welcome!
*
* @author Jeroen Zwartepoorte (Jeroen@xs4all.nl)
* @author used source by Zafir Anjum (www.codeguru.com/java/articles/123.shtml)
* @version 1.0
*/
public class JListEx extends JComponent
{
// Private variables.
private JScrollPane scrollpane = null;
private JList list = null;
private boolean bDataTips = false;
/**
* Parameterless constructor.
*/
public JListEx()
{
list = new JList() {
public String getToolTipText(MouseEvent event)
{
return this_getToolTipText(event);
}
public Point getToolTipLocation(MouseEvent event)
{
return this_getToolTipLocation(event);
}
};
init();
}
/**
* Object array constructor.
*/
public JListEx(final Object[] listData)
{
list = new JList(listData) {
public String getToolTipText(MouseEvent event)
{
return this_getToolTipText(event);
}
public Point getToolTipLocation(MouseEvent event)
{
return this_getToolTipLocation(event);
}
};
init();
}
/**
* Vector constructor.
*/
public JListEx(final Vector listData)
{
list = new JList(listData) {
public String getToolTipText(MouseEvent event)
{
return this_getToolTipText(event);
}
public Point getToolTipLocation(MouseEvent event)
{
return this_getToolTipLocation(event);
}
};
init();
}
/**
* ListModel constructor.
*/
public JListEx(ListModel dataModel)
{
list = new JList(dataModel) {
public String getToolTipText(MouseEvent event)
{
return this_getToolTipText(event);
}
public Point getToolTipLocation(MouseEvent event)
{
return this_getToolTipLocation(event);
}
};
init();
}
/**
* Intialize class variables.
*/
protected void init()
{
this.setLayout(new BorderLayout());
scrollpane = new JScrollPane(list);
this.add(scrollpane, BorderLayout.CENTER);
}
/**
* Returns wether the datatips are enabled.
*/
public boolean getDataTips()
{
return bDataTips;
}
/**
* Enable or disable datatips.
*/
public void setDataTips(boolean bDataTips)
{
this.bDataTips = bDataTips;
if (bDataTips)
list.setToolTipText("text");
else
list.setToolTipText("");
}
/**
* Returns scrollpane that contains JList.
*/
public JScrollPane getScrollPane()
{
return scrollpane;
}
/**
* This method is called when the JList is asked for the tooltip text.
* JList.getToolTipText refers to this method. This method is declared here
* because it needs access to the JScrollPane that the JList is in.
*/
protected String this_getToolTipText(MouseEvent event)
{
int idx = list.locationToIndex(event.getPoint());
if (idx == -1)
return null;
Object obj = list.getModel().getElementAt(idx);
boolean bSelected = obj.equals(list.getSelectedValue());
Component comp = list.getCellRenderer().getListCellRendererComponent(list,
obj, idx, bSelected, bSelected);
if (obj == null)
return null;
if (obj.toString().equals(""))
return null;
if (comp.getPreferredSize().width < scrollpane.getSize().width)
{
return null;
}
return obj.toString();
}
/**
* This method is called when the JList is asked for the position of the
* tooltip. JList.getToolTipLocation refers to this method. This method is
* declared here because it needs access to the JScrollPane that the JList
* is in.
*/
protected Point this_getToolTipLocation(MouseEvent event)
{
int idx = list.locationToIndex(event.getPoint());
if (idx == -1)
return null;
Object obj = list.getModel().getElementAt(idx);
boolean bSelected = obj.equals(list.getSelectedValue());
Component comp = list.getCellRenderer().getListCellRendererComponent(list,
obj, idx, bSelected, bSelected);
if (obj == null)
return null;
if (obj.toString().equals(""))
return null;
if (comp.getPreferredSize().width < scrollpane.getSize().width)
{
return null;
}
Point pt = list.getCellBounds(idx, idx).getLocation();
if (pt == null)
return null;
pt.translate(-2, -1);
return pt;
}
/**
* JList methods.
*
* These methods are declared here so that the programmer can use JListEx
* like any normal JList. Since this class extends JComponent we need to
* add all the public methods in JList here and call the appropriate method
* in the JList.
*/
public void addListSelectionListener(ListSelectionListener listener) {
list.addListSelectionListener(listener);
}
public void addSelectionInterval(int anchor, int lead) {
list.addSelectionInterval(anchor, lead);
}
public void clearSelection() {
list.clearSelection();
}
public void ensureIndexIsVisible(int index) {
list.ensureIndexIsVisible(index);
}
public int getAnchorSelectionIndex() {
return list.getAnchorSelectionIndex();
}
public Rectangle getCellBounds(int index1, int index2) {
return list.getCellBounds(index1, index2);
}
public ListCellRenderer getCellRenderer() {
return list.getCellRenderer();
}
public int getFirstVisibleIndex() {
return list.getFirstVisibleIndex();
}
public int getFixedCellHeight() {
return list.getFixedCellHeight();
}
public int getFixedCellWidth() {
return list.getFixedCellWidth();
}
public int getLastVisibleIndex() {
return list.getLastVisibleIndex();
}
public int getLeadSelectionIndex() {
return list.getLeadSelectionIndex();
}
public int getMaxSelectionIndex() {
return list.getMaxSelectionIndex();
}
public int getMinSelectionIndex() {
return list.getMinSelectionIndex();
}
public ListModel getModel() {
return list.getModel();
}
public Dimension getPreferredScrollableViewportSize() {
return list.getPreferredScrollableViewportSize();
}
public Object getPrototypeCellValue() {
return list.getPrototypeCellValue();
}
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return list.getScrollableBlockIncrement(visibleRect, orientation, direction);
}
public boolean getScrollableTracksViewportHeight() {
return list.getScrollableTracksViewportHeight();
}
public boolean getScrollableTracksViewportWidth() {
return list.getScrollableTracksViewportWidth();
}
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return list.getScrollableUnitIncrement(visibleRect, orientation, direction);
}
public int getSelectedIndex() {
return list.getSelectedIndex();
}
public int[] getSelectedIndices() {
return list.getSelectedIndices();
}
public Object getSelectedValue() {
return list.getSelectedValue();
}
public Object[] getSelectedValues() {
return list.getSelectedValues();
}
public Color getSelectionBackground() {
return list.getSelectionBackground();
}
public Color getSelectionForeground() {
return list.getSelectionForeground();
}
public int getSelectionMode() {
return list.getSelectionMode();
}
public ListSelectionModel getSelectionModel() {
return list.getSelectionModel();
}
public boolean getValueIsAdjusting() {
return list.getValueIsAdjusting();
}
public int getVisibleRowCount() {
return list.getVisibleRowCount();
}
public Point indexToLocation(int index) {
return list.indexToLocation(index);
}
public boolean isSelectedIndex(int index) {
return list.isSelectedIndex(index);
}
public boolean isSelectionEmpty() {
return list.isSelectionEmpty();
}
public int locationToIndex(Point location) {
return list.locationToIndex(location);
}
public void removeListSelectionListener(ListSelectionListener listener) {
list.removeListSelectionListener(listener);
}
public void removeSelectionInterval(int index0, int index1) {
list.removeSelectionInterval(index0, index1);
}
public void setCellRenderer(ListCellRenderer cellRenderer) {
list.setCellRenderer(cellRenderer);
}
public void setFixedCellHeight(int height) {
list.setFixedCellHeight(height);
}
public void setFixedCellWidth(int width) {
list.setFixedCellWidth(width);
}
public void setListData(final Object[] listData) {
list.setListData(listData);
}
public void setListData(final Vector listData) {
list.setListData(listData);
}
public void setModel(ListModel model) {
list.setModel(model);
}
public void setPrototypeCellValue(Object prototypeCellValue) {
list.setPrototypeCellValue(prototypeCellValue);
}
public void setSelectedIndex(int index) {
list.setSelectedIndex(index);
}
public void setSelectedIndices(int[] indices) {
list.setSelectedIndices(indices);
}
public void setSelectedValue(Object anObject,boolean shouldScroll) {
list.setSelectedValue(anObject, shouldScroll);
}
public void setSelectionBackground(Color selectionBackground) {
list.setSelectionBackground(selectionBackground);
}
public void setSelectionForeground(Color selectionForeground) {
list.setSelectionForeground(selectionForeground);
}
public void setSelectionInterval(int anchor, int lead) {
list.setSelectionInterval(anchor, lead);
}
public void setSelectionMode(int selectionMode) {
list.setSelectionMode(selectionMode);
}
public void setSelectionModel(ListSelectionModel selectionModel) {
list.setSelectionModel(selectionModel);
}
public void setValueIsAdjusting(boolean b) {
list.setValueIsAdjusting(b);
}
public void setVisibleRowCount(int visibleRowCount) {
list.setVisibleRowCount(visibleRowCount);
}
}
Posted On: 11-Feb-1999