• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the font of Selected Text in JTextPane

 
Syed Usman
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Shane Roylance
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I don't understand the dillema, but JTextPane is a decendent of JTextComponent. Therefore it does have getSelectionStart and getSelectionEnd methods. I have been messing around with JTextPanes for a little bit, and I still don't really understand them. But there is a good tutorial on Sun's site about them if you haven't already read it Sun Text Tutorial
Sorry I am not more help.
Shane
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Syed,
First of all use JComboBox inplace of AWT Choice. We have such a beatiful swing library (both functionality wise and architechture wise) why u guys r using AWT??
Secondly, i don;t see any code written for changing the attributes of the text pane. All the code is in init and hence not dynamic (i.e. it's not in any action).
But ur probs seems to be something different. U can't find getselectionStart() and getSelectionEnd() methods in JTextPane. Right??? The answer is that in JTextPane's javadoc just scroll a little bit and there u'll find javadoc summary of the methods of all the parent classes and interfaces. In JTextComponent u'll find those required methods
Lastly why r u setting the tool bar's layout to GridLayout. Do not set it. JToolBasr will automatically set to BoxLayout it's version depending upon thew orientation i.e. X_AXIS or PAGE_AXIS. If the tool bar is floatable and tjhe user drags it to somewhere else and the makes it verticle. And if he then arranges it such that the tool bar now occuopies the whole screen in ur case the buttons will grow with the width equal to screen width.
Hope this helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!