Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 5:21 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: JTextPane example
PostPosted: Tue Aug 02, 2011 12:09 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

JTextPane gives you more ability to your text area ,this is example show you how to print text with different colors and same also different fonts .
Code:


 

import java
.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

public class FrameTextArea extends JFrame {

    private StyleContext context = new StyleContext();
    private StyledDocument document = new DefaultStyledDocument(context);
    private javax.swing.text.Style style = context.getStyle(StyleContext.DEFAULT_STYLE);
    private final JTextPane textArea = new JTextPane(document);
    private final JScrollPane TextAreaScroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    private JButton jButton = new JButton("Clear");

    public FrameTextArea() {

        setSize(400, 200);
        setTitle("Text Pane");
        StyleConstants.setAlignment(style, StyleConstants.ALIGN_LEFT);
        StyleConstants.setFontSize(style, 10);
        StyleConstants.setSpaceAbove(style, 1);
        StyleConstants.setSpaceBelow(style, 1);
        setLayout(new FlowLayout());
        jButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    document.remove(0, document.getLength());
                } catch (BadLocationException ex) {
                    Logger.getLogger(FrameTextArea.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });


        add(jButton);
        add(TextAreaScroll);
        StyleConstants.setForeground(style, Color.red);
        try {
            StyleConstants.setForeground(style, Color.red);
            document.insertString(document.getLength(), "\n" + new Date() + ": " + "Red Text", style);
            StyleConstants.setForeground(style, Color.PINK);
            document.insertString(document.getLength(), "\n" + new Date() + ": " + "PINK Text", style);

            StyleConstants.setForeground(style, Color.black);
            document.insertString(document.getLength(), "\n" + "<a href=\"http://www.codemiles.com\" >Link</a>", style);
        } catch (BadLocationException ex) {
            Logger.getLogger(FrameTextArea.class.getName()).log(Level.SEVERE, null, ex);
        }


        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
       FrameTextArea frameTextArea= new FrameTextArea();
    }
}
 

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team