2

Hi I am trying to find out whether it is possible to apply background color only to a portion of JTextpane? For example if I have a JTextpane of size 300 X 400 then can I apply some color to a portion 300 X 200 ?

I am not posting any code as I dont have any.

Eg: enter image description here

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
user001
  • 991
  • 6
  • 16
  • 34
  • 1
    Are you trying to [highlight text](http://stackoverflow.com/q/5674128/230513)? – trashgod Mar 31 '13 at 08:48
  • This is not a simple task. The problem is, text is painted as part of the call to paintComponent, which also paints the background. What you would need to do is set the text pane as transparent (setOpaque(false)), override paintComponent, paint the background, paint your color and call super.paintComoponent last – MadProgrammer Mar 31 '13 at 08:53
  • Thanks will try that. I have posted the sample screenshot above – user001 Mar 31 '13 at 08:57
  • 2
    Can you use [HTML formatting](http://stackoverflow.com/q/9615991/230513)? – trashgod Mar 31 '13 at 09:05
  • 1
    @user1815809, you've post 13 questions and not accepted a single answer in any of those questions. I think I'll skip answering this one. – camickr Mar 31 '13 at 17:52
  • Hey camickr sorry If I haven't accepted any.. I am looking for some better solution. For now I am trying the one suggested by MadProgrammer. – user001 Apr 02 '13 at 07:29

1 Answers1

2

You simply need to add StyleConstants.Background, as a parameter to addAttribute() of the StyleContext, which will return one AttributeSet object. Hope this is what you looking for :

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

import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;

public class TextPaneTest extends JFrame
{
    private JPanel topPanel;
    private JTextPane tPane;

    public TextPaneTest()
    {
        topPanel = new JPanel();        

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);            

        EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));

        tPane = new JTextPane();                
        tPane.setBorder(eb);
        //tPane.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        tPane.setMargin(new Insets(5, 5, 5, 5));

        topPanel.add(tPane);

        appendToPane(tPane, "My Name is Too Good.\n", Color.RED, Color.WHITE);
        appendToPane(tPane, "I wish I could be ONE of THE BEST on ", Color.WHITE, Color.BLUE);
        appendToPane(tPane, "Stack", Color.DARK_GRAY, Color.BLACK);
        appendToPane(tPane, "Over", Color.MAGENTA, Color.BLUE);
        appendToPane(tPane, "flow", Color.ORANGE, Color.YELLOW);

        getContentPane().add(topPanel);

        pack();
        setVisible(true);   
    }

    private void appendToPane(JTextPane tp, String msg, Color f, Color b)
    {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, f);
        aset = sc.addAttribute(aset, StyleConstants.Background, b);

        aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
        aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);

        int len = tp.getDocument().getLength();
        tp.setCaretPosition(len);
        tp.setCharacterAttributes(aset, false);
        tp.replaceSelection(msg);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextPaneTest();
                }
            });
    }
}

Here is the output :

TEXTPANE

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • Hi Gagandeep, Thanks for the reply. But I am not looking for this. I believe the above example is only highlighting text but not coloring the entire panel from left end to right end. I am looking for something like the screenshot given in my question – user001 Apr 02 '13 at 07:19