Hello,
I am creating an application and using swings to change the font of selected text.... using JTextPane i was able to change the font of normal text but i am not able to change font of selected text. consider the following code
////////////////////////
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class Multi {
static JFrame frame;
static JMenuBar mbr;
static JToolBar tb;
static JButton b1;
static JMenu mnu;
static JMenuItem mni;
static Container content;
static JTextPane pane;
static SimpleAttributeSet set;
static Document doc;
static Choice fontlist;
static Toolkit tkit;
public static void main(String args[]) {
frame = new JFrame("Multiattributed text");
mbr=new JMenuBar();
tb=new JToolBar();
tb.setLayout(new GridLayout(1,2));
tkit=Toolkit.getDefaultToolkit();
b1=new JButton("Hello");
fontlist=new Choice();
tb.add(b1);
tb.add(fontlist);
tb.add(b1);
String names[]=tkit.getFontList();
for (int i=0;i<names.length;i++)
{
fontlist.addItem(names[i]);
System.out.println(names[i]);
}
mnu=new JMenu("File");
mni=new JMenuItem("New");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content = frame.getContentPane();
pane = new JTextPane();
set = new SimpleAttributeSet();
set.addAttribute(StyleConstants.CharacterConstants.Bold,Boolean.TRUE);
// Initialize attributes before adding text
pane.setCharacterAttributes(set, true);
pane.setText("Three");
set = new SimpleAttributeSet();
StyleConstants.setItalic(set, true);
doc = pane.getStyledDocument();
try {
doc.insertString(doc.getLength(),"Blind", set);
} catch (BadLocationException e) {
System.err.println("Bad location");
return;
}
set = new SimpleAttributeSet();
StyleConstants.setFontSize(set, 48);
try {
doc.insertString(doc.getLength(), "Mice", set);
} catch (BadLocationException e) {
System.err.println("Bad location");
return;
}
JScrollPane scrollPane = new JScrollPane(pane);
content.add(scrollPane, BorderLayout.CENTER);
content.add(tb,BorderLayout.NORTH);
mnu.add(mni);
mbr.add(mnu);
frame.setJMenuBar(mbr);
frame.setSize(300, 200);
frame.show();
}
}
///////////////////////////
this code will only change the font of fixed text "Blink" and "Mice" but i am not able to change font of selected text.. as i have not functions of getSelectionStart and getSelectionEnd in JTextPane here ......
Can u guide me about it....
thanks...