1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class TextPaneAppend extends JTextPane
{
private static final long serialVersionUID = 1L;
public void append( AttributeSet attr, String chaine )
{
setCaretPosition( getDocument().getLength() );
setCharacterAttributes( attr, true );
replaceSelection( chaine );
setCaretPosition( getDocument().getLength() );
}
public AttributeSet appendAttributeSet( AttributeSet old, Object name, Object value )
{
return StyleContext.getDefaultStyleContext().addAttribute( old, name, value );
}
public void append( Object attributeName, Object attributeValue, String chaine )
{
append(
StyleContext.getDefaultStyleContext().addAttribute(
SimpleAttributeSet.EMPTY, attributeName, attributeValue ),
chaine );
}
public static void main( String argv[] )
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
TextPaneAppend pane = new TextPaneAppend();
pane.append( StyleConstants.Bold, Boolean.TRUE, "coucou\n" );
pane.append( StyleConstants.Italic, Boolean.TRUE, "hello\n" );
pane.append(
pane.appendAttributeSet(
pane.appendAttributeSet( SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.WHITE ),
StyleConstants.Background, Color.RED ),
"et alors ?\n" );
pane.append( StyleConstants.Foreground, Color.RED, "salut\n" );
frame.setContentPane( new JScrollPane( pane ) );
frame.setSize( 640, 480 );
frame.setVisible( true );
}
} |
Partager