Saturday, August 24, 2013

JTextPane

JTextPane is a text component that you can apply different styles or attributes set to different parts of the text. Placing any component or image to the JTextPane object is allowed. In the example code below, each line of text int text pane is marked up with with attributes (color, italic, bold, strikethrough, font name, and font size).

Constructors of the JTextPane class:
-JTextPane()
creates a new text pane.
-JTextPane(StyleDocument doc)
creates a enw text pane with a document model.

Useful methods of the JTextPane class:
-getCharacterAttributes():AttributeSet
returns the character attributes at the current caret position.
-insertComponent(Component c):void
adds the specified component to the text pane at the current caret position.
-insertIcon(Icon icon):void
adds the specified icon image to the text pane at the current caret position.
-setCaretPosition(int pos):void
specifies the text insertion caret position.
-setCharacterAttributes(AttributeSet,boolean replace):void
specifies the character attributes.


In the example below, the JTextPane component is used to display text, image, and a combobox. Every line of the text is styled differently. The setCharacterAttributes method is used to apply styles to the text. You need to define the attribute sets and supply it to this method. A single style attribute can be constructed by using the NameStyle class. It is the sub-class of the StyleContext classs. The StyleContext class has a method called addAttribute that allows you to combine the attribute object with another attribute so that it creates a set of attributes.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyleContext.NamedStyle;

class JTextPaneShow extends JFrame implements ItemListener{
JTextPane txtpane;
JTextPaneShow(String title){
setTitle(title);
setSize(500,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contpane=getContentPane();
contpane.setLayout(new BorderLayout());
txtpane=new JTextPane();
setStyles("This is blue text.\n",Color.BLUE,false,false,false,"Arial",20);
setStyles("This is italic red text.\n",Color.RED,true,false,false,"Arial",20);
setStyles("This is black bold text.\n",Color.BLACK,false,true,false,"Arial",20);
setStyles("This is strikethrough green text.\n",Color.GREEN,true,false,true,"Arial",20);
setStyles("This is arial bigger text.\n",Color.CYAN,false,false,false,"Arial",30);
setStyles("Placing image on JTextPane is possible.Great!\n",Color.RED,false,false,false,"Times New Roman",15);
placeImage("d:/tr.png");
placeComp();
contpane.add(txtpane,BorderLayout.CENTER);
txtpane.setEditable(false);
setVisible(true);
}
public void setStyles(String text, Color fcolor, boolean italic,boolean bold, boolean strikethrough, String fname, int fsize){
StyleContext sty=StyleContext.getDefaultStyleContext();
NamedStyle nstyle=sty.new NamedStyle();
nstyle.addAttribute(StyleConstants.Foreground,fcolor);
AttributeSet attset=sty.addAttribute(nstyle, StyleConstants.Italic, italic);
attset=sty.addAttribute(attset,StyleConstants.Bold, bold);
attset=sty.addAttribute(attset,StyleConstants.StrikeThrough, strikethrough);
attset=sty.addAttribute(attset,StyleConstants.FontFamily, fname);
attset=sty.addAttribute(attset,StyleConstants.Size, fsize);
txtpane.setCharacterAttributes(attset, false);
setCaretPos();
txtpane.replaceSelection(text);
     
}
public void placeComp(){
setCaretPos();
Object[] colorcode={0xffffff,0xff2345,0xf00999,0xfffff,0xf00ff0};
JComboBox comcolor=new JComboBox(colorcode);
comcolor.addItemListener(this);
txtpane.insertComponent(comcolor);
}
public void placeImage(String imgfile){
setCaretPos();
txtpane.insertIcon(new ImageIcon(imgfile));

}
public void setCaretPos(){
int len = txtpane.getDocument().getLength();
       txtpane.setCaretPosition(len);
     
}

public void itemStateChanged(ItemEvent e){
JComboBox cb=(JComboBox)e.getSource();
txtpane.setBackground(new Color((Integer) cb.getSelectedItem()));
}



}

public class JFrameSwing {
public static void main(String[] args){
new JTextPaneShow("JTextPane");
}
}

JTextPane Swing

2 comments:

  1. Wow! Thank you! I continuously needed to write on my blog something like that. Can I implement a fragment of your post to my site? cleaning services company singapore


    ReplyDelete