Solved

ToolTip for a Column on JTable

Posted on 2001-08-08
4
638 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 47
count11 challenge 6 47
Passing list of object to Oracle Database Procedure 3 42
Starting to learn JAVA, 7 48
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

17 Experts available now in Live!

Get 1:1 Help Now