Como eu permito que um usuário mude sua fonte em um JTextPane usando um JComboBox?

Eu estou achando que a quantidade de documentação útil / tutoriais na internet está faltando quando se trata do tópico JTextPanes. Eu estou tentando fazer um processador de texto simples, e eu quero que ele seja capaz de selecionar uma família de fontes de um JComboBox que se preenche com base nas fontes que um usuário instalou em seu sistema. No entanto, não importa o que eu experimente, não consigo descobrir como fazer isso funcionar.

O que eu tenho é uma classe de barra de ferramentas que é construída a partir de um JTextPane. Atualmente, ele possui vários botões de estilo que funcionam para definir alinhamento e negrito, itálico e sublinhado.

Aqui está meu código:

<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
</code>

Então, para resumir o meu problema: Eu não tenho idéia de como definir corretamente o ouvinte para o ComboBox tal que a) é sensível à fonte individual selecionada eb) de alguma forma usa StyledEditorKit.FontFamilyAction para facilitar a vida?

Slash, se eu estou me aproximando de algo sobre isso do jeito errado, eu adoraria ouvir o caminho certo. Como eu disse, minhas fontes na internet não são muito claras sobre o assunto.

Muito obrigado!

questionAnswers(2)

yourAnswerToTheQuestion