Archive for August, 2007

Java Swing - O Reilly public abstract Position createPosition(int (Web hosting providers)

Friday, August 31st, 2007

Java Swing - O Reilly public abstract Position createPosition(int offs) throws BadLocationException Creates a Position object used to track the contents of the Document at the specified offset. Section 20.1.7, later in this chapter, contains an example showing how this might be used. public abstract Object getProperty(Object key) public abstract void putProperty(Object key, Object value) Retrieves and inserts (respectively) arbitrary properties associated with the document. These properties can be used to store things such as the document title, author, etc. public abstract void render(Runnable r) Executes the given Runnable, guaranteeing that the contents of the model will not be changed while the Runnable is running. The input Runnable may not modify the model. This method allows the Document to be painted without concerns about its contents changing during the painting process. It is called by the TextUI’s paint() method. 20.1.2 The Element Interface Element is an interface used to describe an arbitrary portion of a document. However, it’s important to realize that Elements do not actually contain a portion of the document, they just define a way of structuring it. Because of this, a Document may be described by multiple sets of Elements, each defining a different logical structure. Each Element is described by an AttributeSet, which defines things such as the font style or color used in the Element. Note that this means that all Document content described by a given Element will have the same set of attributes. Elements may contain other Elements, allowing a document to be described by an arbitrary tree structure. Figure 20.2 shows a purely hypothetical example of the structure of a series of Elements. In this diagram, there is a single “document” element made up of two “paragraph” elements, each of which is, in turn, made up of a pair of “sentence” elements. Finally, one of the sentences is further split into two more elements, one for a plain text area and the other for a section of bold text. Remember, this is only an example of how elements fit together; the element types here do not map directly to anything provided by Swing. Figure 20.2. Sample Element structure Another way to understand the concept of an Element is to look at mark-up languages such as HTML or XML. Documents defined by such mark-up languages can be easily represented using the - 612
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Java Swing - O Reilly Document defines the following (Web hosting support)

Friday, August 31st, 2007

Java Swing - O Reilly Document defines the following standard methods for managing event listeners. public abstract void addDocumentListener(DocumentListener listener) public abstract void removeDocumentListener(DocumentListener listener) public abstract void addUndoableEditListener(UndoableEditListener listener) public abstract void removeUndoableEditListener(UndoableEditListener listener) 20.1.1.3 Constants Document defines the two constants, shown in Table 20.2, which may be used as keys when calling getProperty() or putProperty(). Table 20.2, Document Constants Constant Type Description The property name used to describe any information known about the StreamDescriptionProperty String stream from which the Document was initialized (if it was initialized from a stream) TitleProperty String The property name used to store the Document’s name, if it has one 20.1.1.4 Text Manipulation Methods These methods manipulate the contents of the document. They can all throw a BadLocationException. This is an exception (defined later in the chapter) used throughout the text package to indicate that an attempt has been made to reference a document offset that does not exist. For example, attempting to insert text at offset 200 of a 100-character document would result in a BadLocation-Exception. public abstract String getText(int offset, int length) throws BadLocationException Retrieves the text at the specified location in the Document. The returned string will be of the requested length, beginning at the specified offset. public abstract void getText(int offset, int length, Segment txt) throws BadLocationException Retrieves the text at the specified location in the Document. The returned text will be of the requested length, beginning at the specified offset, and is returned in the input Segment(defined later in the chapter) object. public abstract void insertString(int offset, String str, AttributeSet a) throws BadLocationException Inserts the specified string at the given offset in the Document. The new String should be described by the given AttributeSet, and the Document should update its Element structures as necessary to reflect this. public abstract void remove(int offs, int len) throws BadLocationException Removes the specified section of text from the Document. The Document should update its Element structures to reflect the change. 20.1.1.5 Other Methods - 611
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Java Swing - O Reilly multiple ways of structuring

Thursday, August 30th, 2007

Java Swing - O Reilly multiple ways of structuring the same document content. The key thing to understand is that the Document contains the data; the Elements just give it structure. Each Element has an associated AttributeSet, which defines things such as the font or text style to be applied to the Element. The attributes themselves may be any arbitrary Object (the value), indexed by another Object (the key). Arbitrary positions in the document may be represented using Position objects. Position provides the ability to track a given location in the document as its absolute offsets change. For example, if you created a position that pointed to the beginning of a given sentence, that position would always refer to the beginning of the sentence, no matter what other changes were made to the document. The Document, Element, Position, and AttributeSet interfaces are all described in detail later in this section. 20.1.1 The Document Interface We’ve described the Document interface a bit already. Now let’s take a look at its properties, constructors, and methods. 20.1.1.1 Properties Table 20.1 shows the properties defined by the Document interface. The properties defined by the Document interface are fairly straightforward. A property called defaultRootElement is provided to access the Element at the root of the default Element structure hierarchy. In addition, the rootElements property specifies an array of all root elements (including the default) available in the Document. Most Document types only define a single root. Table 20.1, Document Properties Property Data Type get is set bound Default Value defaultRootElement Element endPosition Position length int rootElements Element[] startPosition Position Two Positions, startPosition and endPosition, are provided. These properties always reference the beginning and end of the document, regardless of changes made to it throughout its lifetime. Finally, the length property specifies the total number of characters[2] in the Document. [2] We’ll see in the next chapter that Documents are not limited to holding text. In most cases, length refers to the number of characters in the Document, but embedded images and components may also be included in this count, each being stored as a single character. Newline characters count as well, so a document containing “HellonWorld” would have a length of 11. 20.1.1.2 Events Implementations of the Document interface fire DocumentEvents and UndoableEditEvents to indicate changes that have been made to the Document’s contents. UndoableEditEvent and UndoableEditListener were covered in Chapter 18. The corresponding DocumentEvent classes will be discussed at the end of this chapter. - 610
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Java Swing - O Reilly Over (Bulletproof web design) the next five

Thursday, August 30th, 2007

Java Swing - O Reilly Over the next five chapters, we’ll examine every class and interface in the Swing text package, building many interesting and powerful sample programs as we go. The order of the next five chapters is fairly important. You should read the next two chapters in sequence to gain an understanding of the interfaces and classes used to describe the document model. The chapter discussing Text Views, Chapter 23, is fairly advanced and will be optional for many readers. Most of the material covered in the last chapter will make sense even if you skip the Views chapter. Chapter 20. Document Model and Events In the previous chapter, we introduced the Swing text components, ignoring many of the details in order to simplify the introduction. In this chapter,[1] we’ll give a detailed explanation of the many classes and interfaces that make up the basic text model. Additionally, we’ll look at the classes and interfaces involved in delivering events to provide notification of document model changes. [1] Certain sections of this chapter refer to Swing’s undo facility, and assume that you already understand the basics of it. If you’ve not yet read Chapter 18, we recommend that you read at least the introductory section of it before continuing here. 20.1 The Document Model Before getting into the details of the classes and interfaces that make up the Swing text model, we’ll give a brief overview of the top-level interfaces that serve as the model’s key abstractions. These interfaces, and the relationships between them, are shown in Figure 20.1. Figure 20.1. High-level Document class diagram The first and arguably most important interface to look at is called Document. The model representation of any text component (even a simple text field) in Swing is defined by this interface. A Document is little more than an arbitrary collection of text (though Swing’s implementations of the interface provide much more). Document objects are partitioned by Element s that describe the structural pieces of a Document, such as paragraphs or sections of specially formatted text (e.g., italics). Elements are made up of child Elements, forming a tree with a “root” Element as the base and arbitrary levels of child Elements below it. Each Document defines one or more Elements as its root Elements, from which the entire structure of the Document can be determined. Typically, only a single root Element is defined. However, more complex document types might contain multiple root elements, to support - 609
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Java Swing - O Reilly StyleConstants.setForeground(attrs, Color.red); StyleConstants.setItalic(attrs, true); (My space web page)

Wednesday, August 29th, 2007

Java Swing - O Reilly StyleConstants.setForeground(attrs, Color.red); StyleConstants.setItalic(attrs, true); StyleConstants.setBold(attrs, true); showMsg(msg, attrs); } // Return the visual component to be displayed public Component getComponent() { return pane; } // Show a text message using the specified AttributeSet protected void showMsg(String msg, AttributeSet attrs) { Document doc = pane.getDocument(); msg += “n”; try { doc.insertString(doc.getLength(), msg, attrs); } catch (BadLocationException ex) { ex.printStackTrace(); } } private JTextPane pane; // A sample test program public static void main(String[] args) { Diagnostic diag = new Diagnostic(); JFrame f = new JFrame(); f.addWindowListener(new BasicWindowMonitor()); f.getContentPane().add(diag.getComponent()); f.setSize(300,200); f.setVisible(true); // Display a few messages… diag.showInfo(”System normal”); diag.showWarning(”Disk space low”); diag.showError(”Out of memory”); diag.showError(”Program performed an illegal operation”); diag.showInfo(”System normal”); } } Over the course of the next two chapters, we’ll explain all of the strange-looking methods used in this example. You might want to refer back to this example as you read those chapters. Figure 19.12 shows the display generated by this class. Unfortunately, we can’t show you the colors. Figure 19.12. JTextPane diagnostic example 19.2 More to Come In this chapter, we’ve shown how easy it is to do simple things with the Swing text framework. However, if you want to do more than we’ve demonstrated in this chapter, Swing has a lot to offer. - 608
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Java Swing - O Reilly In this chapter, we’ll (Web server logs)

