This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Customizing JToolTip Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Customizing JToolTip" Watch "Customizing JToolTip" New topic
Author

Customizing JToolTip

jagan bisoyi
Greenhorn

Joined: Mar 09, 2008
Posts: 15
Hello ladies and gentle man,

Can I customize a JToolTip so that I can add a JPanel to it?
i.e is it possible add component to a JToolTip.
A sample code will make a great favor.

Jagan
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10699
    
    9

Originally posted by jagan bisoyi:
Hello ladies and gentle man,

Can I customize a JToolTip so that I can add a JPanel to it?
i.e is it possible add component to a JToolTip.
A sample code will make a great favor.

Jagan


Yes you can do it. Check out JComponent#createToolTip. JToolTip extends Container so you can definitely add components to it. If it is just formatting you are interested in, I would suggest you use HTML in your tooltip.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
jagan bisoyi
Greenhorn

Joined: Mar 09, 2008
Posts: 15
Hi Maneesh,


Thanks for the reply. Still I am facing a problem.
JToolTip doesn't shows the Jbutton what I have added.
I also tried to add JPanel but A run time Exception rises to tell that,
"Can't add a Window to a container!"

Here is my code:

<blockquote>code:
<pre name="code" class="core">import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolTip;
public class ToolTipTest {
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton b = new JButton("Hello, World") {
public JToolTip createToolTip() {

JButton b1= new JButton("Button for tooltip");
b1.setBackground(Color.MAGENTA);
JToolTip tip = super.createToolTip();
tip.add(b1);

tip.setBackground(Color.YELLOW);
tip.setForeground(Color.RED);
return tip;
}
};
frame.add(b,"Center");
b.setToolTipText("asdf");

frame.setSize(300, 200);
frame.setVisible(true);
}
}
</pre>
</blockquote>


Please help me in this regard if you have any solution.

Jagan
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10699
    
    9

<blockquote>code:
<pre name="code" class="core">
public class MyToolTip extends JToolTip {

public MyToolTip() {
setLayout(new GridLayout(2,1));

JPanel panel=new JPanel();
panel.setOpaque(false);
panel.add(new JButton("OK"));

add(new JLabel(getToolTipText()));
add(panel);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(200,100);
}

}
</pre>
</blockquote>
jagan bisoyi
Greenhorn

Joined: Mar 09, 2008
Posts: 15
Thanks Maneesh for your immediate and correct reply.
My problem is solved almost.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Customizing JToolTip