CHANGE  FONT WITH JTEXTPANE

I am using JTextPane and want the user to be able to highlight text and change font styles. How do I do this? I tried setFont but it does not work. Thanks.. Sarah

Hello,
did you try something like this :
javax.swing.text.MutableAttributeSet attr = new javax.swing.text.SimpleAttributeSet();
attr.addAttribute(javax.swing.text.StyleConstants.FontFamily, fontName);
yourJTextPane.getStyledDocument().setCharacterAttributes(yourJTextPane.getSelectionStart(), yourJTextPane.getSelectionEnd() - yourJTextPane.getSelectionStart(), attr, false);

Similar Messages

  • Changing fonts in jtextpane

    Hi, I'm new to playing around with JTextPane's and I need to create a basic text editor. The problem I'm having is that when I select a new font and use the backspace, it generates many textUI exceptions (on my pc). When I run the same code on mac os x version 10.1.5, I don't get any of these exceptions. Maybe the problem is that my pc is using SDK 1.2.1, and my mac is running the java it was packaged with(which I believe is 1.3.1, but I'm not sure.) How do you find out what the java SDK version is on mac os x 10.1.5 anyway? I'm new to OS X as well. :-/
    please help me figure out what's wrong here, would updating my sdk on my pc fix this? Below is a very basic code sample that will illustrate my problem:
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.text.*; //for JTextPane
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import javax.swing.text.rtf.*;
    public class basicMemo extends JFrame{
    Color bgColor=Color.white;
    String fontName="Monospaced";
    int fontSize=12;
    JTextPane textPane;
    JComboBox fontBox;
    JComboBox sizeBox;
    JPanel northPanel;
    File fName=null;
    DefaultStyledDocument doc;
    RTFEditorKit rtfKit;
    StyleContext context;
    Container c;
    JFileChooser chooser;
    public basicMemo(){
    c=getContentPane();
    context=new StyleContext();
    doc=new DefaultStyledDocument(context);
    textPane=new JTextPane();
    rtfKit=new RTFEditorKit();
    textPane.setEditorKit(rtfKit);
    textPane.setDocument(doc);
    northPanel=new JPanel();
    chooser=new JFileChooser();
    chooser.setCurrentDirectory(new File("."));
    initFonts();
    initTop();
    c.add(northPanel,BorderLayout.NORTH);
    c.add(textPane,BorderLayout.CENTER);
    setSize(300,300);
    show();
    public void initFonts(){
    String fontNames[]={"Monospaced", "TimesRoman", "Courier",
    "Arial"};
    fontBox=new JComboBox(fontNames);
    fontBox.setEditable(true);
    fontBox.setSelectedItem(fontName);
    fontBox.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    fontName=fontBox.getSelectedItem().toString();
    MutableAttributeSet attr=new SimpleAttributeSet();
    StyleConstants.setFontFamily(attr,fontName);
    textPane.setCharacterAttributes(attr,false);
    String fontSizes[]={ "10","12","14","16","20","24","36","48","72"};
    sizeBox=new JComboBox(fontSizes);
    sizeBox.setEditable(true);
    sizeBox.setSelectedItem(fontSize+"");
    sizeBox.addActionListener(
    new ActionListener(){
    public void actionPerformed(ActionEvent e){
    int size=0;
    try{
    size=Integer.parseInt(sizeBox.
    getSelectedItem().toString());
    catch(NumberFormatException ex) {
    return;}
    fontSize=size;
    MutableAttributeSet attr=new SimpleAttributeSet();
    StyleConstants.setFontSize(attr,size);
    textPane.setCharacterAttributes(attr,false);
    //top panel
    public void initTop(){
    northPanel.setLayout(new FlowLayout());
    northPanel.add(fontBox);
    northPanel.add(sizeBox);
    public static void main(String args[]){
    new basicMemo();
    }

    I checked my java version on my mac, and its 1.3.1.
    So I thought that I would update my pc's java to 1.3.1.
    After doing that, and verifying that it was updated,
    I still get the s ame exceptions when I use backspace
    on a new font!!! I just don't get it.
    Maybe it doesn't have to do with the java version, maybe its a computer
    setting or something. I'd switch to the mac and program
    it on that, however that seems ridiculous that it
    would refuse to work on my pc. Any suggestions?
    im running win98 SE on the pc, if that helps anyone.

  • Can I change Fonts with Dragon Dictate

    Can I Change Fonts, Call out e-mail and search Commands with Dragon Dictate ?
    Also Can I use it without a Head Phone because my Speakers etc. plug in that portion
    behind the screen.

    Speech recognition expert Dan Newman of Sayican.com has a training video for Dragon Dictate. There is a free lesson that you can download.

  • Change font with a bottom.

    Hi,
    I couldn't find an answer for this but: Is there a way to chnage the font in the front panel by pressing a button?
    As you can see, I am building a farily simple starwars-theme-application, what I want to to is to use that funky starwar font ( like the one os top of the siganl inputs), but when a button is press the font goes back to a readable font (ie; arial)
    Solved!
    Go to Solution.

    You can create a property node for each of the controls you want to change.  Write the font name to the property Label.FontName.

  • Changing Fonts with a Pre-made Template

    I am trying to change the font of the words in the premade template titled "Float." Is this even a possibility? I am having a hard time figuring it out.
    Thanks for any help.

    it's all grouped according to the dropzone...hit f5 and in your layers tab, right before the name of the group is a little arrow, click it to reveal 3 other groups, repeat the process till you see the text layer.... New to motion? Read the manual and do the tutorials.

  • Cannot quick change font with arrow key / mac

    I have been using illustrator for years on windows and just recently switched to mac.
    On the windows version of Illustrator CS5 you can preview different fonts on the desired text by just placing the cursor on the font selection bar and then simply press the up/down arrows keys. On mac it does not work that way.
    Does anybody know how i can fix this?

    I'm having the same issue on:
    PC
    Windows 7
    Adobe Illustrator CS5
    Also, at certain times I click in the font selection bar and type in the name of the font I want... or part of it and it shows up and when I hit the enter key it does not change the font but rather switches the selected font back to what it was originally on. Effectively doing nothing.

  • Motion project to Compressor - changes font with render

    I have a project that is a simple opening title using Frutiger Black Condensed as the font (isn't everyone using Frutiger today?)
    The font is not in my user folder. It's located in the main Library Font folder. I had this issue before and it was because the font was in the User folder. You guys helped me with that one!
    The font has been validated.
    There are no duplicates.
    Motion sees the font and lets me set text to the font.
    I export it to Compressor and the Preview in Compressor shows the correct font.
    I render it using the Animation codec and the resulting QT movie has a different font.
    Rendering directly out of Motion works fine, of course, but I want to use Compressor because I want to set up several to render and walk away.
    Thanks for any help or tips.
    Scot
    Message was edited by: Scot Walker

    how very strange I had exactly the same issue today. The .mp4 export via compressor from motion had a substituted font. I didnt have too much time to explore the situation.
    the work around for me was to bring the motion project into fcp and then export it via compressor from there. Why this should be different I have no idea, but it worked.
    thanks
    adam

  • [SOLVED]Change font issue

    Hi, im new to Arch , im happy with it so far! But I want to get my favorite font "terminus" to work. I've read the wiki and i've also checked the FAQ for urxvt how to change font with no success. Im using latest Arch and I use urxvt as my terminal and awesome as wm, and i want the font to work on those both and in luakit if its possible. I've setup my xinitrc to make terminus work i've also resintalled it ones cuz I thought the font was broken or something. I used "URxvt.font:terminus" in my $HOME/.Xdefaults with no success. Ive also tried to add it to my rc.conf but it didnt make the font work in X(i havent checked if it appears on the commandline with no X up and running).
    I hope i could get some help
    Last edited by GNUkid (2012-03-11 18:36:38)

    karol wrote:
    urxvt*font: -*-terminus-medium-r-*-*-12-*-*-*-*-*-*-*
    works for me.
    Post your .Xdefaults.
    Didnt work Heres my .Xdefualts:
    urxvt*font: -*-terminus-medium-r-*-*-12-*-*-*-*-*-*-*
    URxvt.foreground: #1793D1
    URxvt.background: #555753
    URxvt.scrollBar: off
    magicalChicken wrote:
    .Xdefaults is deprecated, use .Xresources instead. Try in .Xresources adding
    urxvt*font: <name of font>
    Then to your .xinitrc add
    xrdb --merge ~/.Xresources
    I use this for setting the inconsolata font as default in urxvt and it works just fine.
    The .Xresources didnt work for me for some reason so thats why im using Xdefaults.

  • Easiest way to change font size/color with CSS?

    I've never used CSS before; I need to know how to change the font size and color using CSS, since it seems to be the only way to do it now. Sometimes I just need to change the font just for a few words, or a line here and there. Not necessarily page-wide, you understand. Is there a step-by-step process showing the simplest way to accomplish this? I'm not a web developer or professional designer, nor do I want to be. I just use DW to build simple web sites for personal use. I don't build using HTML - I solely use DM's GUI functions. And I'm also trying to teach my nine year-old how to start out page-building. I certainly hope that something as simple as changing font size doesn't require one to buy a book to learn HTML, which seems to be what a lot of people are telling me. Thanks for any assistance.

    Easiest is not always the best way.  You must learn the fundamentals. 
    CSS
    /**this styles all paragraphs in your site**/
    p {
    font-size: 18px;
    color: #000;
    /**This is a class.  Classes are re-usable**/
    .red {
    color:red;
    font-weight:bold;
    HTML:
    <p>This is a normal paragraph</p>
    <p class="red">This is a red paragraph</p>
    <p>This is a normal paragraph with <span class="red">some red text here</span> but not here.</p>
    If you're going to teach your 9yr old how to build web pages, start with the links below.
    There's no point in teaching your child bad habits that will have to be unlearned later.
    http://www.html.net/
    http://w3schools.com/
    http://www.csstutorial.net/
    http://phrogz.net/css/HowToDevelopWithCSS.html
    Nancy O.

  • I'm using firefox 6.0.2 It's making problem with Bangla font ONLY on FACEBOOK others bangli website FORNT SIZE are OK. On the FACEBOOK it shows Bangla font in a very / Too small! I change font SIZE in firefox setting but i can't solve. Pls HELP me

    I'm using firefox 6.0.2 It's making problem with Bangla font ONLY on FACEBOOK others bangli website FORNT SIZE are OK. On the FACEBOOK it shows Bangla font in a very / Too small! I change font SIZE in firefox setting but i can't solve. Pls HELP me ..

    Reset the page zoom on pages that cause problems: <b>View > Zoom > Reset</b> (Ctrl+0 (zero); Cmd+0 on Mac)
    *http://kb.mozillazine.org/Zoom_text_of_web_pages
    You can use an extension to set a default font size and page zoom on web pages.
    *Default FullZoom Level: https://addons.mozilla.org/firefox/addon/default-fullzoom-level/
    *NoSquint: https://addons.mozilla.org/firefox/addon/nosquint/

  • Office365 macbook air while copy paste and change font to tahoma forarded message remain with different fonts and size not accepting change

    office365 macbook air while copy paste and change font to tahoma forarded message remain with different fonts and size not accepting change
    mac yosemite
    office365

    Also here's the log before it happened. As you can see there is no activity from 9:48 to 10:00 am when the sound occurred.
    23/02/2014 9:48:12.792 am ntpd[52]: FREQ state ignoring -0.145411 s
    23/02/2014 9:48:15.258 am WindowServer[96]: _CGXHWCaptureWindowList: No capable active display found.
    23/02/2014 9:48:20.000 am kernel[0]: AppleCamIn::systemWakeCall - messageType = 0xE0000280
    23/02/2014 9:48:20.000 am kernel[0]: ARPT: 61.319378: AirPort_Brcm43xx::powerChange: System Sleep
    23/02/2014 9:48:20.000 am kernel[0]: ARPT: 61.319390: wl0: powerChange: *** BONJOUR/MDNS OFFLOADS ARE NOT RUNNING.
    23/02/2014 9:48:20.000 am kernel[0]: AppleCamIn::systemWakeCall - messageType = 0xE0000340
    23/02/2014 10:00:04.000 am bootlog[0]: BOOT_TIME 1393167604 0
    23/02/2014 10:00:06.000 am syslogd[17]: Configuration Notice:
    ASL Module "com.apple.appstore" claims selected messages.
    Those messages may not appear in standard system log files or in the ASL database.
    23/02/2014 10:00:06.000 am syslogd[17]: Configuration Notice:
    ASL Module "com.apple.authd" sharing output destination "/var/log/system.log" with ASL Module "com.apple.asl".
    Output parameters from ASL Module "com.apple.asl" override any specified in ASL Module "com.apple.authd".

  • Changing fonts and font sizes with hotkeys

    In MS Word you can select text and hit Ctrl + Shift + "F" to get to the drop down menu to change the font and just hit tap to change the size. Is that possible in Pages?

    Use cmd shift + or - to change font size.
    It may be cmd - for the reduce size, I may have regularised the keyboard shortcut on my system.
    You will need to use the toolbar or the type palette to change the font itself.
    Peter
    btw Once you are in the various pulldown menus, including the toolbar Type menus, you can use your up or down keys plus enter or return to change selections.

  • How do I change font in Firefox example when I go to Craigs list, everything is in italics....not so with Internet Explorer....I like Mozilla best and want to correct this problem

    How do I change fonts in Mozilla"

    Hi,
    Please try this: '''Tools''' (or '''Alt''' + '''T''') > '''Options''' > '''Applications'''. Look for or start typing '''mailto''' in the search box and for the '''Action''' change it to the desired mail client (the application that opens up in Explorer) if the value is different.
    '''Options''' > '''Applications''': https://support.mozilla.com/en-US/kb/Options%20window%20-%20Applications%20panel
    Managing Actions: https://support.mozilla.com/en-US/kb/Managing%20file%20types

  • Change font in String ?

    Is it possible to change font of a String like U cant do it with html coding? If I have :
    String text ="this is a text";
    can I change the color and the size of the text and so on, instead of the default ones?

    You can set these attributes for the component which displays your string. For example, if you want to use a JLabel to display your String you can set the font, font style and colour of the JLabel.
    JLabel lab = new JLabel(String);
    lab.setFont(new Font("Arial", Font.BOLD, 16);
    lab.setForegound(Color.red);You can do the same for JTextAreas and JTextFields. However, all of the text in these components will take the same formatting. JTextPane and JEditorPane are more flexible in that you can display text in various colours and fonts etc.
    have a look at the Swing Tutorial. Follow the link to Text Components.

  • Save/open document with different font with RTFEditor!

    Hello everyone !
    I am creating a RTF Editor with JTextPane, DefaultStyledDocument and RTFEditor,It is right if input English character with different Font such as Arial,Basemic etc .but with Non-English ,It is question!!when I input chinese in font "����GB_2312",and save it ,then I open it again ,font is changed to "��"��but font size,color ,and style is correct!I doubt it is RTFEditor does't support chinese font ,is it?how can I do?
    thanks a lot!

    That's because nobody has posted an answer. You can find the discussion at how do I save a document with different page orientations to one pdf? I think this is pretty much what happens on Mac, but you can combine the separate PDFs to one.

Maybe you are looking for

  • Iphone 5 randomly shut off and won't come back on

    For the last few weeks my iphone 5 has been randomly shutting off. Usually when this happens I just hold down the power and home buttons and my phone reboots within a few seconds. However last night my phone shut off and will not turn back on no matt

  • No bootable device issue when installing Win 7 Pro 64 with OSX 10.8.3

    Trying to install Win 7 Pro 64 on brand new iMac running 10.8.3. Using external LaCie optical drive to read Win 7 OEM DVD installer. Using Bootcamps Assistant, I can create the Windows partition but then when the iMac restarts to insatll Windows, I g

  • RAW resolution

    We have PSE 5 w/current RAW Converter - WinXP Our Olympus software which accompanied the E500 camera opens RAW images at 3264x2448 - 314 dpi Resolution When opening the same image in PSE 5 RAW, then Editor, the image is 3264x2448  but  240 dpi Resolu

  • Bought an audiobook, but it is not showing in library or on iTouch

    purchased an audiobook on iTunes.  It never appeared in my library of books and did not show up on the iTouch i was syncing.  when i select the audiobook again in iTunes, it says i already purchased it and will be billed twice.  Not in Downloads eith

  • PXE Boot Problem

    Hello everyone! I've got a PXE boot server running and I can boot my EEE 701 from it without issue, it gets to the installer but then I get asked for the source of the packages. Now, I don't have an Internet connection to the boot server, so that's n