+ Reply to Thread
Results 1 to 1 of 1

Thread: HTML with Swing Components

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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:

    Code:
    package tutorials;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class HTMLJButton {
    	
    	public HTMLJButton(){
    		JFrame mainFrame = new JFrame("HTML");
    		mainFrame.setLayout(new java.awt.FlowLayout());
    
    		JButton button1 = new JButton("JButton Text");
    		JLabel label1 = new JLabel("JLabel Text");
    		
    		mainFrame.add(button1);
    		mainFrame.add(label1);
    		
    		mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
    		mainFrame.setVisible(true);
    		mainFrame.pack();
    	}
    	
    	public static void main(String[] args){ 
    		new HTMLJButton();
    	}
    }
    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:
    Code:
    JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
    		"<br /><font color='#00FF00'>Blue Text</font></html>");
    Not only can you modify fonts, you can also insert images:
    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.

    Code:
    package tutorials;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class HTMLJButton {
    	
    	public HTMLJButton(){
    		JFrame mainFrame = new JFrame("HTML");
    		mainFrame.setLayout(new java.awt.FlowLayout());
    
    		JButton button1 = new JButton("<html><font color='#FF0000'>JButton Text</font></html>");
    		JLabel label1 = new JLabel("<html><font color='#FF0000'>Red Text</font>" +
    				"<br /><font color='#00FF00'>Blue Text</font>" +
    				"<br /><font color='#0000FF'>Green Text</font>" +
    				"<br /><font color='#000000'>Black Text</font>" +
    				"<br /><font color='#FFFFFF'>Blue Text</font></html>");
    		
    		mainFrame.add(button1);
    		mainFrame.add(label1);
    		
    		mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
    		mainFrame.setVisible(true);
    		mainFrame.pack();
    	}
    	
    	public static void main(String[] args){ 
    		new HTMLJButton();
    	}
    }
    Originally posted as HTML with Swing Components
    Last edited by John; 08-01-2010 at 09:26 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Swing Thread Safety - How to properly modify components.
    By farrell2k in forum Java Tutorials
    Replies: 0
    Last Post: 06-18-2010, 10:08 PM
  2. setting minimum size of swing Components
    By ClemsonCS in forum Java Help
    Replies: 0
    Last Post: 04-14-2009, 11:11 PM
  3. .NET and serviced components
    By hurricanesoftwares in forum Software Development Tools
    Replies: 2
    Last Post: 10-03-2008, 01:03 PM
  4. WinFX Components
    By brackett in forum C# Programming
    Replies: 2
    Last Post: 06-01-2006, 06:24 AM

Tags for this Thread

Bookmarks

Posting Permissions

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