Results 1 to 5 of 5

Thread: How to use HTML in Swing Components?

  1. #1
    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"

  2. #2
    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

  3. #3
    Join Date
    May 2008
    Posts
    2,393

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

  4. #4
    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>");

  5. #5
    Join Date
    May 2008
    Posts
    2,015

    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));
    ...

Similar Threads

  1. Layout manager for Swing Components in JAVA
    By afidelino in forum Software Development
    Replies: 3
    Last Post: 30-01-2011, 03:28 AM
  2. Jms + swing
    By Prince$O$Persia in forum Software Development
    Replies: 5
    Last Post: 24-07-2010, 03:30 AM
  3. Java SWING Components - Table Models
    By Isiah in forum Software Development
    Replies: 5
    Last Post: 21-07-2010, 01:34 AM
  4. What are JFC and Swing?
    By DANIEL 602 in forum Software Development
    Replies: 5
    Last Post: 14-02-2010, 03:11 AM
  5. what is the difference between Swing and Awt?
    By Deepest BLUE in forum Software Development
    Replies: 4
    Last Post: 27-11-2009, 05:52 PM

Posting Permissions

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