Adding Icon to JTextPane : JTextPane « Swing « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Swing » JTextPane 
14. 38. 9. Adding Icon to JTextPane
Adding Icon to JTextPane
import java.awt.BorderLayout;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class JTextPaneWithIcon {
  public static void main(String args[]) {
    JFrame frame = new JFrame("TextPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);

    Icon icon = new ImageIcon("yourFile.gif");
    JLabel label = new JLabel(icon);
    StyleConstants.setComponent(labelStyle, label);

    try {
      document.insertString(document.getLength()"Ignored", labelStyle);
    catch (BadLocationException badLocationException) {
      System.err.println("Oops");
    }
    
    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300150);
    frame.setVisible(true);
  }
}
14. 38. JTextPane
14. 38. 1. Difference between JEditorPane and JTextPane
14. 38. 2. TabStop and TabSet ClassesTabStop and TabSet Classes
14. 38. 3. Loading a JTextPane with Content: using StyleConstants to set Align, Font size, SpaceLoading a JTextPane with Content: using StyleConstants to set Align, Font size, Space
14. 38. 4. StyleConstants ClassStyleConstants Class
14. 38. 5. Change the Font size of JTextPane
14. 38. 6. SimpleAttribute: Bold, ItalicSimpleAttribute: Bold, Italic
14. 38. 7. SimpleAttribute: Bold, Italic, ColorSimpleAttribute: Bold, Italic, Color
14. 38. 8. Font family
14. 38. 9. Adding Icon to JTextPaneAdding Icon to JTextPane
14. 38. 10. Uses a listener label to display caret and selection status For JTextPaneUses a listener label to display caret and selection status For JTextPane
14. 38. 11. Using Actions with Text Components: JTextPane
14. 38. 12. Editor based on JTextPane
14. 38. 13. Create a right-aligned tab stop at 200 pixels from the left margin
14. 38. 14. Create a center-aligned tab stop at 300 pixels from the left margin
14. 38. 15. Create a decimal-aligned tab stop at 400 pixels from the left margin
14. 38. 16. Create a tab set from the tab stops
14. 38. 17. Setting the Font and Color of Text in a JTextPane Using Styles: Makes text red
14. 38. 18. Setting the Font and Color of Text in a JTextPane Using Styles: Inherits from Red; makes text red and underlined
14. 38. 19. Setting the Font and Color of Text in a JTextPane Using Styles: Makes text 24pts
14. 38. 20. Setting the Font and Color of Text in a JTextPane Using Styles: Makes text italicized
14. 38. 21. Customizing Tab Stops in a JTextPane Component
14. 38. 22. Inserting an Image into a JTextPane Component
14. 38. 23. Inserting a Component into a JTextPane Component
14. 38. 24. Inserting Styled Text in a JTextPane Component
14. 38. 25. A style can have multiple attributes; this one makes text bold and italic
14. 38. 26. Duplicate style
14. 38. 27. Italicize the entire paragraph containing the position 12
14. 38. 28. Listing the Styles Associated with a JTextPane
14. 38. 29. Listing the Attributes in a Style
14. 38. 30. Replace style
14. 38. 31. Set logical style; replaces paragraph style's parent
14. 38. 32. Get logical style and restore it after new paragraph style
14. 38. 33. Determining If a Style Attribute Applies to a Character or the Paragraph
14. 38. 34. Determine if the attribute is a color or a font-related attribute.
14. 38. 35. Sharing Styles Between JTextPanes
14. 38. 36. Background color
14. 38. 37. Foreground color
14. 38. 38. Enumerating the Paragraphs of a JTextPane Component
14. 38. 39. Displaying Simple HTML Files
14. 38. 40. JTextPane Look and Feel
14. 38. 41. A separation of a data from the visual representation.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.