Results 1 to 8 of 8
  1. #1
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default Changing the color of "setToolTipText"

    Can anybody plz explain me how to change the color of this component??:)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One way is to override the createToolTip method of your JComponent that is showing the text. For e.g.,

    Java 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();
          }
        });
      }
    }

  3. #3
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    cool.. thanx for the solution. I appreciate your presence out there. for us.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Y. Progammer View Post
    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.

  5. #5
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    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

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    Java 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;
          }
      
    }
    Look up in this forum and elsewhere use of anonymous inner classes for more information.

  7. #7
    Y. Progammer is offline Member
    Join Date
    Feb 2010
    Posts
    68
    Rep Power
    0

    Default

    Java 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);
        }
    }

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Y. Progammer View Post
    Java Code:
    //...i
            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>");
    //...
    Yup, this is as per my reply in reply#4

Similar Threads

  1. [COLOR="Navy"]execute .bat file in mysql [/COLOR]
    By msankar.ravi in forum Networking
    Replies: 0
    Last Post: 02-24-2010, 05:27 AM
  2. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 08:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •