Solved

ToolTip for a Column on JTable

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 96
HTTPSessionBindingListernter not configured in deployment descriptor 2 52
java stored proc example 9 29
jdbc error in jsp application 20 44
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

785 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