2


I have requirement of Text wrapping + Text Centering + Nimbus Look and Feel + with a background color black.
I tried out with the following 2 components
1. JTextPane
-------supports: Text Wrapping + Text Centering using StyledDocument
-------issue : custom background color doesn't work Nimbus Look and Feel(be it disabled or enabled)
which is bug as show here http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=ab1938d61a7fd83ca2b54eb8df7?bug_id=6789980

my jdk version is : 1.6.0_33
Fixes tried :

UIManager.getLookAndFeelDefaults().put("TextPane.background", Color.RED);

but this also is unable to override the Nimbus default which is #d6d9df (214,217,223)

Can anyone help me with text Centering in the JTextArea? Thanks in advance

1. JTextArea
-------supports: Text Wrapping + background coloring with Nimbus LnF (only when its enabled)
-------issue : Text Centering is an issue in here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • http://stackoverflow.com/questions/3213045/centering-text-in-a-jtextarea-or-jtextpane-horizontal-text-alignment might help – Mogli Jun 18 '13 at 06:09
  • yes it works's but with JTextPane only and not with JTextArea. I need to solve my problem with JTextArea only (because the background color cannot be set in the JTextPane with Nimbus LnF). Didn't had any luck with that as of for now , :'( – Er Kaushik Chakraborty Jun 18 '13 at 08:56

3 Answers3

2

To center text you should use a JTextPane not a JTextArea. Centering text is a big issue.

It is better to solve the background problem with the JTextPane.

When using the Nimbus LAF it looks like you need to provide a custom Painter to just paint the background as a solid color. Check out this answer by @mKorbel. You would need to change the property tag. Also in the FillPainter I changed:

g.setColor(color);
g.setColor(object.getBackground());
Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks .... Working ,,,, This is what I used ... `UIManager.getLookAndFeelDefaults().put("TextPane[Enabled].backgroundPainter", new FillPainter());`

    `public class FillPainter implements Painter { @Override public void paint(Graphics2D g, JComponent object, int width, int height) { g.setColor(object.getBackground()); } }`
    – Er Kaushik Chakraborty Jun 19 '13 at 10:44
1

Thanks .... Working ,,,, This is what I used ...

UIManager.getLookAndFeelDefaults().put("TextPane[Enabled].backgroundPainter", new FillPainter());`


public class FillPainter implements Painter<JComponent> {
    @Override
    public void paint(Graphics2D g, JComponent object, int width,int height) {
        g.setColor(object.getBackground());
    }
}
0

I use this to center text in JTextArea

public static void centerText (JTextArea ta)
{
    BufferedImage fake1 = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D fake2 = fake1.createGraphics();
    FontMetrics fm = fake2.getFontMetrics(ta.getFont());

    int lines = ta.getLineCount();
    ArrayList<String> list = new ArrayList<>();
    try
    {
        for (int i = 0; i < lines; i++)
        {
            int start = ta.getLineStartOffset(i);
            int end = ta.getLineEndOffset(i);

            String line = ta.getText(start, end - start).replace("\n","");
            list.add (line.trim());
        }
    }
    catch (BadLocationException e)
    {
        System.out.println(e);
    }
    alignLines (list, fm, ta);
}

private static void alignLines (ArrayList<String> list, FontMetrics fm, JTextArea ta)
{
    String leading = "      ";
    int longest = -1;
    for (String s : list)
    {
        if (fm.stringWidth(s) > longest)
            longest = fm.stringWidth(s);
    }
    for (int n=0; n<list.size(); n++)
    {
        String s = list.get(n);
        if (fm.stringWidth(s) >= longest)
            continue;
        while (fm.stringWidth(s) < longest)
            s = ' '+s+' ';
        list.set(n, s);
    }
    StringBuilder sb = new StringBuilder();
    for (String s : list)
    {
        sb.append(leading).append(s).append('\n');
    }
    ta.setText (sb.toString());
}

call CenterText with your filled JTextArea as argument