Daniel Guldberg Aaes
Daniel Guldberg Aaes

Reputation: 292

Make marked text bold with Java Swing and JEditorPane

I'm trying to make a simple text editor in java with swing. So far I've create this GUI: enter image description here

Bold, Italic and underline is not functional at the moment. What I wan't to do is to be able to highlight some text with the mouse and then be click on the Bold button for an example to make that text bold. But I'm not sure what to do to be able to accomplish that.

My code so far is:

import javax.swing.*;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainFrame  {


    private JFrame frame;
    //private JTextArea textArea;
    private  JEditorPane textArea;


    MainFrame() {
        frame = new JFrame("Simple Text Editor");

        MakeContentPane();
        MakeMenubar();

        frame.pack();
        frame.setVisible(true);
        frame.setSize(800,400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public void MakeMenubar(){
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        // create the File menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);

        JMenu editMenu = new JMenu("Edit");
        menubar.add(editMenu);

        JMenu aboutMenu = new JMenu("About"); // TODO: Create about popup windowd
        menubar.add(aboutMenu);

        JMenuItem quitItem = new JMenuItem("Quit");
        quitItem.addActionListener((e) -> System.exit(1));

        JMenuItem openItem = new JMenuItem("Open");
        openItem.addActionListener((e) -> Open());

        JMenuItem saveAsItem = new JMenuItem("Save");
        saveAsItem.addActionListener((e) -> SaveAs());

        fileMenu.add(openItem);
        fileMenu.add(saveAsItem);
        fileMenu.add(quitItem);

        Action action = new StyledEditorKit.BoldAction();
        action.putValue(Action.NAME, "Bold");
        editMenu.add(action);

    }

    public void MakeContentPane() {
        //textArea = new JTextArea();
        textArea = new JEditorPane("text/html", "");


        Container contentPane = frame.getContentPane();

        contentPane.setLayout(new BorderLayout());
        contentPane.add(textArea, BorderLayout.CENTER);

        JButton boldItem = new JButton("Bold"); //TODO Create action
        JButton italicItem = new JButton("Italic"); //TODO Create action
        JButton underLineItem = new JButton("Underline"); //TODO Create action

        JPanel buttons = new JPanel();
        buttons.setLayout(new GridLayout(1,3));
        buttons.add(boldItem);
        buttons.add(italicItem);
        buttons.add(underLineItem);

        contentPane.add(buttons, BorderLayout.NORTH);


    }


    public void SaveAs() {

        final JFileChooser SaveAs = new JFileChooser();
        SaveAs.setApproveButtonText("Save");
        int actionDialog = SaveAs.showOpenDialog(frame);
        if (actionDialog != JFileChooser.APPROVE_OPTION) {
            return;
        }

        File fileName = new File(SaveAs.getSelectedFile() + ".html");
        BufferedWriter outFile = null;
        try {
            outFile = new BufferedWriter(new FileWriter(fileName));

            textArea.write(outFile); 

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (outFile != null) {
                try {
                    outFile.close();
                } catch (IOException e) {

                }
            }
        }
    }

    public void Open(){
        // TODO: Create method to open files
        final JFileChooser SaveAs = new JFileChooser();
    }


    public static void main(String[] args) {
        MainFrame frame = new MainFrame();

    }



}

Upvotes: 0

Views: 1825

Answers (1)

camickr
camickr

Reputation: 324118

Don't use a JEditorPane, that is for HTML.

Instead use a JTextPane with simple text and then you can apply attributes to the text using the default Actions provided by the editor kit.

The StyledEditorKit comes with Bold, Italic and Underline Actions that you can use. For example:

JButton bold = new JButton( new StyledEditorKit.BOLD_ACTION );

Read the section from the Swing tutorial on Text Component Features for more information and a working example to get you started.

Upvotes: 2

Related Questions