Tag Archives: JTextPane

HiLighting words in a JTextPane

Introduction

This example shows how text can be HiLighted in the text of a JTextPane.

JTextPane textPane = new JTextPane();
StyledDocument doc = textPane.getStyledDocument();

// Makes text red
Style style = textPane.addStyle("Red", null);
StyleConstants.setForeground(style, Color.red);

// Inherits from "Red"; makes text red and underlined
style = textPane.addStyle("Red Underline", style);
StyleConstants.setUnderline(style, true);

// Makes text 24pts
style = textPane.addStyle("24pts", null);
StyleConstants.setFontSize(style, 24);

// Makes text 12pts
style = textPane.addStyle("12pts", null);
StyleConstants.setFontSize(style, 12);

// Makes text italicized
style = textPane.addStyle("Italic", null);
StyleConstants.setItalic(style, true);

// A style can have multiple attributes; this one makes text bold and italic
style = textPane.addStyle("Bold Italic", null);
StyleConstants.setBold(style, true);
StyleConstants.setItalic(style, true);

// Set text in the range [5, 7) red
doc.setCharacterAttributes(5, 2, textPane.getStyle("Red"), true);

// Italicize the entire paragraph containing the position 12
doc.setParagraphAttributes(12, 1, textPane.getStyle("Italic"), true);

 

Enumerating the parameters in the Swing JTextPane

Introduction

JPanel
JPanel

The java.swing.jpanel is a generic lightweight container. For examples and task-oriented documentation for JPanel, see How to Use Panels, a section in The Java Tutorial. The example below shows how to enumerate over the paragraphs in the JTextPane.

JTextPane
JTextPane

 

 

 

 

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

// Get section element 
Element section = textPane.getDocument().getDefaultRootElement();

// Get number of paragraphs.
// In a text pane, a span of characters
// terminated by single
// newline is typically called a paragraph.
int paraCount = section.getElementCount();

// Get index ranges for each paragraph
for (int i=0; i<paraCount; i++) {
    Element e = section.getElement(i);
    int rangeStart = e.getStartOffset();
    int rangeEnd = e.getEndOffset();
    try {
        String para = textPane.getText(rangeStart, rangeEnd-rangeStart);
    } catch (BadLocationException ex) {
    }
}

See also the Swing UITutorial.

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