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 56 57 58 59 60 61 62 63 64 65 66
   | import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
 
 
@SuppressWarnings("serial")
public class MyJTextPane extends JTextPane {
 
	private JButton btnURL = new JButton("lien");
 
 
	public MyJTextPane() {
		super();
 
		DefaultStyledDocument doc = (DefaultStyledDocument) this.getDocument();
		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
 
		Font font = new Font("Arial",Font.PLAIN,12);
 
		this.setFont(font);
		this.setEditable(false);
 
		Style plain = this.addStyle("plain", def);
		StyleConstants.setForeground(plain, Color.BLACK);
 
 
		Style gras = this.addStyle("gras", def);
		StyleConstants.setForeground(gras, Color.BLACK);
		StyleConstants.setFontSize(gras, 16);
		StyleConstants.setBold(gras, true);
 
		Style icone = this.addStyle("icone", def);
		StyleConstants.setIcon(icone, new ImageIcon("c:\\search2.png"));
 
		Style btn = this.addStyle("btn", def);
		StyleConstants.setComponent(btn, btnURL);
		btnURL.setForeground(Color.BLUE);
		btnURL.setPreferredSize(new Dimension(60, 20));
		btnURL.setMaximumSize(new Dimension(60, 20));
		btnURL.setMinimumSize(new Dimension(60, 20));
		btnURL.setContentAreaFilled(false);
 
		try {
			doc.insertString(doc.getLength(), " ", icone);
			doc.insertString(doc.getLength(), " Titre :\n", gras);
			doc.insertString(doc.getLength(), "ceci est mon texte ! ( ", plain);
			doc.insertString(doc.getLength(), " ", btn);
			doc.insertString(doc.getLength(), " ) dsqfksjqdljfk", plain);
			doc.insertString(doc.getLength(), "\naaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", plain);
		} 
		catch (BadLocationException e) {
			e.printStackTrace();
		}
 
	}
 
 
} | 
Partager