next up previous
Next: Progress Bars, Scroll Bars Up: Text In Swing Previous: Viewing RTF format

Simple document processing

Finally we develop a simple example to demonstrate some additional features :

Let us consider a very simple word processing example, we will examine how this simple, powerful class can be integrated into your own code.

This example focuses on how text style changes work within the JTextPane class.

So, the example shows only how the text style mechanism works, but the code does not support file management, or most of the other features of a complete word processor (see assessed exercise)

The JTextpaneExample.java code creates an application window with an instance of a JTextPane, and a simple toolbar containing only a combo box:

JTextpaneExample Output

The program illustrates how to create several different text styles which we can apply to any test we enter into the editor. Each of the styles we create is added to an instance of a hash table, so we can support a number of different styles in our editor.

The createStyles() method builds each text style, and inserts them into this table for later reference when the user makes a choice from the combo box.

Compile and run the program and notice how easily styles can be mixed within the display.

The code listing for JTextpaneExample.java is as follows:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

class JTextpaneExample
		extends		JFrame
		implements	ActionListener
{
	private Hashtable				attributes;
	private	JComboBox				styleCombo;
	private	DefaultStyledDocument	doc;
	private	JTextPane				textComponent;

	public JTextpaneExample()
	{
		setTitle( "Document	Handling Application" );
		setSize( 300, 190 );
		setBackground( Color.gray );

		JPanel topPanel	= new JPanel( new BorderLayout() );
		getContentPane().add( topPanel );

		// Create styles for the document
		StyleContext sc = new StyleContext();
		doc = new DefaultStyledDocument( sc );
		createStyles( sc );

		// Create a text pane to display text
		textComponent = new JTextPane( doc );
		textComponent.setBackground( Color.white );
		topPanel.add( textComponent, BorderLayout.CENTER );

		// Create a toolbar to handle style changes
		topPanel.add( createToolBar(), BorderLayout.NORTH );
	}

	// Create a VERY simple toolbar panel
	public JPanel createToolBar()
	{
		JPanel panel = new JPanel( new FlowLayout() );
	
		styleCombo = new JComboBox();
		styleCombo.addActionListener( this );
		panel.add( styleCombo );
		
		// Add each style to the combobox
		for( Enumeration e = attributes.keys(); e.hasMoreElements(); ) 
			styleCombo.addItem( e.nextElement().toString() ); 

		return panel;
	}

	// Handle changes to the combobox (style changes)
	public void actionPerformed( ActionEvent e )
	{
		if( e.getSource() == styleCombo )
		{
			try {
				// Determine the new style
				Style s = (Style)attributes.get( styleCombo.getSelectedItem() );
			            
				// Set the style and return to the editor
				doc.insertString( textComponent.getCaret().getDot(), " ", s );
		    	textComponent.grabFocus();
			}
			catch( BadLocationException exception )
			{
		    }
		}
	}
	
	// Create some different font styles
	public void createStyles( StyleContext sc )
	{
		Style	myStyle;
		        
		// Allocate a hashtasble for our styles
		attributes = new Hashtable();

		// No style
		myStyle = sc.addStyle( null, null );
		attributes.put( "none", myStyle ); 

		// Normal
		myStyle = sc.addStyle( null, null );
		StyleConstants.setLeftIndent( myStyle, 10 );
		StyleConstants.setRightIndent( myStyle, 10 );
		StyleConstants.setFontFamily( myStyle, "Helvetica" );
		StyleConstants.setFontSize( myStyle, 14 );
		StyleConstants.setSpaceAbove( myStyle, 4 );
		StyleConstants.setSpaceBelow( myStyle, 4 );
		attributes.put( "normal", myStyle ); 

		// Big
		myStyle = sc.addStyle( null, null );
		StyleConstants.setFontFamily( myStyle, "Dialog" );
		StyleConstants.setFontSize( myStyle, 28 );
		attributes.put( "big", myStyle ); 

		// Bold
		myStyle = sc.addStyle( null, null );
		StyleConstants.setBold( myStyle, true );
		attributes.put( "bold", myStyle ); 
	}

	// Main() method to	get	the ball rolling
	public static void main( String args[] )
	{
		// Create an instance of the test application
		JTextpaneExample mainFrame	= new JTextpaneExample();
		mainFrame.setVisible( true );
	}
}


next up previous
Next: Progress Bars, Scroll Bars Up: Text In Swing Previous: Viewing RTF format
Dave Marshall
4/14/1999