Wie erlaube ich einem Benutzer, seine Schriftart in einem JTextPane mithilfe einer JComboBox zu ändern?

Ich finde, dass es an hilfreichen Dokumentationen / Tutorials im Internet mangelt, wenn es um das Thema JTextPanes geht. Ich versuche, ein einfaches Textverarbeitungsprogramm zu erstellen, und möchte, dass es in der Lage ist, eine Schriftfamilie aus einer JComboBox auszuwählen, die sich anhand der Schriften auffüllt, die ein Benutzer auf seinem System installiert hat. Egal, was ich mit Experimenten versuche, ich kann nicht herausfinden, wie es funktioniert.

Was ich habe, ist eine Symbolleistenklasse, die aus einem JTextPane aufgebaut ist. Derzeit gibt es eine Reihe von Stilschaltflächen, mit denen Ausrichtung und Fettdruck, Kursivschrift und Unterstreichung festgelegt werden können.

Hier ist mein Code:

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

Um mein Problem zusammenzufassen: Ich habe keine Ahnung, wie ich den Listener richtig auf die ComboBox einstellen soll, damit a) er für die ausgewählte Schriftart empfindlich ist und b) irgendwie StyledEditorKit.FontFamilyAction verwendet, um das Leben zu vereinfachen?

Slash, wenn ich irgendetwas falsch angehe, würde ich gerne den richtigen Weg hören. Wie gesagt, meine Quellen im Internet sind zu diesem Thema nicht sehr klar.

Vielen Dank!

Antworten auf die Frage(2)

Ihre Antwort auf die Frage