Adding TabStops to the JTextPane

Introduction

This example shows how to create Tab Stops in a JTextPane. This is useful since the JTextPane doesn’t have this by default.

// Create a text pane
JTextPane textPane = new JTextPane();

// Create one of each type of tab stop
java.util.List list = new ArrayList();

// Create a left-aligned tab stop at 100 pixels from the left margin
float pos = 100;
int align = TabStop.ALIGN_LEFT;
int leader = TabStop.LEAD_NONE;
TabStop tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a right-aligned tab stop at 200 pixels
// from the left margin
pos = 200;
align = TabStop.ALIGN_RIGHT;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a center-aligned tab stop at 300 pixels
// from the left margin
pos = 300;
align = TabStop.ALIGN_CENTER;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a decimal-aligned tab stop at 400 pixels from the left margin
pos = 400;
align = TabStop.ALIGN_DECIMAL;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a tab set from the tab stops
TabStop[] tstops = (TabStop[])list.toArray(new TabStop[0]);
TabSet tabs = new TabSet(tstops);

// Add the tab set to the logical style;
// the logical style is inherited by all paragraphs
Style style = textPane.getLogicalStyle();
StyleConstants.setTabSet(style, tabs);
textPane.setLogicalStyle(style);

 

4 thoughts on “Adding TabStops to the JTextPane”

  1. Thank you for another fantastic post. Where else could anyone get that kind of information in such an ideal way of writing? I’ve a presentation next week, and I am on the look for such information.

  2. I was just looking for this information for some time. After 6 hours of continuous Googleing, at last I got it in your site. I wonder what is the lack of Google strategy that don’t rank this type of informative websites in top of the list. Normally the top web sites are full of garbage.

  3. Good day! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy reading your articles. Can you recommend any other blogs/websites/forums that cover the same subjects? Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.