Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4263904
Votes 303
Synopsis JTextPane: Paragraphs with Justified Attributes Appear Centered
Category java:classes_swing
Reported Against 1.2 , 1.2.2 , tiger-beta
Release Fixed mustang(b50), 5.0u6(b03) (Bug ID:2128705)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 4220235
Submit Date 19-AUG-1999
Description


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) 
======================================================================




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)
======================================================================


  xxxxx@xxxxx   2003-12- customer 

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();
	}
}
Posted Date : 2005-07-28 15:11:37.0
Work Around
N/A
Evaluation
There is no code to display justified yet.
  xxxxx@xxxxx   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>
  xxxxx@xxxxx   2005-03-23 22:27:49 GMT
  xxxxx@xxxxx   2005-03-23 22:42:23 GMT
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.
Posted Date : 2005-08-26 16:12:55.0

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
Posted Date : 2005-08-12 19:06:06.0
Comments
  
  Include a link with my name & email   

Submitted On 25-FEB-2000
hemnanid
I am facing the same problem and am in an urgency to solve it.  Gotta meet project deadlines you know.  So 
if this bug is fixed please inform me by email at danny@cyberoffice.com

Thanks


Submitted On 17-JAN-2001
prunarg
I've almost finish the development, and I've found this 
bug. I can't go on, 'cause in I'm developing a legal soft, 
and law demands text justified. Let me know when you fix it 
(soon, please!!!) to pruna@sinectis.com.ar or let me know 
if I can help with the development.


Submitted On 31-JAN-2001
rixal
Would you please let me know when you are going to 
implement this feature? I'm out of schedule and I need to 
give at least this info to my customer.


Submitted On 15-FEB-2001
gfaron
Possible workaround (but I could just be talking out of my @$$):
I'm not sure if this could be implemented into an actual JTextPane because I don't believe the access is 
granted at this level, but you might if you subclassed it.  My thought for an immediate workaround (for 
those that need this ASAP and have posted such needs above) is to draw the text using drawString().  This 
would of course prevent the text from being editable, but you could make your justification.

Justifying text should be a simple matter of finding the total stringWidth() of all words in a line (tokenize 
around the space character) and then subtracting that total pixel width from your available width.  The 
number you have left is what is devoted to spacing.  Divide that number by (wordCountPerLine - 1) and 
you'll have your offset to draw the individual word Strings.

  As I've said, I have not looked into the JTextPane class, but you may be able to implement this algorithm 
into a JTextPane-subclass to provide justified text in an editable field.


Submitted On 25-FEB-2001
lbarowski
Forget the justification, the real bug is that the text is centered.
Otherwise, html meant to be justified would look acceptable. That
much should be easy to fix, and can be worked-around in various
ways.


Submitted On 29-AUG-2001
ritvi
we also face the same bug so kidly give the solution if any
and mail me if solved


Submitted On 23-OCT-2001
unix2
I tried the Word Processor from ThinkFree Office before...
and they have managed to "Justified" the text... they 
should be using pure Java Swing package also... my question 
is : how they managed to fix the bug before the 
true "originator" fixed it... ?? mm.. hope to get the 
solution soon. all the best.

Thanks.


Submitted On 06-NOV-2001
robinpaul78
Same Problem here
My test editor is complete , execpt for the JustifiedAction

Please let me know when you resolve this bug


Submitted On 09-NOV-2001
anandshinde
I m working on frontpage like htmleditor, all is done 
except the justify part, pls let me know when this bug is 
fixed.


Submitted On 20-DEC-2001
zohas
Same problem. Is anyone going to fix it eventually? 


Submitted On 05-MAY-2002
rednael
I need correct justify alignment, too. But when I see that
this bug is about 3 YEARS old and has not been fixed yet I
don't have much hope that this will change soon. Damn, what
is the problem? Do I really have to implement it myself???


Submitted On 13-JUL-2002
SsantoshK
Hey I have problem related to highlighting words in 
JTextPane using HighLighter.addHighLight(....) .The 
highlighted portion is not at the appropriate palce and it 
hovers around the desired word. Plz give me mail if u have 
any solutions.


Submitted On 14-AUG-2002
Ivanhoe
The CENTER/JUSTIFIED error is in the getAlignment() method 
of javax.swing.text.ParagraphView, which returns: 0=left, 
1=right, 0.5=center, 0.5=justified. The return value is 
supposed to represent the proportion of blank space 
(totalwidth-linewidth) which should precede the beginning of 
the line. The whole concept of JUSTIFIED text just doesn't fit 
this idea at all... It's easy to change the method to return 
0=justified (so that JUSTIFIED and LEFT are identical), but as 
for fixing JUSTIFIED completely, I wonder if they might have 
to break a fair bit of running code to do that.


Submitted On 01-OCT-2002
cuzub
I need this feature as well. By the way, this is not the case in other countries but in France, justified is the default formatting...


Submitted On 22-OCT-2002
MartinBauerSindelfingen
I can't go on, because I'm developing software and law 
demands text justified. Let me know when you fix it: 
maba@softwareag.com.


