Solved

ToolTip for a Column on JTable

Posted on 2001-08-08
4
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

734 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