• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Customizing JToolTip

 
jagan bisoyi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jagan bisoyi
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Maneesh for your immediate and correct reply.
My problem is solved almost.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic