Blurry Text after setting StyleConstants.setFirstLineIndent as negative in Swing

StackOverflow https://stackoverflow.com/questions/17700530

  •  03-06-2022
  •  | 
  •  

سؤال

Im a newbie to Swing applications. I need to add firstLineIndent to the text , but after setting this value as negative the paragraph's first line seems to be little blurry. I am unable to recognize the behavior of StyleConstants.setFirstLineIndent(attr,-50) method. How to rectify this error.

below is the link of the code which I am using as reference:

http://java-sl.com/tip_hanging_first_line.html

Thanks...

هل كانت مفيدة؟

المحلول

Not sure why that example extends a JEditorPane. An editor pane is for HTML. I prefer to use a JTextPane for styled text. Also don't know why a custom editor kit is used.

The following code works for me:

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

//public class HangingIndent extends JEditorPane {
public class HangingIndent extends JTextPane {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Negative (Hanging) first line indent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final HangingIndent app = new HangingIndent();
//        app.setEditorKit(new MyEditorKit());
        app.setText("The paragraph with long text is necessary to show how " +
                "hanging indent can be achieved. We should set not only the " +
                "first line indent but the same left indent.");
        StyledDocument doc=(StyledDocument)app.getDocument();
        SimpleAttributeSet attrs=new SimpleAttributeSet();
        StyleConstants.setFirstLineIndent(attrs, -50);
        StyleConstants.setLeftIndent(attrs, 50);

        doc.setParagraphAttributes(0,doc.getLength(),attrs, false);

        JScrollPane scroll = new JScrollPane(app);
        frame.getContentPane().add(scroll);

        frame.setSize(400, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public HangingIndent() {
        super();
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top