Wednesday, August 29th, 2007

Java Swing - O Reilly In this chapter, we’ll just touch the surface, showing how you can do a few simple things using a JTextPane that you couldn’t easily do with the AWT or JTextArea. Since most of the properties and methods defined in JTextPane use classes and interfaces that we’ve not yet defined, we’ll omit all of the details of JTextPane properties and methods from this section. For a more complete discussion of JTextPane, refer to Chapter 21. 19.1.9.1 Using the JTextPane In this example, we’ll show how to use a JTextPane as a more powerful text window for displaying diagnostic information. In AWT, or with a Swing JTextArea, you are limited to a single font, style, and foreground color. Using a JTextPane, we can easily add text using a variety of formats. This is done using AttributeSets to define the features of the displayed text. For the purpose of this example, it’s enough to understand that an AttributeSet (we’ll actually use SimpleAttributeSet, a mutable extension of AttributeSet) defines a group of attributes and that the values of these attributes can be set by calling various static methods on the StyleConstants class. Here’s a simple utility for displaying diagnostic messages to a user. Three types of messages (informational, warning, and error) are displayed using different colors and text styles. // Diagnostic.java // import javax.swing.*; import javax.swing.text.*; import java.awt.*; public class Diagnostic { // Create new new Diagnostic display using a black JTextPanepublic Diagnostic() { pane = new JTextPane(); pane.setBackground(Color.black); pane.setEditable(false); } // Show an informational message (green, plain text) public void showInfo(String msg) { SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setForeground(attrs, Color.green); showMsg(msg, attrs); } // Show a warning message (yellow, italic text) public void showWarning(String msg) { SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setForeground(attrs, Color.yellow); StyleConstants.setItalic(attrs, true); showMsg(msg, attrs); } // Show an error message (red, bold/italic text) public void showError(String msg) { SimpleAttributeSet attrs = new SimpleAttributeSet(); - 607
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Web site hosting - Java Swing - O Reilly Called to indicate that

