Solved

ToolTip for a Column on JTable

Posted on 2001-08-08
4
641 Views
Last Modified: 2008-02-07
Hi,

I want to setup the Tooltip to one of the Columns in my table. This column is not wide enough to show the conntets in full. So I'd like to show the contents as ToolTip depending on where the mouse is pointed. I did some thing like this and it didn't worked out. Any input?

thanks,
java-whizkid.

MyCellRender renderer = new MyCellRender(table);
myColumn.setCellRenderer(renderer);


class MyCellRender extends DefaultTableCellRenderer implements MouseListener{
       
        JTable jtbl;
        String DEFAULT_TOOLTIP = "No ToolTip";
        public  MyCellRender(JTable jt){
            super();
            this.jtbl = jt;
            setToolTipText(DEFAULT_TOOLTIP);
           
        }
        public void setToolTipText(String tip){
            super.setToolTipText(tip);
        }
       
        public void mouseEntered(MouseEvent me){
            int row = jtbl.rowAtPoint(me.getPoint());
            int col = jtbl.columnAtPoint(me.getPoint());
            setToolTipText((String)jtbl.getValueAt(row, col));
        }
       
        public void mouseExited(MouseEvent me){
            setToolTipText("Exited");    
        }
        public void mouseClicked(MouseEvent me){
            int row = jtbl.rowAtPoint(me.getPoint());
            int col = jtbl.columnAtPoint(me.getPoint());
            setToolTipText((String)jtbl.getValueAt(row, col));
        }
        public void mousePressed(MouseEvent me){
        }
        public void mouseReleased(MouseEvent me){
        }
    }
0
Comment
Question by:jkid
4 Comments
 
LVL 6

Accepted Solution

by:
kotan earned 50 total points
ID: 6366722
I have tried your work and found that mouseEntered() is not enough to do that and I have add mouseMove().
Below is the code:

// member variable indicate current pointed field.
int curR = -1;
int curC = -1;

public void mouseEntered(MouseEvent evt) {
     curR = jtbl.rowAtPoint(me.getPoint());
     curC = jtbl.columnAtPoint(me.getPoint());
     setToolTipText((String)jtbl.getValueAt(row, col));
}

public void mouseMove(MouseEvent evt) {
     int lR = jtbl.rowAtPoint(me.getPoint());
     int lC = jtbl.columnAtPoint(me.getPoint());
     if (lR != curR || lC != curC) {
          curR = lR;
          curC = lC;
          setToolTipText((String)jtbl.getValueAt(row, col));
     }
}

public void MouseExited(MouseEvent evt) {
     curR = -1;
     curC = -1;
}


For mouseClicked, I think this is not nessarry, since the tooltips will be display when the mouse moves over.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 50 total points
ID: 6366731
You don't need to provide a mouse listener (and I can't see where you add your mouse listener). You simply need to set the tool tip text in your renderers getTableCellRendererComponent method:

public Component getTableCellRendererComponent(
  JTable table, Object value, boolean selected,
  boolean hasFocus, int row, int col)
{
   JLabel c = (JLabel)super.getTableCellRendererComponent(
      table, value, selected, hasFocus, row, col);
   
   c.setToolTipText(value.toString());
   return c;
}

I think you may also need to call the following on your table:

ToolTipManager.sharedInstance().registerComponent(yourTable);

0
 

Expert Comment

by:CleanupPing
ID: 9061904
jkid:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9329898
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between  kotan and objects

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Core Java. What output will be and why ? 6 65
Eclipse IDE - Cannot copy/paste from console output 8 126
Non-recursive backtracking, using a stack 1 98
Java Loop 6 48
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now