MrSilent
MrSilent

Reputation: 574

Java Eclipse text editor

Good day. So I'm working on this project and I'm having one question. I have an encyclopedia and I want to add a text editor. I have a text and a scrollpanel and I want, when I select a sentence of my text and I press one button, to change the font, make the text bold, italic, underlined etc. How can I do that?

My code looks like this, the text.txt is a text file with "aaaa" in it.

package test;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JFrame;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class test extends JFrame {
    private static final long serialVersionUID = 1L;
    JFrame test = new JFrame("test");

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test();
                    frame.setVisible(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public test() {
        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setBounds(new Rectangle(0, 0, 0, 0));
        getContentPane().setLayout(null);

        test.setName("frame");
        test.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        test.setBounds(300,0,800,800);
        test.setResizable(false);
        test.getContentPane().setLayout(null);

        JScrollPane text = new JScrollPane();
        text.setBackground(Color.DARK_GRAY);
        text.setBounds(0, 0, 500, 400);
        getContentPane().add(text);

        JTextArea textarea = new JTextArea();
        setBackground(Color.WHITE);
        textarea.setEditable(false);
        textarea.setWrapStyleWord(true);
        textarea.setLineWrap(true);
        try{
            FileInputStream fstream = new FileInputStream("D:\\Facultate\\anul 2\\Java Workspace\\test\\src\\text.txt");
            DataInputStream in = new DataInputStream(fstream);
            Reader reader = new InputStreamReader(in);
            textarea.read(reader, fstream);
        }catch(Exception e){System.err.println("Error: " + e.getMessage());}
        text.setViewportView(textarea);
    }
}

Upvotes: 1

Views: 650

Answers (2)

Lorena Pita
Lorena Pita

Reputation: 1516

There is an amazing and Free Text Editor for Java. You can find it at Download a ready-to-use CKEditor package that best suits your needs. It's a product distributed by Amazon Web Services.

Upvotes: 0

SirRichie
SirRichie

Reputation: 1186

From the documentation: "A JTextArea is a multi-line area that displays plain text." So if you want different fonts, etc. in one area, you'll have to use another control, probably RTFEditorKit

Upvotes: 1

Related Questions