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();
}
}