Search code examples
javacolorsjlabel

How to add Hex font color to a JLabel?


I searched stackOverFlow for answers but I still coudn't find any (only for android. I use commandline). I want to add Hex color code as the font color.

I first used this but I need to add out from JDK given system/defined colors

g2l2.setForeground(Color.BLUE);

Something like this. But didn't work.

g2l2=new JLabel();
g2l2.setLocation(50,60);        
g2l2.setSize(150,30);        
g2l2.setText("Members");        
g2l2.setTextColor(Color.parseColor("#43B7BA"));        
g2l2.setFont(new Font("Calibri Light",Font.BOLD,15));        
g2cont.add(g2l2);    
g2l2.addMouseListener(this);

ERROR: Cannot find symbol Color.parseColor(String) in Location: class Color.


Solution

  • You can try this:

    Color myColor = Color.decode("#43B7BA");
    

    This worked for me once.

    Hope it helped you.

    Regards.

    By the way, you can try also using the "RGB" approach, there are many pages which can get the RGB values from an hex:

    Color redColor = new Color(255,0,0)