# txtpane_color.py # Copyright (C) 2003 Frederic Laurent # # txtpane_color.py is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # txtpane_color.py is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with txtpane_color.py; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA from javax.swing import * from javax.swing.text import * from java.awt import * # The main frame with a JTexPane frame = JFrame("JTextPane: color test", size=(400,200), defaultCloseOperation=JFrame.EXIT_ON_CLOSE) txtpane = JTextPane() sdoc = txtpane.getStyledDocument() frame.getContentPane().add(JScrollPane(txtpane)) frame.visible=1 # various styles titleStyle = sdoc.addStyle("title", None) StyleConstants.setBackground(titleStyle , Color.WHITE) StyleConstants.setForeground(titleStyle , Color.BLUE) StyleConstants.setFontFamily(titleStyle , "dialoginput") StyleConstants.setBold(titleStyle , 1) StyleConstants.setFontSize(titleStyle , 12) descrStyle = sdoc.addStyle("description", titleStyle) StyleConstants.setForeground(descrStyle, Color.RED) StyleConstants.setBold(descrStyle, 0) # insert the text sdoc.insertString(0, "title: ", sdoc.getStyle("title")) sdoc.insertString(sdoc.getLength(), "a big description about nothing in particular...", sdoc.getStyle("description"))