Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to use HTML in Swing Components?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-02-2010
Member
 
Join Date: Jul 2006
Posts: 218
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.!!

__________________
~*~Silent~Kid~*~
"To The World You May Be Just One Person, But To One Person You May Be The World"
Reply With Quote
  #2  
Old 12-02-2010
Member
 
Join Date: Jul 2006
Posts: 289
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.
__________________
Signatures reduce available bandwidth
Reply With Quote
  #3  
Old 12-02-2010
Member
 
Join Date: May 2008
Posts: 2,384
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));
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 12-02-2010
Member
 
Join Date: Nov 2005
Posts: 1,323
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>");
Reply With Quote
  #5  
Old 12-02-2010
Member
 
Join Date: May 2008
Posts: 2,008
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));
...
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use HTML in Swing Components?"
Thread Thread Starter Forum Replies Last Post
Layout manager for Swing Components in JAVA afidelino Software Development 3 30-01-2011 04:28 AM
Jms + swing Prince$O$Persia Software Development 5 24-07-2010 04:30 AM
Java SWING Components - Table Models Isiah Software Development 5 21-07-2010 02:34 AM
What are JFC and Swing? DANIEL 602 Software Development 5 14-02-2010 04:11 AM
what is the difference between Swing and Awt? Deepest BLUE Software Development 4 27-11-2009 06:52 PM


All times are GMT +5.5. The time now is 02:49 PM.