Tuesday, August 28th, 2007

Java Swing - O Reilly Called to indicate that a hyperlink request has been made. Typical implementations of this method obtain the new URL from the event and call setPage() on the associated JEditorPane. See the JEditorPane example earlier in the chapter to see how this method can be used. 19.1.8 The HyperlinkEvent Class This event class (found in javax.swing.event) describes a hyperlink request. 19.1.8.1 Properties HyperlinkEvent defines the properties shown in Table 19.9. The description property allows a description of the link (typically the URL text string) to be defined. This is useful when a URL can’t be formed from the text, meaning that the URL will be null. The eventType property defines the type of event that has occurred. The possible values are described below. The URL property reflects the URL the event refers to. Table 19.9, HyperlinkEvent Properties Property Data Type get is set bound Default Value description string null eventType HyperlinkEvent.EventType From constructor URL URL From constructor See also the java.util.EventObject class (not covered in this book). 19.1.8.2 Constructors public HyperlinkEvent(Object source, HyperlinkEvent.EventType type, URL u) public HyperlinkEvent(Object source, HyperlinkEvent.EventType type, URL u, string desc) Creates a new event with the specified arguments. The value of the type parameter is taken from the constant values defined in the HyperlinkEvent.EventType class: ENTERED, EXITED, and ACTIVATED. The only value currently used is ACTIVATED. The desc parameter is optional. 19.1.8.3 Inner Classes public static final class EventType This simple inner class just holds a single String value and defines three constants of type HyperlinkEvent.EventType. These constants are ENTERED, EXITED, and ACTIVATED. Currently, only ACTIVATED is fired by the HTMLEditorKit. It is used to indicate that a hyperlink has been activated (clicked) by the user. The other values could be used to change the cursor type when the cursor is moved over (or away from) a hyperlink. 19.1.9 The JTextPane Class JTextPane is a complex extension of JEditorPane that provides functionality typical of a basic word processor, including features such as multicolored text, multiple fonts and text styles, image embedding, and more. There’s a great deal to discuss with respect to JTextPane and the many classes it interacts with. - 606
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Web server certificate - Java Swing - O Reilly setEditorKitForContentType()). If the appropriate