Submitted On 25-OCT-2002
mdunstan
Not having justified text is a huge problem however, this
can't be properly implemented because of bug 4352983. As
soon as you have justified text and try to print, the text
is no longer justified. The right border is not aligned
properly. 

We have implemented justified text however we have to
disable it as well as right justified because of the
printing issue. Combining these two bug votes is 209. This
is obviously a huge problem for many users. 

Both these bugs have been around since 2000. I really want
to continue using Java however, this makes it extremely
difficult as our application is essentially crippled. Our
app targets the mortgage industry where companies are
printing up to 1 million docs per month. Please fix this in
1.4.2.


Submitted On 30-OCT-2002
colmoflaherty
Our end user just told us that under no circumstance is the 
app (a Client Reporting system with textual market 
commentaries in there)  going live without the text being 
justified (which sort of puts us in a bad place, given that the 
feature is not supported).  I'm off to check out some 
alternative fixes / workarounds.  If anything comes up, I'll 
post it.


Submitted On 05-NOV-2002
themax
Guys, if we want this fixed, we're going to have to vote for 
it.  Vote early, vote often, vote now!!


Submitted On 05-NOV-2002
itlanadmin
have the same problem - fix would be most appreciated.


Submitted On 31-MAR-2003
maurizio316
I'm developing a legal soft too, please mail me if solved


Submitted On 20-MAY-2003
the_hbf
I need to write justified in my JTextPane. Please inform me 
when it solved


Submitted On 14-JUL-2003
vasanthv
does lava provides some solution for text justification????


Submitted On 14-JUL-2003
vasanthv
I am facing the text justification problem and am in an 
urgency to solve it.  any alternate approach tried by anyone 
or if this bug is fixed please inform me by email at 
pgrao77@yahoo.com


Submitted On 14-JUL-2003
vasanthv
I am facing the text justification problem and am in an 
urgency to solve it.  any alternate approach tried by anyone 
or if this bug is fixed please inform me by email at 
pgrao77@yahoo.com


Submitted On 27-AUG-2003
victorMPlascencia
What happen Sun???, itīs a problem since Aug 19, 1999, 
threre are 4 years!!!!!, its the solutions of a big 
company!!!!!!!!, too Bad!!!!!


Submitted On 01-SEP-2003
tommy_K
As most posts stating an urgent need of the JUSTIFIED 
alignment in JEditorPane were in year 2000/2001 I'd like to 
state "still some dev. around needing this not less urgently"


Submitted On 01-SEP-2003
tommy_K
ah ya, and please give me 10 more radio buttons to vode for 
this!!


Submitted On 15-SEP-2003
pgRao
Why this bug fixing takes 2-3 years? Will it be solved in near 
future?


Submitted On 16-OCT-2003
jc_lim3
I was helping my friend build an text editor when we suddenly 
run across this error. can java device an alternative solution 
to remedy this problem?


Submitted On 28-NOV-2003
St.efan
Justified text align is an important feature of many commercial 
applications. There are over 200 votes. Why does it take so 
long to fix this bug?


Submitted On 03-DEC-2003
anWalser
really bad thing


Submitted On 04-DEC-2003
Torsten_R
This is a known bug since 1999, there are more than 200 
votes and these comments to these bug from 2000 until 
today so a lot of Java users need the fix for this bug.
I think these are enough reasons to give it a high priority but 
Sun seems to ignore this bug until today because currently 
version 1.4.2_x is available and it is not fixed and it seems 
that it will not be fixed in the next versions.
Does Sun consider this bug parade when it decide which 
features will be in a new version? If yes, then it should 
consider these bugs with a lot of votes and user comments 
since a long time!!


Submitted On 07-DEC-2003
mprudhom
There's a workaround for this at:

http://forum.java.sun.com/thread.jsp?
forum=57&thread=289879

It worked great for me.


Submitted On 25-DEC-2003
TimirKumarPatel
I am facing the same problem and am in an urgency to 
solve it.


Submitted On 23-JUL-2004
j2591128
when will sun release the fix for this Justified alignment problem ..... Dear SUN, please give a solution for this ... it is being pending from 1999 which is a very very long delay ..... PROJECTS ARE BEING SCRAPED BCOZ OF THIS PROBLEM


Submitted On 21-FEB-2005
TimirKumarPatel
I don't understand ...
Why This is Still Pending..??
This is most  important requirement for software appilcation development.
Why Sun Microsystem is not taking it seriosuly...

My project is Crashing Only due to This BUG.
PLEASE DO SOME THING FAST-
THANKS.


Submitted On 21-FEB-2005
TimirKumarPatel
PLEASE ..ATLEAST LET ME KNOW...
WHEN THIS WILL BE SOLVED OUT
PLEASE...

THX.


Submitted On 24-MAR-2005
gfaron
My personal opinion: It won't be.  Plan a workaround.


Submitted On 01-JUL-2005
mauronr
Still happens in 1.5.0_03 for Windows...
Almost 6-years-old bug...  I sold the idea of using java instead of M$ platform and now... What a great support from Sun...



PLEASE NOTE: JDK6 is formerly known as Project Mustang