How do I allow a user to change his font in a JTextPane using a JComboBox?
Asked Answered
G

2

3

I'm finding the amount of helpful documentation/tutorials on the internet are lacking when it comes to the topic of JTextPanes. I'm trying to do a simple text processor, and I want it to be able to select a font family from a JComboBox that populates itself based on the fonts a user has installed on their system. However, no matter what I try with experimenting, I can't figure out how to make it work.

What I have is a toolbar class that is built off of a JTextPane. Currently, it has a bunch of style buttons that work to set alignment and bold, italics and underline.

Here's my code:

/**
* The StyleBar is used to customize styles in a Styled Document. It will take a
* JTextPane as an argument for its constructor and then all actions to be taken
* will affect the text in it.
*
* @author Andrew
*/
public class StyleBar extends JToolBar {

    private JLabel fontLbl;
    private JComboBox fontBox;

        // ...Irrelevant stuff to the problem at hand.

    /**
    * The initEvents method is used to initialize the necessary events for the
    * tool bar to actually do its job. It establishes the focus listener to the
    * buttons on the bar, and gives each one its individual functionality. It 
    * also establishes the Font Selection interface.
    */
    public void initEvents() {
        //For each item in the tool bar, add the focus listener as well as the
        //formatting listeners:
        boldFormat.addActionListener(new StyledEditorKit.BoldAction()); //boldFormat is
        boldFormat.addActionListener(resetFocus);                       //a JButton

        //Ditto for my italicsFormat and underlineFormat button(s) in the toolbar

        leftAlign.addActionListener(new StyledEditorKit.AlignmentAction(null,
                StyleConstants.ALIGN_LEFT));
        leftAlign.addActionListener(resetFocus);    //This listener just resets focus
                                                    //back onto the TextPane.

        //Ditto for my right and centerAlign buttons

        //Set up the Font list, and add a listener to the combo box
        buildFontMenu();
    }

    /**
    * The buildFontMenu detects all of the SYstem's available fonts and adds 
    * them to the Font Selection box.
    */
    public void buildFontMenu(){
        GraphicsEnvironment ge = 
                GraphicsEnvironment.getLocalGraphicsEnvironment();
        final String[] fontNames = ge.getAvailableFontFamilyNames();
        for (int i = 0; i < fontNames.length; i++){
            //What do I do here to take the entry at String[i] and make it so that when
            //the user selects it it sets the font Family in a similar way to that of
            //pressing the boldFormat button or the leftAlign button?
        }
    }

    //Everything else is irrelevant

So to sum up my problem: I have no idea how to properly set the listener to the ComboBox such that a) it's sensitive to the individual font selected and b) somehow uses StyledEditorKit.FontFamilyAction to make life easy?

Slash, if I'm approaching anything about this the wrong way, I'd love to hear the right way. As I said, my sources on the internet aren't very clear on the subject.

Thanks so much!

Gest answered 13/5, 2012 at 1:18 Comment(0)
R
3

StyledEditorTest puts the Actions in a JToolBar, but the idea is the same. See also Charles Bell's HTMLDocumentEditor, mentioned here. For example,

bar.add(new StyledEditorKit.FontFamilyAction("Serif", Font.SERIF));
bar.add(new StyledEditorKit.FontFamilyAction("SansSerif", Font.SANS_SERIF));

Addendum: As you are using JComboBox, you can forward the event to the corresponding StyledEditorKit Action, which operates on the current selection by default.

JComboBox combo = new JComboBox();
combo.addItem("Serif");
combo.addItem("Sans");
combo.addActionListener(new ActionListener() {

    Action serif = new StyledEditorKit.FontFamilyAction("Serif", Font.SERIF);
    Action sans = new StyledEditorKit.FontFamilyAction("Sans", Font.SANS_SERIF);

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Sans".equals(e.getActionCommand())) {
            sans.actionPerformed(e);
        } else {
            serif.actionPerformed(e);
        }
    }
});
Rodrique answered 13/5, 2012 at 1:25 Comment(0)
K
0

This may be a bit late, but I found myself stuck on the similar problem, and although the previous answer does help, here is a more complete answer for when you want to use all the fonts available on your computer..

Where "fonts" is a string array containing all the desired fonts to be used in your program

        final JComboBox jcb = new JComboBox(fonts);

    final Action [] actions = new Action[fonts.length];

    for (int i = 0; i < actions.length; i++)
    {
        actions[i] = new StyledEditorKit.FontFamilyAction(fonts[i], fonts[i]);
    }

    jcb.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event) 
            {
                for (int i = 0; i < actions.length; i++)
                {
                    if (fonts[i].equals((String)jcb.getSelectedItem()))
                    {
                        actions[i].actionPerformed(event);
                        break;
                    }
                }
            }
        });
Koren answered 22/5, 2013 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.