Can anybody plz explain me how to change the color of this component??:)
Printable View
Can anybody plz explain me how to change the color of this component??:)
One way is to override the createToolTip method of your JComponent that is showing the text. For e.g.,
Code:import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.*;
public class ToolTipTextColor {
private static void createAndShowUI() {
JPanel tooltipPanel = new JPanel() {
@Override
public JToolTip createToolTip() {
JToolTip tooltip = super.createToolTip();
tooltip.setFont(tooltip.getFont().deriveFont(Font.BOLD, 32));
tooltip.setForeground(Color.blue);
tooltip.setOpaque(false);
return tooltip;
}
};
tooltipPanel.setPreferredSize(new Dimension(100, 100));
tooltipPanel.setBackground(Color.pink);
tooltipPanel.setToolTipText("hello");
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(400, 300));
panel.add(tooltipPanel);
JFrame frame = new JFrame("ToolTipTextColor");
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
cool.. thanx for the solution. I appreciate your presence out there. for us.
Your welcome. The other solution, and the one to use if you want to change the color of the font of all the tool tips in your app is to change the look and feel default foreground color for tooltips via the UIManager. For more on this, and to see which property to change, I refer you to Rob Camick's article in his indispensable blog that you can find here: UIManager Defaults « Java Tips Weblog
If you're smart, you'll bookmark this site.
Quote:
private static void createAndShowUI() {
JPanel tooltipPanel = new JPanel() {
@Override
public JToolTip createToolTip() {
JToolTip tooltip = super.createToolTip();
tooltip.setFont(tooltip.getFont().deriveFont(Font. BOLD, 32));
tooltip.setForeground(Color.blue);
tooltip.setOpaque(false);
return tooltip;
}
};
Buddy I didn't understand this much of your coding ..... you added a panel on a panel on a frame..
JPanel tooltipPanel = new JPanel() {/**/ };
what this statement actually means?? i mean {}; i never used these after "JPanel()".
and the rest of the quoted code .. plz explain
I'm subclassing JPanel but doing it anonymously -- not giving the class a name and doing it inside of another class. It's similar to doing this:
Look up in this forum and elsewhere use of anonymous inner classes for more information.Code:class MyPanel extends JPanel {
public JToolTip createToolTip() {
JToolTip tooltip = super.createToolTip();
tooltip.setFont(tooltip.getFont().deriveFont(Font.BOLD, 32));
tooltip.setForeground(Color.blue);
tooltip.setOpaque(false);
return tooltip;
}
}
Code:import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
public class Demo extends JPanel {
Demo() {
UIManager.put("ToolTip.background", new ColorUIResource(255, 255, 255));
setToolTipText("<html>This is also a way <br>to change the color<br> of the tooltip in your <br>application!</html>");
}
public static void main(String[] args) {
javax.swing.JFrame frame = new javax.swing.JFrame("Two-Dimensional Graph Sketcher");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.add(new Demo());
frame.setSize(100, 100);
frame.setVisible(true);
}
}