JDK-4263904 : JTextPane: Paragraphs with Justified Attributes Appear Centered
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.2.0,1.2.2,5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,windows_98,windows_nt,windows_xp generic,windows_98,windows_nt,windows_xp
  • CPU: generic,x86
  • Submitted: 1999-08-19
  • Updated: 2005-08-31
  • Resolved: 2005-08-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6
5.0u6Fixed 6 b50Fixed
Related Reports
Duplicate :  
Description
Name: skT88420			Date: 08/19/99


Applying the SwingConstants.ALIGN_JUSTIFIED attribute to a JTextPane results in the text being centered and not justified as expected.

Here is some code that demonstrates the bug.  It produces a JFrame containing a JTextPane on the left hand side and a group of four buttons on the right hand side.  The buttons on the right hand side may be used to align the text.  Options available are left-justification, centered, right-justification and justified.  Pressing any of the first three buttons results in the text being aligned as expected.  However, pressing the justified button results in the text being centered.


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

public class TestAlignment extends JFrame
{
  private JTextPane myTextPane;
  private SimpleAttributeSet sas;
  private StyledDocument theDocument;

  public TestAlignment()
  {
    super();

    setSize( 500, 500);
    getContentPane().setLayout( new GridLayout( 1, 2));

    myTextPane = new JTextPane();
    myTextPane.setSize( 250, 250);
    getContentPane().add( myTextPane);
    myTextPane.setText( "A long line of text for testing alignment");

    sas = new SimpleAttributeSet();

    JButton leftAlignButton = new JButton( "left");
    JButton rightAlignButton = new JButton( "right");
    JButton centreButton = new JButton( "centered");
    JButton justifyButton = new JButton( "justified");

    JPanel buttonPanel = new JPanel();

    leftAlignButton.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e)
      {
        StyleConstants.setAlignment( sas, StyleConstants.ALIGN_LEFT);
        myTextPane.setParagraphAttributes( sas, false);

        myTextPane.repaint();
      }
    });

    rightAlignButton.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e)
      {
        StyleConstants.setAlignment( sas, StyleConstants.ALIGN_RIGHT);
        myTextPane.setParagraphAttributes( sas, false);

        myTextPane.repaint();
      }
    });

    centreButton.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e)
      {
        StyleConstants.setAlignment( sas, StyleConstants.ALIGN_CENTER);
        myTextPane.setParagraphAttributes( sas, false);

        myTextPane.repaint();      }
    });

    justifyButton.addActionListener( new ActionListener() {
      public void actionPerformed( ActionEvent e)
      {
        StyleConstants.setAlignment( sas, StyleConstants.ALIGN_JUSTIFIED);
        myTextPane.setParagraphAttributes( sas, false);

        myTextPane.repaint();
      }
    });

    buttonPanel.add( leftAlignButton);
    buttonPanel.add( centreButton);
    buttonPanel.add( rightAlignButton);
    buttonPanel.add( justifyButton);

    getContentPane().add( buttonPanel);
  }

  public static void main( String args[])
  {
    TestAlignment myFrame = new TestAlignment();
    myFrame.show();
  }
}


java -version
java version "1.2"
Classic VM (build JDK-1.2-V, native threads)

java -fullversion
JAVA.EXE full version "JDK-1.2-V"
(Review ID: 94114) 
======================================================================

Name: krT82822			Date: 09/14/99


I have been trying to build a text-processor using the HTMLEditorKit, the AlignmentAction Left, Right and Centered works fine, but the AlignmentAction Justified it doesn't work
the result of the action is a text Centered, instead of a text Justified.  

public class AlignJustifiedAction extends StyledEditorKit.AlignmentAction
	{
AlignJustifiedAction()
		{
		 super("ALIGN_JUSTIFIED",StyleConstants.ALIGN_JUSTIFIED);
		 }		
}....
(Review ID: 95233)
======================================================================


###@###.### 2003-12-02

Same problem has been reported by a CAP member:

Test case:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class MyJTextPane extends JFrame {
	
	SimpleAttributeSet sas;
	JTextPane tp;
	
	public MyJTextPane() {
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setTitle("Double click switches left and justified align.");

		String s = "When Mr. Bilbo Baggins of Bag End announced" +
				"that he would shortly be celebrating his eleventy-first" +
				"birthday with a party of special magnificence, there" +
				"was much talk an excitement in Hobbiton.";

		tp = new JTextPane();
		sas = new SimpleAttributeSet();
		StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
		StyleConstants.setBold(sas, true);		
		tp.setText(s);
		tp.setParagraphAttributes(sas, true);
		tp.setEnabled(false);
		
		tp.addMouseListener(new MouseAdapter() {
			
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					if(StyleConstants.getAlignment(sas)
							==StyleConstants.ALIGN_JUSTIFIED) {
						StyleConstants.setAlignment(sas,
											StyleConstants.ALIGN_LEFT);
						tp.setParagraphAttributes(sas, true);
					}
					else {
						StyleConstants.setAlignment(sas,
											StyleConstants.ALIGN_JUSTIFIED);
						tp.setParagraphAttributes(sas, true);
					}
				}
			}
		});
		
		this.getContentPane().add(new JScrollPane(tp));
		this.setSize(400, 200);
		this.show();
	}

	public static void main(String[] args) {
		MyJTextPane pane = new MyJTextPane();
	}
}

Comments
EVALUATION This fix goes to mustang and will be backported to 1.5 update release http://sa.sfbay.sun.com/projects/swing_data/mustang/4263904.1
12-08-2005

EVALUATION At this time we are going to support justification only for non i18n documents. We justify row by extending spaces by the same amount. "justifiable" row starts after the rightmost TAB. leading and trailing spaces are non extendable. (This how justification works in MS Word) P.S. While working on this I found a regression in FlowView introduced in mustang by the fix for 4855207: [Fix for 4250864: Low performance in JTextPane when paragraph has a lot of text] In some cases Views in paragraph left as children of LogicalView. They shoud be children of a Row instead.
28-07-2005

EVALUATION There is no code to display justified yet. timothy.prinzing@eng 1999-08-23 we are targeting to fix this bug for mustang release <note> It is unlikely the fix will be back-ported to 1.5 update release because it might be impossible to address this problem without introducing new public API (We can not introduce new public API in update releases) </note> ###@###.### 2005-03-23 22:27:49 GMT ###@###.### 2005-03-23 22:42:23 GMT
23-03-2005