How to use HTML in Swing Components?
Hi friends,
I really want some help from you guys again. Last time your help was tremendous. So thought that posting to you would be helpful. I want to specify that a component's text has HTML format. I have tried lot of things but was not getting the desired output. So, please tell me how to use HTML in Swing Components.?? Please help me as soon as possible.!!:notworthy
Re: How to use HTML in Swing Components?
You should be knowing some basics of the swing components. Many swing components display a text string as part of their GUI. A component's text is displayed in one font and color by default. And this comes on one line. The font and color of text that is contained in a container can be determined by invoking some component's methods. These methods are setFont and setForeground methods. you can use HTML, when you want to mix fonts or colors within the text.
Re: How to use HTML in Swing Components?
By using the setFont and setForeground methods, you can change the font and color of a component's text. You will have to invoke only that methods. The following code can help you for creating a label. Also you can set it's font and color by using the same code :
Code:
label = new JLabel("Label Example");
label.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
label.setForeground(new Color(0x00ffdd));
Re: How to use HTML in Swing Components?
You can specify a component's text which is having the HTML format. You will have to put the <html> tag at the beginning of the text, when you want to specify that a component's text has HTML formatting. Once you use this, you can use any valid HTML in the remainder. If you want to use HTML in a button, you can use the following code :
HTML Code:
button = new JButton("<html><b><u>Trial</u>Example</b><br>lhere</html>");
Re: How to use HTML in Swing Components?
I have provided you the code so that you can understand more clearly. The following code specifies the text formatting for the two buttons :
HTML Code:
b1 = new JButton("<html><center><b><u>D</u>Example</b><br>"
+ "<font color=#0000dd>First Button</font>",
leftButtonIcon);
Font font = b1.getFont().deriveFont(Font.PLAIN);
b1.setFont(font);
...
b2 = new JButton("Second Button", rightButtonIcon);
b2.setFont(font);
b2.setForeground(new Color(0x0000dd));
...