Lost Password?

  #1 (permalink)  
Old 09-23-2007, 09:59 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,437
Last Blog:
Object Oriented Design...
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
Default HTML with Swing Components

One useful feature of Swing GUI's many people overlook is the ability to use simple HTML tags within swing components. This tutorial assumes you know how to create a GUI in swing and add components. If you do not know how to do this, it is a good idea to read one of my previous tutorials. You can view my Tutorial Index here: http://forum.codecall.net/java-tutor...ial-index.html

Lets first create a basic GUI with a JButton and JLabel:

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6.  
  7. public class HTMLJButton {
  8.    
  9.     public HTMLJButton(){
  10.         JFrame mainFrame = new JFrame("HTML");
  11.         mainFrame.setLayout(new java.awt.FlowLayout());
  12.  
  13.         JButton button1 = new JButton("JButton Text");
  14.         JLabel label1 = new JLabel("JLabel Text");
  15.        
  16.         mainFrame.add(button1);
  17.         mainFrame.add(label1);
  18.        
  19.         mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
  20.         mainFrame.setVisible(true);
  21.         mainFrame.pack();
  22.     }
  23.    
  24.     public static void main(String[] args){
  25.         new HTMLJButton();
  26.     }
  27. }

But what if you want the text on the button to be a different color? You can use the HTML font tags inside the JButton string parameter.

Such as:
Java]JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
"<br /><font color='#00FF00'>Blue Text</font></html>");[/highlight]

Not only can you modify fonts, you can also insert images:
[highlight="Java Code:
    JLabel label1 = new JLabel("<html><img src='http://www.google.com/intl/en_ALL/images/logo.gif'></img></html>");

There is much more you can do with HTML, just note, when you do insert HTML into a componnents string paramater, you need to surround the HTML with the opening and closing HTML tags <html></html> and you need to use single quotes since the entire string is bound by double quotes.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6.  
  7. public class HTMLJButton {
  8.    
  9.     public HTMLJButton(){
  10.         JFrame mainFrame = new JFrame("HTML");
  11.         mainFrame.setLayout(new java.awt.FlowLayout());
  12.  
  13.         JButton button1 = new JButton("<html><font color='#FF0000'>JButton Text</font></html>");
  14.         JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
  15.                 "<br /><font color='#00FF00'>Blue Text</font>" +
  16.                 "<br /><font color='#0000FF'>Green Text</font>" +
  17.                 "<br /><font color='#000000'>Black Text</font>" +
  18.                 "<br /><font color='#FFFFFF'>Blue Text</font></html>");
  19.        
  20.         mainFrame.add(button1);
  21.         mainFrame.add(label1);
  22.        
  23.         mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
  24.         mainFrame.setVisible(true);
  25.         mainFrame.pack();
  26.     }
  27.    
  28.     public static void main(String[] args){
  29.         new HTMLJButton();
  30.     }
  31. }
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
About HTML Onur Tutorials, Classes and Code 0 08-28-2007 06:13 PM
HTML Basic Formatting clookid Tutorials, Classes and Code 14 03-06-2007 03:10 PM
HTML Introduction clookid Tutorials, Classes and Code 5 01-08-2007 09:43 PM
What is HTML, DHTML and XHTML? Lop HTML Programming 5 08-09-2006 11:00 AM
Removing HTML from a String using ColdFusion roger ASP, ASP.NET and Coldfusion 0 05-11-2006 10:19 PM


All times are GMT -5. The time now is 10:28 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads