• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to indent text in JTextPane ... ?

Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i m getting some problem using JTextPane when i set the left indent property using StyleConstants.setLeftIndent() function ( in initStylesForTextPane() fuction this is my function not java pl pay attention here as i printed the LeftIndent which is just what i set above but it is not indenting..? ) it is not doing indention but performing all othe task i included the code below

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.text.*;
public class UsingPane extends JFrame {
private JTextPane txtPane;
private JTextField txtField;
private JButton btnSend;
private Container thisFrame;
private JPanel pnlArea;
private JPanel pnlInput;

private JScrollPane jsp;
private Style caption,text;
private Document doc;

private String strUserID;
private Insets withPadding;
UsingPane (){
//super (c,false);
super ("new window");

strUserID = "me";
setSize (350,300);

private void createUI(){
BorderLayout layout = new BorderLayout();
txtPane = new JTextPane();
txtField = new JTextField();

btnSend =new JButton("Send");
pnlArea = new JPanel (new BorderLayout());
pnlInput = new JPanel (new BorderLayout());

jsp = new JScrollPane(txtPane);
jsp.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

thisFrame = getContentPane();
thisFrame.setLayout (layout);

pnlArea.add ( jsp , BorderLayout.CENTER);
pnlInput.add (txtField , BorderLayout.CENTER);
pnlInput.add (btnSend , BorderLayout.EAST);

thisFrame.add ( pnlArea , BorderLayout.CENTER);
thisFrame.add ( pnlInput, BorderLayout.SOUTH);

btnSend.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent evt) {

appendChatArea(getUserID() + " says" + "\n" , "caption");
appendChatArea(getChatFieldText() + "\n" , "text");

doc = txtPane.getDocument();
txtPane.setBorder(BorderFactory.createEmptyBorder(0,5,5 ,0));
withPadding = new Insets (1,1,1,1);

public String getUserID(){
return strUserID;
public void appendChatArea(String s ,String stname){
try {
doc.insertString(doc.getLength(), s, txtPane.getStyle(stname));
} catch (BadLocationException ble) {
System.err.println("Couldn't insert initial text.");
public void clearTextField(){

public String getChatFieldText (){
return txtField.getText();

public void enablePadding(){
// txtChatPane.setMargin(withPadding);
public void disablePadding() {

public static void main (String [ ] args) {
new UsingPane();
private void initStylesForTextPane() {

Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
Style regular = txtPane.addStyle("regular", def);
StyleConstants.setFontFamily(def, "SansSerif");

Style s = txtPane.addStyle ("caption", regular);
StyleConstants.setBold (s,true);
StyleConstants.setFontSize(s , 15);
StyleConstants.setLeftIndent (s,2.0f);
s = txtPane.addStyle ("text", regular);
StyleConstants.setFontSize(s , 13);
StyleConstants.setItalic (s,true);

StyleConstants.setLeftIndent (s,20.0f);
StyleConstants.setStrikeThrough (s, true);

System.out.println("style indent of text is " + StyleConstants.getLeftIndent(s)) ;
Ghazanfar Qureshi
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need the answer urgently pl...
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I see a couple of problems. The first one is that your Style is local to the initStylesForTextPane() method. Once that is done - poof - the style is gone. Make it an instance variable and it will last a bit longer.
Next, you define Style s and set some attributes for it, and then in the next breath you clobber those attributes with DIFFERENT attributes. Why??
Also, your question is VERY difficult to read. Please use proper punctuation. Most languages support periods.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!