Gustavo, já tive que fazeralgo parecidono passado.Como estou de bom humor, vou postar um exemplo aqui q eu peguei num site java japonês uma vez:
[code]
import javax.swing.;
import javax.swing.text.;
import javax.swing.event.;
import java.awt.event.;
import java.awt.;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.;
public class TextPaneTest extends JFrame implements ActionListener, CaretListener{
protected JTextPane textPane;
protected DefaultStyledDocument doc;
protected StyleContext sc;
protected JToolBar toolBar;
protected JComboBox comboFonts; /* Fontes*/
protected JComboBox comboSizes; /* Fontes-TAMs */
protected JToggleButton toggleB; /* */
protected JToggleButton toggleI; /* */
protected JToggleButton toggleU; /* */
protected JToggleButton toggleS; /* */
protected JComboBox comboColor; /* */
protected String currentFontName = "";
protected int currentFontSize = 0;
protected boolean flag = false;
protected String[] colorHTML = {"#000000", "#0000FF", "#00FF00",
"#00FFFF", "#FF0000", "#FF00FF", "#FFFF00", "#FFFFFF"};
protected RTFEditorKit rtfEditor;
public static void main(String[] args){
TextPaneTest test = new TextPaneTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.setVisible(true);
}
TextPaneTest(){
setTitle("TextPaneTest Test");
setBounds( 10, 10, 500, 300);
textPane = new JTextPane();
JScrollPane scroll = new JScrollPane(textPane,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(scroll, BorderLayout.CENTER);
sc = new StyleContext();
doc = new DefaultStyledDocument(sc);
textPane.setDocument(doc);
textPane.addCaretListener(this);
/* */
rtfEditor = new RTFEditorKit();
JMenuBar menuBar = createMenuBar();
setJMenuBar(menuBar);
initToolbar();
getContentPane().add(toolBar, BorderLayout.NORTH);
/* */
initDocument();
}
protected JMenuBar createMenuBar(){
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File", true);
menuBar.add(fileMenu);
JMenuItem newItem = new JMenuItem("New");
fileMenu.add(newItem);
newItem.addActionListener(this);
newItem.setActionCommand("newItem");
JMenuItem openItem = new JMenuItem("Open");
fileMenu.add(openItem);
openItem.addActionListener(this);
openItem.setActionCommand("openItem");
JMenuItem saveItem = new JMenuItem("Save");
fileMenu.add(saveItem);
saveItem.addActionListener(this);
saveItem.setActionCommand("saveItem");
fileMenu.addSeparator();
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(this);
exitItem.setActionCommand("exitItem");
return menuBar;
}
protected void initDocument(){
StringBuffer sb = new StringBuffer();
sb.append("Texto anexado\n");
sb.append("Ao TextPane!!!");
try{
/**/
doc.insertString(0, new String(sb),
sc.getStyle(StyleContext.DEFAULT_STYLE));
}catch (BadLocationException ble){
System.err.println("Impossivel Exibir o Texto!!!");
}
}
protected void initToolbar(){
toolBar = new JToolBar();
toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
/* */
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
String familyName[] = ge.getAvailableFontFamilyNames();
/* */
comboFonts = new JComboBox(familyName);
comboFonts.setMaximumSize(comboFonts.getPreferredSize());
comboFonts.addActionListener(this);
comboFonts.setActionCommand("comboFonts");
toolBar.add(comboFonts);
/* */
comboSizes = new JComboBox(new String[] {"8", "9", "10",
"11", "12", "14", "16", "18", "20", "22", "24", "26",
"28", "36", "48", "72"});
comboSizes.setMaximumSize(comboSizes.getPreferredSize());
comboSizes.addActionListener(this);
comboSizes.setActionCommand("comboSizes");
toolBar.add(comboSizes);
toolBar.addSeparator();
/* */
toggleB = new JToggleButton("<html><b>B</b></html>");
toggleB.setPreferredSize(new Dimension(26, 26));
toggleB.addActionListener(this);
toggleB.setActionCommand("toggleB");
toolBar.add(toggleB);
/* ŽÎ‘Ì ‘I‘ð—pƒgƒOƒ‹ƒ{ƒ^ƒ“ */
toggleI = new JToggleButton("<html><i>I</i></html>");
toolBar.add(toggleI);
toggleI.addActionListener(this);
toggleI.setActionCommand("toggleI");
toggleI.setPreferredSize(new Dimension(26, 26));
/* */
toggleU = new JToggleButton("<html><u>U</u></html>");
toolBar.add(toggleU);
toggleU.addActionListener(this);
toggleU.setActionCommand("toggleU");
toggleU.setPreferredSize(new Dimension(26, 26));
/* */
toggleS = new JToggleButton("<html><s>S</s></html>");
toolBar.add(toggleS);
toggleS.addActionListener(this);
toggleS.setActionCommand("toggleS");
toggleS.setPreferredSize(new Dimension(26, 26));
toolBar.addSeparator();
/* */
DefaultComboBoxModel colorModel = new DefaultComboBoxModel();
for (int i = 0 ; i < 8; i++){
/* */
StringBuffer sb = new StringBuffer();
sb.append("<html><font color=\"");
sb.append(colorHTML[i]);
sb.append("\">¡</font></html>");
colorModel.addElement(new String(sb));
}
comboColor = new JComboBox(colorModel);
comboColor.setMaximumSize(comboColor.getPreferredSize());
comboColor.addActionListener(this);
comboColor.setActionCommand("comboColor");
toolBar.add(comboColor);
}
public void actionPerformed(ActionEvent e){
if (flag){
/* */
return;
}
String actionCommand = e.getActionCommand();
if (actionCommand.equals("exitItem")){
System.exit(0);
}else if ((actionCommand.equals("newItem")) ||
(actionCommand.equals("openItem")) ||
(actionCommand.equals("saveItem"))){
fileOperation(actionCommand);
}else{
MutableAttributeSet attr = new SimpleAttributeSet();
if (actionCommand.equals("comboFonts")){
/* */
String fontName = comboFonts.getSelectedItem().toString();
StyleConstants.setFontFamily(attr, fontName);
}else if (actionCommand.equals("comboSizes")){
/* */
int fontSize = 0;
try{
fontSize = Integer.parseInt(comboSizes.
getSelectedItem().toString());
}catch (NumberFormatException ex){
return;
}
StyleConstants.setFontSize(attr, fontSize);
}else if (actionCommand.equals("toggleB")){
/* */
StyleConstants.setBold(attr, toggleB.isSelected());
}else if (actionCommand.equals("toggleI")){
/* */
StyleConstants.setItalic(attr, toggleI.isSelected());
}else if (actionCommand.equals("toggleU")){
/* */
StyleConstants.setUnderline(attr, toggleU.isSelected());
}else if (actionCommand.equals("toggleS")){
/* */
StyleConstants.setStrikeThrough(attr, toggleS.isSelected());
}else if (actionCommand.equals("comboColor")){
/* */
int col = comboColor.getSelectedIndex();
int b = (col % 2) * 255;
int g = ((col / 2) % 2) * 255;
int r = ((col / 4) % 2) * 255;
StyleConstants.setForeground(attr, new Color(r, g, b));
}else{
return;
}
setAttributeSet(attr);
}
textPane.requestFocusInWindow();
}
protected void fileOperation(String actionCommand){
JFileChooser chooser = new JFileChooser();
RtfFilter filter = new RtfFilter();
chooser.setFileFilter(filter);
if (actionCommand.equals("newItem")){
/* */
doc = new DefaultStyledDocument(sc);
textPane.setDocument(doc);
}else if (actionCommand.equals("openItem")){
/* */
doc = new DefaultStyledDocument(sc);
if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION){
return;
}
File fChoosen = chooser.getSelectedFile();
try{
InputStream in = new FileInputStream(fChoosen);
rtfEditor.read(in, doc, 0);
in.close();
}catch(Exception ex){
ex.printStackTrace();
}
textPane.setDocument(doc);
}else if (actionCommand.equals("saveItem")){
/* */
if (chooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION){
return;
}
File fChoosen = chooser.getSelectedFile();
try{
OutputStream out = new FileOutputStream(fChoosen);
rtfEditor.write(out, doc, 0, doc.getLength());
out.close();
}catch(Exception ex){
ex.printStackTrace();
}
}else{
return;
}
}
protected void setAttributeSet(AttributeSet attr) {
/* */
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
doc.setCharacterAttributes(start, end - start, attr, false);
}
public void caretUpdate(CaretEvent e){
flag = true;
int p = textPane.getSelectionStart();
AttributeSet atrr = doc.getCharacterElement(p).getAttributes();
String name = StyleConstants.getFontFamily(atrr);
/* */
if (!currentFontName.equals(name)){
currentFontName = name;
comboFonts.setSelectedItem(name);
}
int size = StyleConstants.getFontSize(atrr);
/* */
if (currentFontSize != size){
currentFontSize = size;
comboSizes.setSelectedItem(Integer.toString(size));
}
/* */
toggleB.setSelected(StyleConstants.isBold(atrr));
toggleI.setSelected(StyleConstants.isItalic(atrr));
toggleU.setSelected(StyleConstants.isUnderline(atrr));
toggleS.setSelected(StyleConstants.isStrikeThrough(atrr));
/* */
Color col = StyleConstants.getForeground(atrr);
int r = (col.getRed() / 255) * 4;
int g = (col.getGreen() / 255) * 2;
int b = col.getBlue() / 255;
comboColor.setSelectedIndex(r + g + b);
flag = false;
}
}[/code]
E um RTFilter só para compilar:
import java.io.File;
public class RtfFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept (File f) {
return f.getName ().toLowerCase ().endsWith (".rtf")
|| f.isDirectory ();
}
public String getDescription () {
return "Rtf files (*.rtf)";
}
} // class RtfFilter
Arrume os filtros comforme sua necessidade(Txt,Rtf…) e se vira! 