Solved

ToolTip for a Column on JTable

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …

630 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