File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextPane --find the mystery Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "JTextPane --find the mystery " Watch "JTextPane --find the mystery " New topic
Author

JTextPane --find the mystery

selvas kumars
Ranch Hand

Joined: Jan 06, 2001
Posts: 115
Hi swing gurus,
Here is the sample file having textpane set to
html editor kit.I am going through each attributeset of the
character and changing the atribute still I am not able to make the necessary changes in UI..sometimes it works and sometimes not...(my version is jdk1.3.0)..
My reuirement is like taking out html document from the textpane
by calling textPane.getText()(I will get o/p in html format).
I tried out giving StyledEditorKit.BoldAction in action listener..it will work well for the selected text and the time
i make a getText() on textpane i won't have the latest html doc seen in UI.

Run the sample by changing this textpane.setText("hello how are you"); to textpane.setText("hello how are you");
Selection any portion and try to make all changes and see...
Give me a solution to solve the problem...since i will set an html doc into a textpane and make change and get it back again a latest html doc seen in UI...How to proceed on this???
Try out our mind on this .....
Thanks & Regards,
Silva.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.text.StyledDocument;
import javax.swing.text.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.event.*;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import java.io.*;
import javax.swing.text.html.HTML;

public class Demo11 extends JFrame implements ActionListener {
public static JTextPane textpane= new JTextPane();
JPanel panel = new JPanel(new BorderLayout());
JTextPane textpane1 = new JTextPane();

String s1;
JButton button = new JButton("print1");
String news="";
Container c = getContentPane();
HTMLDocument htmlDoc = new HTMLDocument();
HTMLEditorKit htmlEditorKit;
JButton bold;
JButton italic;
JButton underline;

public Demo11(){
c.setLayout(new BorderLayout());

htmlEditorKit = new HTMLEditorKit();
textpane.setEditorKit(htmlEditorKit);

button.addActionListener(this);
bold = new JButton("Bold");
bold.addActionListener(this);
italic = new JButton("italic");
underline = new JButton("underline");
italic.addActionListener(this);
underline.addActionListener(this);
panel.add("Center",button);
JPanel pan = new JPanel(new GridLayout(1,3));
pan.add(bold);
pan.add(italic);
pan.add(underline);
c.add("North",pan);
c.add("Center",textpane);
c.add("South",panel);
textpane.setPreferredSize(new Dimension(300,400));
textpane.setText("hello how are you");
pack();
show();
}
public void actionPerformed(ActionEvent e){
int startpostion = 0;
int mark = textpane.getCaret().getMark();
int dot = textpane.getCaret().getDot();
int end = 0;
if(mark<dot){>
startpostion =mark;
end = dot;
}else {
startpostion = dot;
end =mark;
}
if(e.getSource()==button)
System.out.println(textpane.getText());

if(e.getSource()==bold){
Element element = textpane.getStyledDocument().getCharacterElement(startpostion);
AttributeSet attrs = element.getAttributes();
SimpleAttributeSet simpleAttr = new SimpleAttributeSet();
simpleAttr.addAttributes(attrs);
boolean firstCharBold = false;
if(StyleConstants.isBold(attrs)){
firstCharBold = false;
}else {
firstCharBold = true;

}
for(int i =startpostion ;i<=end;i++){
AttributeSet attrs1 = textpane.getStyledDocument().getCharacterElement(i).getAttributes();
SimpleAttributeSet simpleAttr1 = new SimpleAttributeSet();
simpleAttr1.addAttributes(attrs1);
if(firstCharBold){
StyleConstants.setBold(simpleAttr1,true);
}else{
simpleAttr1.removeAttribute(HTML.Tag.B);
StyleConstants.setBold(simpleAttr1,false);
}
textpane.getStyledDocument().setCharacterAttributes(i ,1, simpleAttr1, true);

}
}
if(e.getSource()==italic){
Element element = textpane.getStyledDocument().getCharacterElement(startpostion);
AttributeSet attrs = element.getAttributes();
SimpleAttributeSet simpleAttr = new SimpleAttributeSet();
simpleAttr.addAttributes(attrs);
boolean firstCharItalic = false;
if(StyleConstants.isItalic(attrs)){
firstCharItalic = false;
}else firstCharItalic = true;

for(int i =startpostion ;i<end;i++){>

AttributeSet attrs1 = textpane.getStyledDocument().getCharacterElement(i).getAttributes();
SimpleAttributeSet simpleAttr1 = new SimpleAttributeSet();
simpleAttr1.addAttributes(attrs1);
if(firstCharItalic){
StyleConstants.setItalic(simpleAttr1,true);
}else{
simpleAttr1.removeAttribute(HTML.Tag.I);
StyleConstants.setItalic(simpleAttr1,false);
}
textpane.getStyledDocument().setCharacterAttributes(i ,1, simpleAttr1, true);

}
}
if(e.getSource()==underline){
Element element = textpane.getStyledDocument().getCharacterElement(startpostion);
AttributeSet attrs = element.getAttributes();
SimpleAttributeSet simpleAttr = new SimpleAttributeSet();
simpleAttr.addAttributes(attrs);
boolean firstCharUnderline = false;
if(StyleConstants.isUnderline(attrs)){
firstCharUnderline = false;
}else firstCharUnderline = true;
for(int i =startpostion ;i<end;i++){>

AttributeSet attrs1 = textpane.getStyledDocument().getCharacterElement(i).getAttributes();
SimpleAttributeSet simpleAttr1 = new SimpleAttributeSet();
simpleAttr1.addAttributes(attrs1);
if(firstCharUnderline){
StyleConstants.setUnderline(simpleAttr1,true);
}else{
simpleAttr1.removeAttribute(HTML.Tag.U);
StyleConstants.setUnderline(simpleAttr1,false);
}
textpane.getStyledDocument().setCharacterAttributes(i ,1, simpleAttr1, true);
}
}
}

public static void main(String args[]){
new Demo11();

}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextPane --find the mystery
 
Similar Threads
JTextPane not reflecting changes
Scaling
Alignment for centering text in JTextPane does not work
JTextPane +insert text,image with style
run this file