Swing : Using HTML in button and Label text

You can decorate the buttons and label using the html text. You no need to call methods to set color, font size, font style etc for the Button and labels. Just specify them through the HTML tag.


import javax.swing.*;
import java.awt.*;

class HTMLButton
{
    public static void main(String ar[ ] )
    {
         String html=
        "<html><table border=1>"
        +"<tr><td>One</td><td>1</td></tr>"
        +"<tr><td>Two</td><td>2</td></tr>"
        +"</table>";
        
        JFrame frame = new JFrame(  );
        JButton button = new JButton(html);

         String html2=
        "<html><table border=0>"
        +"<tr><td><font color=\"red\">WEL</font></td><td><font color=\"blue\">COME</font></td></tr>"
        +"</table>";
        JLabel label=new JLabel(html2);
        
        frame.getContentPane(  ).add( button );
        frame.getContentPane(  ).add( label );
        frame.getContentPane(  ).setLayout(new FlowLayout( ));
        frame.pack(  );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setVisible(true);

    }
}


No comments:

Popular Posts