Tuesday, August 28th, 2007

Java Swing - O Reilly setEditorKitForContentType()). If the appropriate EditorKit cannot be created, a DefaultEditorKit is returned. public void setEditorKitForContentType(String type, EditorKit k) Explicitly sets the EditorKit to be used for a given content type. public static EditorKit createEditorKitForContentType(String type) Attempts to create a new EditorKit instance for the given content type. In order for this method to return a non-null object, the content type must have been associated with an editor kit class name by a call to register-EditorKitForContentType(). public static void registerEditorKitForContentType(String type, String classname) Called to associate a content type to an editor kit class name. It is called three times in the JEditorPane initializer block. These calls define the mappings shown in Table 19.8. Table 19.8, Default Content-Type Mappings Content Type Class application/rtf javax.swing.text.rtf.RTFEditorKittext/plain javax.swing.JEditorPane.PlainEditorKit[5] text/html javax.swing.text.html.HTMLEditorKit text/rtf javax.swing.text.rtf.RTFEditorKit [5] A package private subclass of DefaultEditorKit. protected EditorKit createDefaultEditorKit() Called to create a default editor kit. It returns a new instance of a package private subclass of DefaultEditorKit called PlainEditorKit. This method is not used typically, as it is only called by getEditorKit() if no editor kit has been set for the pane. 19.1.6.5 Miscellaneous Methods public void setPage(String url) throws IOException A convenience method used to set the current page, given a URL string. An IOException will be thrown if the given URL cannot be loaded. protected void scrollToReference(String reference) Used by setPage() to scroll the display to the specified reference within the current document. This provides support for URLs containing references like JComponent updateUI. Note that this method only works with HTML documents. 19.1.7 The HyperlinkListener Interface This interface (found in javax.swing.event) defines a single method, used to respond to hyperlink activations. public abstract void hyperlinkUpdate(HyperlinkEvent e) - 605
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Web site translator - Java Swing - O Reilly The contentType property reflects

Monday, August 27th, 2007

