Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:22:40+00:00 2026-06-14T02:22:40+00:00

I am trying to make a small HTML-wysiwyg with a JTextPane but I can’t

  • 0

I am trying to make a small HTML-wysiwyg with a JTextPane but I can’t get the BackgroundAction to work. I am using setCharacterAttributes on the StyledDocument of the JTextPane but it seems problematic. The view is ok but the Document is not.

Here is a small demo code showing the problem. There are 2 JTextPane:

  1. I set the background color of my text in the first one
  2. I retrieve the text of the first JTextPane and set it on the second one

    –> They don’t show the same thing although they have the same text.

Is there a way to set the background color on the current selected text and have the JTextPane report an updated HTML-text?

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class TestDifferentStyles {

    private void initUI() {
        JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTextPane textPane = new JTextPane();
        final JTextPane textPane2 = new JTextPane();
        textPane2.setEditable(false);
        textPane.setContentType("text/html");
        textPane2.setContentType("text/html");
        textPane.setText("<html><head></head><body><p>Hello world</p></body></html>");
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setForeground(set, Color.GREEN);
        StyleConstants.setBackground(set, Color.BLACK);
        ((StyledDocument) textPane.getDocument()).setCharacterAttributes(0, textPane.getDocument().getLength(), set, false);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        panel.add(textPane, gbc);
        panel.add(textPane2, gbc);
        frame.add(panel);
        frame.setSize(500, 400);
        frame.setVisible(true);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                System.err.println(textPane.getText());
                textPane2.setText(textPane.getText());
            }
        });
    }

    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestDifferentStyles().initUI();
            }
        });
    }

}

The output result (the black border are around each JTextPane):
output result

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:22:41+00:00Added an answer on June 14, 2026 at 2:22 am

    Here is the code for an Action that can set the background color:

    public class BackgroundColorAction extends StyledEditorKit.StyledTextAction {
    
        private Color color;
    
        public BackgroundColorAction(Color color) {
            super(StyleConstants.Background.toString());
            this.color = color;
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            JEditorPane editor = getEditor(ae);
            if (editor == null) {
                return;
            }
            //Add span Tag
            String htmlStyle = "background-color:" + Util.getHTMLColor(color);
            SimpleAttributeSet attr = new SimpleAttributeSet();
            attr.addAttribute(HTML.Attribute.STYLE, htmlStyle);
            MutableAttributeSet outerAttr = new SimpleAttributeSet();
            outerAttr.addAttribute(HTML.Tag.SPAN, attr);
            //Next line is just an instruction to editor to change color
            StyleConstants.setBackground(outerAttr, this.color);
            setCharacterAttributes(editor, outerAttr, false);
        }
    }
    

    I had lot of trouble setting background color. But finally, I have managed to crack it.`
    Sorry I forgot to post the subroutine. Here you go:

    /**  
     * Convert a Java Color to equivalent HTML Color.
     *
     * @param color The Java Color
     * @return The String containing HTML Color.
     */
    public static String getHTMLColor(Color color) {
        if (color == null) {
            return "#000000";
        }
        return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make a small countdown timer in my app but it's not working.
I am trying to make a small html page including jquery . I added
I am trying to make a virtual die (for playing dice) using CSS/HTML and
I'm currently learning Python and I'm trying to make a small scraper but I'm
heeey!) I was trying to make a gallery but i get an error on
I have small problem. I'm trying to get text whitch is out of html
im trying to make a small name summary function depending on the size of
I'm trying to make a small radius with the AudioPlaybackAgent as Microsoft show in
I am trying to make a small application for my arm target. I have
I am trying to make a small application which has the capability of showing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.