YT GAMEWORKS
YT GAMEWORKS

Reputation: 93

Changing font in JEditorPane

Java was my first language, I mainly created CLI apps using it but dropped it 2 years ago for C# but picked it up as it seemed like the easiest to make GUI's in. I am working on a simple notepad ripoff called MakeText, main difference: Dark theme. I wanted to make a toolbar button to change the font... (using IntelliJ idea UI builder) here is the code for the editing page with the toolbar button:

package GUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class EditingPage
{
    JFrame EditingFrame;
    public JPanel EditPanel;
    public JEditorPane MainEditor;
    private JPanel ButtonPanel;
    private JButton fontSizeButton;

    public EditingPage() {
        fontSizeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FontDialouge dialog = new FontDialouge();
                dialog.pack();
                dialog.setVisible(true);
            }
        });
    }
}

and here's the code for the dialogue box for the font:

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FontDialouge extends JDialog
{
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField FontSizeTextBox;

    public FontDialouge()
    {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
    }

    private void onOK()
    {
        String inputFontSize = FontSizeTextBox.getText();

        //Converting string to int
        try
        {
            int OutputFontSize = Integer.parseInt(inputFontSize);
            System.out.println("Successfully set the font!");
            EditingPage editingPage = new EditingPage();
            editingPage.MainEditor.setFont(new Font("JetBrains Mono", Font.PLAIN, OutputFontSize));
        }
        catch (NumberFormatException nfe)
        {
            JOptionPane.showMessageDialog(null, "Not a valid size !");
            System.out.println("ERROR! not a valid size! ERROR: " + nfe);
        }
    }

}

But for some reason when I click the "OK" Button it dosent set the font and it is the same. I havent really worked with java in a long time so sorry if this is a really basic question! Also if you want all the src bc the amount here isn't enough then this has a GitHub page, here.

Upvotes: 0

Views: 273

Answers (1)

Abra
Abra

Reputation: 20913

In method onOK(), in class FontDialouge, you are creating a new instance of EditingPage. This is not the same instance that created the FontDialouge. You are changing the font of a different EditPanel.

Pass a reference of EditPanel to FontDialouge.

package GUI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FontDialouge extends JDialog
{
    private JPanel contentPane;
    private JButton buttonOK;
    private JTextField FontSizeTextBox;
    private JEditorPane editor; // ADDED THIS

    public FontDialouge(JEditorPane editor) // ADDED PARAMETER
    {
        this.editor = editor; // INITIALIZE CLASS MEMBER
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });
    }

    private void onOK()
    {
        String inputFontSize = FontSizeTextBox.getText();

        //Converting string to int
        try
        {
            int OutputFontSize = Integer.parseInt(inputFontSize);
            System.out.println("Successfully set the font!");
            editor.setFont(new Font("JetBrains Mono", Font.PLAIN, OutputFontSize));
        }
        catch (NumberFormatException nfe)
        {
            JOptionPane.showMessageDialog(null, "Not a valid size !");
            System.out.println("ERROR! not a valid size! ERROR: " + nfe);
        }
    }
}

And in method actionPerformed(), in class EditingPage, change the invocation of FontDialouge constructor.

public void actionPerformed(ActionEvent e) {
    FontDialouge dialog = new FontDialouge(MainEditor);
    dialog.pack();
    dialog.setVisible(true);
}

Upvotes: 0

Related Questions