FileDocCategorySizeDatePackage
Stylepad.javaAPI DocSun JDK 1.4.2 Example9655Thu May 12 00:35:28 BST 2005None

Stylepad

public class Stylepad extends Notepad
Sample application using JTextPane.
author
Timothy Prinzing
version
1.17 01/23/03

Fields Summary
private static ResourceBundle
resources
Constructors Summary
public Stylepad()

        try {
            resources = ResourceBundle.getBundle("resources.Stylepad");
        } catch (MissingResourceException mre) {
            System.err.println("Stylepad.properties not found");
            System.exit(0);
        }
    
	super();
    
Methods Summary
javax.swing.JMenucreateColorMenu()

	ActionListener a;
	JMenuItem mi;
	JMenu menu = new JMenu(getResourceString("color" + labelSuffix));
	mi = new JMenuItem(resources.getString("Red"));
	mi.setHorizontalTextPosition(JButton.RIGHT);
	mi.setIcon(new ColoredSquare(Color.red));
	a = new StyledEditorKit.ForegroundAction("set-foreground-red", Color.red);
	//a = new ColorAction(se, Color.red);
	mi.addActionListener(a);
	menu.add(mi);
	mi = new JMenuItem(resources.getString("Green"));
	mi.setHorizontalTextPosition(JButton.RIGHT);
	mi.setIcon(new ColoredSquare(Color.green));
	a = new StyledEditorKit.ForegroundAction("set-foreground-green", Color.green);
	//a = new ColorAction(se, Color.green);
	mi.addActionListener(a);
	menu.add(mi);
	mi = new JMenuItem(resources.getString("Blue"));
	mi.setHorizontalTextPosition(JButton.RIGHT);
	mi.setIcon(new ColoredSquare(Color.blue));
	a = new StyledEditorKit.ForegroundAction("set-foreground-blue", Color.blue);
	//a = new ColorAction(se, Color.blue);
	mi.addActionListener(a);
	menu.add(mi);

	return menu;
    
protected javax.swing.text.JTextComponentcreateEditor()
Create an editor to represent the given document.

	StyleContext sc = new StyleContext();
	DefaultStyledDocument doc = new DefaultStyledDocument(sc);
	initDocument(doc, sc);
        JTextPane p = new JTextPane(doc);
	p.setDragEnabled(true);
        
        //p.getCaret().setBlinkRate(0);
        
        return p;
    
javax.swing.JComboBoxcreateFamilyChoices()

        JComboBox b = new JComboBox();
	String[] fonts = getToolkit().getFontList();
	for (int i = 0; i < fonts.length; i++) {
	    b.addItem(fonts[i]);
	}
	return b;
    
protected javax.swing.JMenucreateMenu(java.lang.String key)
Create a menu for the app. This is redefined to trap a couple of special entries for now.

	if (key.equals("color")) {
	    return createColorMenu();
	} 
	return super.createMenu(key);
    
public javax.swing.Action[]getActions()
Fetch the list of actions supported by this editor. It is implemented to return the list of actions supported by the superclass augmented with the actions defined locally.

        Action[] defaultActions = {
            new NewAction(),
            new OpenAction(),
            new SaveAction(),
            new StyledEditorKit.FontFamilyAction("font-family-LucidaSans", 
                                                 "Lucida Sans"),
        };
        Action[] a = TextAction.augmentList(super.getActions(), defaultActions);
	return a;
    
protected java.lang.StringgetResourceString(java.lang.String nm)
Try and resolve the resource name in the local resource file, and if not found fall back to the superclass resource file.

	String str;
	try {
	    str = this.resources.getString(nm);
	} catch (MissingResourceException mre) {
	    str = super.getResourceString(nm);
	}
	return str;
    
voidinitDocument(javax.swing.text.DefaultStyledDocument doc, javax.swing.text.StyleContext sc)

	Wonderland w = new Wonderland(doc, sc);
	//HelloWorld h = new HelloWorld(doc, sc);
	Icon alice = new ImageIcon(resources.getString("aliceGif"));
	w.loadDocument();
    
public static voidmain(java.lang.String[] args)

        String vers = System.getProperty("java.version");
        if (vers.compareTo("1.1.2") < 0) {
            System.out.println("!!!WARNING: Swing must be run with a " +
                               "1.1.2 or higher version VM!!!");
        }
        JFrame frame = new JFrame();
        frame.setTitle(resources.getString("Title"));
	frame.setBackground(Color.lightGray);
	frame.getContentPane().setLayout(new BorderLayout());
	frame.getContentPane().add("Center", new Stylepad());
	frame.addWindowListener(new AppCloser());
	frame.pack();
	frame.setSize(600, 480);
        frame.show();