How to apply style changes (such as bold, Italic, color, adding bullets, etc) inside a JTable in Java?

72 views Asked by At

I implement a mini word processor using JTextPane in Java. I created a tool bar that makes me be able to bold, underline, and color the texts which the users can input.

Now I insert a JTable into this JTextPane and I hope to be able to do what I can do outside the table. Can I do that? Using cell renderer and editor to introduce the outside environment? Or use LayerUI to draw a table on the top of the JTextPane? I can draw, but how to edit in the image?

I am a new beginner in Java programming. If you can give me some hints, I would appreciate it very very much!

What I expect is that I can type in the JTable and apply bold, italic, underline and color changes, even add bullets, etc.

1

There are 1 answers

2
VGR On

A JTable is a component, not text. It cannot be styled using document styling.

You can, however, install an HTMLEditorKit on your JTextPane, which allows you to add HTML table to your document. An HTML table is comprised of text, so you can apply styles to it:

import java.io.StringReader;
import java.io.IOException;

import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.BadLocationException;

public class TableInTextPane {
    static void showTextPane() {
        JTextPane textPane = new JTextPane();
        textPane.setEditorKit(
            textPane.getEditorKitForContentType("text/html"));

        Style red = textPane.addStyle("red", null);
        StyleConstants.setForeground(red, Color.RED);

        Style green = textPane.addStyle("green", null);
        StyleConstants.setForeground(green, Color.GREEN);

        textPane.setLogicalStyle(red);

        try {
            textPane.getStyledDocument().insertString(0, "Red text", red);
        } catch (BadLocationException e) {
            throw new RuntimeException(e);
        }

        String table =
            "<table border=1>"
            + "<tr><th>Country      <th>Group<th>Position"
            + "<tr><td>Netherlands  <td>A<td>1"
            + "<tr><td>Senegal      <td>A<td>2"
            + "<tr><td>Ecuador      <td>A<td>3"
            + "<tr><td>Qatar        <td>A<td>4"
            + "<tr><td>England      <td>B<td>1"
            + "<tr><td>United States<td>B<td>2"
            + "<tr><td>Iran         <td>B<td>3"
            + "<tr><td>Wales        <td>B<td>4"
            + "</table>";

        int tableStartOffset = textPane.getDocument().getLength();
        try {
            textPane.getEditorKit().read(
                new StringReader(table),
                textPane.getDocument(),
                tableStartOffset);
        } catch (BadLocationException | IOException e) {
            throw new RuntimeException(e);
        }
        int tableEndOffset = textPane.getDocument().getLength();

        int caretPosition = textPane.getCaretPosition();

        textPane.select(tableStartOffset, tableEndOffset);
        textPane.setCharacterAttributes(green, false);

        textPane.select(caretPosition, caretPosition);

        showInWindow(textPane);
    }

    private static void showInWindow(JTextPane textPane) {
        JFrame frame = new JFrame("Table Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(new JScrollPane(textPane));
        frame.pack();

        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> showTextPane());
    }
}