Home arrow static arrow Java Programming [Archive] - Overriding createToolTip?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Overriding createToolTip?
This topic has 13 replies on 1 page.

Posts:14
Registered: 8/1/01
Overriding createToolTip?  
Oct 2, 2001 8:34 AM



 
I am trying to override createToolTip in order to get a multi-line tooltip. It works fine for JButton but it does not work for my DefaultTableCellRenderer. Does anybody know why?

public class MyDefaultCellRenderer extends DefaultTableCellRenderer {  public MyDefaultCellRenderer() {    super();  }  public JToolTip createToolTip() {    return new MultiLineToolTip();  }}


Thanks
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 2, 2001 8:55 AM (reply 1 of 13)



 
Why not use setToolTipText() ? It accepts an HTML string, so you could embed a
in there.
 

Posts:14
Registered: 8/1/01
Re: Overriding createToolTip?  
Oct 2, 2001 9:20 AM (reply 2 of 13)



 
What I need it to do is to automatically wrap long text fields so they can fit on the screen. So, I don't know where the breaks are initially.
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 2, 2001 9:38 AM (reply 3 of 13)



 
OK. How about taking the String instance of your tooltip text and passing it through something like a java.text.BreakIterator ? That would give you all the word break positions - could you not then use that in conjunction with an arbitrary integer that says "I don't want my tool tip width to be any more than N characters" ? You could then determine where best to insert any number of
tags.
 

Posts:14
Registered: 8/1/01
Re: Overriding createToolTip?  
Oct 2, 2001 10:53 AM (reply 4 of 13)



 
Is there a reason why I can't override this method? That would make things much easier because I already have the code to make a tooltip mulit-line. I just haven't been able to apply it to a DefaultTableCellRenderer.
 

Posts:18
Registered: 10/2/01
Re: Overriding createToolTip?  
Oct 2, 2001 11:13 AM (reply 5 of 13)



 
what are duke dollars?
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 3, 2001 12:34 AM (reply 6 of 13)



 
Notional points that can be awarded to people who post useful replies to forum questions. The JDC awards you so many points each month if you regularly take part in forums. I think they also offer a small prize for the highest scorers each month. You get 25 D$s the first time you join.
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 3, 2001 12:37 AM (reply 7 of 13)



 
Overriding is perfectly appropriate. I was just trying to think of a workaround!
Can you post the code for your MultiLineToolTip ? I'll gladly have a go at running it myself, see what happens.
 

Posts:8
Registered: 10/3/01
Cast !  
Oct 3, 2001 4:33 AM (reply 8 of 13)



 
Look closely to your code here:
public JToolTip createToolTip() {    return new MultiLineToolTip();  }

So this method returns a JToolTip. In order to be able to acces the MultiLineToolTip which actually stays behind this, you have to make a cast, smth. like:

MultiLineToolTip mt= (MultiLineToolTip)myDefaultCellRendererInstance.createToolTip();

(The code in bold above is essential.)
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 3, 2001 7:11 AM (reply 9 of 13)



 
Matt,

Not sure if I've simulated what you're doing, but I've managed to get round your problem by getting the JTable to override createToolTip() rather than the renderer - the renderer simply sets its own tool tip text. Here's some test code you can try - it's not a multi-line tooltip but a bespoke tooltip nevertheless which changes the default colours:

import java.awt.*;import javax.swing.*; public class Test extends JFrame{public Test(){	super( "Tooltip Test" );	setSize( 500, 500 );	MyModel model = new MyModel();	JTable jt = new JTable( model ) { public JToolTip createToolTip() { return new MyToolTip(); } };	jt.setDefaultRenderer( String.class, new MyRenderer() );	JScrollPane jsp = new JScrollPane( jt );	JPanel p = new JPanel();	p.add( jsp, BorderLayout.CENTER );	JButton b = new JButton( "Press me" ) { public JToolTip createToolTip() { return new MyToolTip(); } };	b.setToolTipText( "I'm a button" );	p.add( b );	getContentPane().add( p );}                                           public static void main(String args[]){	Test t = new Test();	t.setVisible(true);}    }  import javax.swing.*; public class MyToolTip extends JToolTip{public MyToolTip(){	setForeground(java.awt.Color.red);	setBackground(java.awt.Color.yellow);}}  import javax.swing.*;import javax.swing.table.*;import java.awt.*; public class MyRenderer extends JLabel implements TableCellRenderer{public MyRenderer(){	super();}       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,	 boolean hasFocus, int row,int column){	if (value != null) { setText( ( String ) value ); }	setToolTipText( "I'm a renderer" );	return this;}                      }  import javax.swing.table.*; public class MyModel extends AbstractTableModel{  Class columnClasses[] = {String.class, String.class};  Object  data[][] = {	{"Who", "needs"},	{"tooltips", "anyway"}  };  public Class getColumnClass(int c)  {      return columnClasses[c];  }      public int getColumnCount() { return data[0].length; }      public int getRowCount() { return data.length; }      public Object getValueAt(int r, int c) { return data[r][c]; }    public void setValueAt(Object value, int r, int c) { data[r][c] = value; }    }
 

Posts:14
Registered: 8/1/01
Re: Overriding createToolTip?  
Oct 3, 2001 8:08 AM (reply 10 of 13)



 
Gary, thank you for your help. I moved the createToolTip method to the table and now it works for the cells. I'm not really sure why it didn't work the other way? But, it works now!
Matt
 

Posts:55
Registered: 7/27/98
Re: Overriding createToolTip?  
Oct 3, 2001 9:13 AM (reply 11 of 13)



 
Good.

Yeah, I was wondering why it didn't work directly with the renderer. Without going into a long effort to debug the thing, the only thing that occurs to me is in relation to JComponent. It's that class which is the top-level parent for createToolTip() so anything that wants to override obviously has to subclass from JComponent. When you use a table cell renderer, it typically extends a J-type Swing control, which is fine; but the key method of the TableCellRenderer interface it implements is getTableCellRendererComponent(...), which returns Component and not JComponent. Makes me wonder whether this is part of the issue.....or perhaps I'm talking through my hat......

Is there anyone there from Sun who can enlighten us ?
 

Posts:36
Registered: 3/29/99
Re: Overriding createToolTip?  
Oct 7, 2002 8:33 PM (reply 12 of 13)



 
Your code works fine. And when the following lines are added to the end of the Test class constructor, after getContentPane().add(p);
            for (int i = 0; i < model.getColumnCount(); i++)      {         TableColumn column = jt.getColumnModel().getColumn(i);         MyRenderer tcr = new MyRenderer();         column.setHeaderRenderer(tcr);      }

then it doesn't show the red and yellow tooltips on the table header, only light blue. Also, if a System.out.println("gets here") statement is put into createToolTip() it doesn't print when you hover the mouse over the table header, but it does work over the table cell.

It doesn't work by replacing the above lines with:
 jt.getTableHeader().setDefaultRenderer(new MyRenderer()); 

either. Also, is there a way to use it with JTableHeader.createDefaultRenderer() ?
 

Posts:3
Registered: 6/7/04
Re: Overriding createToolTip?  
Jun 22, 2004 3:00 PM (reply 13 of 13)



 
yes because the renderer is not a real component. Its stamped there by JTable. It is JTable which creates the tooltip and gets it to display.
 
This topic has 13 replies on 1 page.