Java Swing - O Reilly The contentType property reflects the type of content displayed by the editor. This value is taken from the installed EditorKit and typically has values such as “text/plain,” “text/html,” and “text/rtf.” The editorKit supplies everything needed to work with a particular content type. EditorKit and its default implementations are described in detail in Chapter 24. ManagingFocus is set to true for JEditorPane, reflecting the fact that using the TAB key will not cause focus to move to the next component. The page property specifies the URL of the current page being displayed. The scrollableTracksViewportWidth property is set to true in this class. 19.1.6.2 Events JEditorPane s fire a new type of event called a HyperlinkEvent. Typically, this is fired when the user clicks on a hyperlink in the currently displayed document; the program normally responds by loading a new page. To support this new event type, a new event class and listener interface are available in the javax.swing.event package. These are described briefly at the end of this section. As you’d expect, the following methods are provided for working with these events. public synchronized void addHyperlinkListener(HyperlinkListener listener) public synchronized void removeHyperlinkListener(HyperlinkListener listener) public void fireHyperlinkUpdate(HyperlinkEvent e) JEditorPane objects also fire PropertyChangeEvents when the editorKit property is changed. 19.1.6.3 Constructors The following constructors are provided. Note that the last two may throw an IOException if they are unable to load the specified URL. public JEditorPane() Creates an empty pane. public JEditorPane(String url) throws IOException public JEditorPane(URL initialPage) throws IOException Create a pane displaying the specified URL. contentType and editorKit are set based on the content type of the URLConnection created from the given URL. Because these constructors attempt to open a URL, they may throw an IOException if the URL cannot be found. 19.1.6.4 EditorKit Methods The following methods are available for managing EditorKits. You won’t need to use any of these methods if you’re not defining your own EditorKits for working with various content types. public EditorKit getEditorKitForContentType(String type) Returns an EditorKit for the given content type. An attempt is made to create the appropriate EditorKit if one has not already been set (via a call to - 604
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Java Swing - O Reilly pane = new JEditorPane(args[0]); (Personal web server)

Monday, August 27th, 2007

Java Swing - O Reilly pane = new JEditorPane(args[0]); } catch (IOException ex) { ex.printStackTrace(System.err); System.exit(1); } pane.setEditable(false); // Add a hyperlink listener final JEditorPane finalPane = pane; pane.addHyperlinkListener(new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent ev) { try { if (ev.getEventType() == HyperlinkEvent.EventType.ACTIVATED) finalPane.setPage(ev.getURL()); } catch (IOException ex) { ex.printStackTrace(System.err); } } }); JFrame frame = new JFrame(); frame.addWindowListener(new BasicWindowMonitor()); frame.setContentPane(new JScrollPane(pane)); frame.setSize(350,400); frame.setVisible(true); } } We’ve created a very minimal HTML browser.[4] In a real application, you’d want to do things like change the cursor while new pages are being loaded and handle exceptions more elegantly. The anonymous inner class in this example shows a quick way to enable hyperlinks when viewing text in a JEditorPane. We’ll look at the classes and methods used here in the next few pages. [4] This simple browser will not handle all HTML pages. The Swing html package is still being enhanced to provide better HTML support. However, significant progress has been made since earlier releases. 19.1.6.1 Properties Table 19.7 shows the properties defined by JEditorPane. The accessibleContext property depends on the type of EditorKit in use. If an HTMLEditorKit is installed, a special AccessibleJEditorPaneHTML object will be used. Otherwise, its superclass, AccessibleJEditorPane is used. AccessibleJEditorPane extends the JTextComponent.AccessibleJTextComponent class. Table 19.7, JEditorPane Properties Property Data Type get is set bound Default Value UIClassID* String “EditorPaneUI” accessibleContext* AccessibleContext AccessibleJEditorPane or AccessibleJEditorPaneHTML contentType String From editorKit editorKit EditorKit null managingFocus* boolean true page URL null scrollableTracksViewportWidth* boolean true Content Type Class See also properties from the javax.swing.text.JTextComponent class (Table 19.1) - 603
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.