• Post Reply Bookmark Topic Watch Topic
  • New Topic

Alignment in JTextArea/JTextPane

 
RAJEEV BAHL
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please give example to align text scifucally right align in JTextArea. If not possible in this then in JTextPane. Thanks in advance
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hope you can get some help from the tutorials at Sun,
incase you have not yet checked them.
regds.
- satya
 
RAJEEV BAHL
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As described I go through the example. I am able to set fot, size but unable to Align. Following is the code:

DefaultStyledDocument dSDoc = new DefaultStyledDocument();
String initString[] =
{ "\nINDIAN OIL CORPORATION LTD.\n" +
"GUWAHATI-SILIGURI PRODUCTS PIPELINE\n" +
"Sector-III Noonmati Guwahati-20\n" +
"=========================\n\n",
"Tank Out Turn Report\n",
"Date : " + sdf.format(progDate),
"Station : " + (String)stationJComboBox.getSelectedItem() + stationTypeString,
};
SimpleAttributeSet[] attrs = initAttributes(initString.length);
try{
for (int i = 0; i < initString.length; i ++) {
dSDoc.insertString(dSDoc.getLength(), initString[i] + newline,
attrs[i]);
}
}catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text.");
}
jTextPane = new JTextPane();
Dimension pSize = new Dimension(450, 550);
jTextPane.setPreferredSize(pSize);
JTextPane jTextPane2 = new JTextPane(dSDoc);
pSize = new Dimension(450, 200);
jTextPane2.setPreferredSize(pSize);
jTextPane.insertComponent(jTextPane2);
jTextPane.insertComponent(jTable);
jTextPane.insertComponent(jTextArea1);
jTextPane.setEditable(false);

private String newline = "\n";
protected SimpleAttributeSet[] initAttributes(int length) {
SimpleAttributeSet[] attrs = new SimpleAttributeSet[length];
attrs[0] = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs[0], "SansSerif");
StyleConstants.setFontSize(attrs[0], 10);
StyleConstants.setAlignment(attrs[0], StyleConstants.ALIGN_CENTER);
attrs[1] = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs[1], "SansSerif");
StyleConstants.setFontSize(attrs[1], 12);
StyleConstants.setBold(attrs[1], true);
StyleConstants.setUnderline(attrs[1], true);
StyleConstants.setAlignment(attrs[1], StyleConstants.ALIGN_CENTER);
attrs[2] = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs[2], "SansSerif");
StyleConstants.setFontSize(attrs[2], 12);
StyleConstants.setAlignment(attrs[2], StyleConstants.ALIGN_RIGHT);
attrs[3] = new SimpleAttributeSet();
StyleConstants.setFontFamily(attrs[3], "SansSerif");
StyleConstants.setFontSize(attrs[3], 12);
StyleConstants.setAlignment(attrs[3], StyleConstants.ALIGN_JUSTIFIED);
return attrs;
}
 
Jim Ronholm
Greenhorn
Posts: 18
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this is an old question (more than a year old) but I had a similar question and found that isn't very well answered (anywhere) - at least not in simple terms. I did find something that led me to the right answer in the JDC at Sun.
Summary:
  • use JTextPane
  • create attributes instance
  • set a paragraph alignment attribute using StyleConstants class
  • apply the attribute set to the paragraph style of the JTextPane


  • I hope that clarifies it - ask if it doesn't.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!