Archive for October, 2007

Java Swing - O Reilly strategy only catches keyboard

Wednesday, October 31st, 2007

Java Swing - O Reilly strategy only catches keyboard entry. All you need is an extension of PlainDocument that overrides the insertString() method, to verify that the requested insertion is valid. Here’s a class that limits the number of characters in the field: // FixedLengthPlainDocument.java // import java.awt.Toolkit; import javax.swing.*; import javax.swing.text.*; // An extension of PlainDocument that restricts the length of the content it // contains. public class FixedLengthPlainDocument extends PlainDocument { // Create a new document with the given max length public FixedLengthPlainDocument(int maxLength) { this.maxLength = maxLength; } // If this insertion would exceed the maximum document length, we “beep” and do // nothing else. Otherwise, super.insertString() is called. public void insertString(int offset, String str, AttributeSet a) throws BadLocationException { if (getLength() + str.length() > maxLength) { Toolkit.getDefaultToolkit().beep(); } else { super.insertString(offset, str, a); } } private int maxLength; } To use this class, simply pass in an instance of it to the JTextField constructor. Or, if you plan on using this document type for many text fields, you might want to create a new subclass of JTextField. Here’s an example: // FixedLengthTextField.java - 646
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Java Swing - O Reilly doc.insertString(0, “Line Onen”, null); (Web design online)

Wednesday, October 31st, 2007

Java Swing - O Reilly doc.insertString(0, “Line Onen”, null); doc.insertString(doc.getLength(), “Line Twon”, null); doc.insertString(doc.getLength(), “Line Three”, null); root.dump(System.out,0); System.out.println(”==========”); // Remove “One/Line Two/Line”, dump again doc.remove(5, 18); root.dump(System.out,0); } catch (BadLocationException ex) {} } } Here’s the output produced by running this program:
[0,1][ ] ==========
[0,9][Line One] [9,18][Line Two] [18,29][Line Three] ==========
[0,11][Line Three] The first block of output shows the default structure of the PlainDocument. At this point, we just have a single branch element (refer to Table 20.12 to see where the strings “paragraph” and “content” come from), containing a single leaf element. The numbers inside the square brackets show the startOffset and endOffset of the leaf element. The second block of output shows the structure described at the beginning of this section. We still have a single branch element (the paragraph), but it now contains a leaf element for each line of text. The last block shows that removing 18 characters from the middle of the document (including two newline characters) caused the second element to be removed completely, and the first and third elements to be merged into a single new element containing the remaining text. 20.1.20.6 Filtering JTextFields A common question encountered when working with JTextFields is “How can I restrict the characters entered into the field?” In the AWT, this was typically done by adding a KeyListener to the field and consuming the keypress event if the input text was invalid. A much more robust way to fulfill this requirement is to use a document type that enforces the restriction for you. This ensures that any insertion into the Document (including keyboard entry, pasting from the clipboard, or programmatic insertions) will be checked. Using the KeyListener - 645
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

Graphic web design - Java Swing - O Reilly protected PlainDocument(AbstractDocument.Content c) Creates

Tuesday, October 30th, 2007

Java Swing - O Reilly protected PlainDocument(AbstractDocument.Content c) Creates a new document using the specified Content. It adds a document property reflecting a default tab size of 8 and creates a default root element by calling createDefaultRoot(). 20.1.20.4 Protected Methods The only new methods defined in this class are the following protected methods. protected AbstractDocument.AbstractElement createDefaultRoot() Creates the Document’s default root element. The returned element is an AbstractDocument.BranchElement, containing a single empty Abstract- Document.LeafElement. protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) Indicates that content has been inserted into the Document. It refreshes the Element structure, adding and removing LeafElements as necessary to reflect the specified insert. If any Elements are added or removed by this method, indicating that lines have been added or removed, an ElementEdit (discussed at the end of the chapter) is created and added to the given event. protected void removeUpdate(DefaultDocumentEvent chng) Called to indicate that content has been removed from the Document. If the removal spans lines, the lines outside the removal range are joined together into a single Element. If this happens, the added and removed lines are used to create an ElementEdit (discussed at the end of the chapter), which is added to the given event. 20.1.20.5 Looking at PlainDocument’s Element Structures This example shows the Element structure used by PlainDocument using the AbstractDocument.dump() method. We start by printing the structure of an empty PlainDocument. We then add three lines of text and print again. Finally, we remove part of the first line, the entire second line, and part of the third line, showing how elements are removed and merged by the Document. Here’s the source for this simple example: // PlainDocExample.java// import javax.swing.text.*; public class PlainDocExample { public static void main(String[] args) { try { // Dump an empty document PlainDocument doc = new PlainDocument(); AbstractDocument.AbstractElement root = (AbstractDocument.AbstractElement) doc.getDefaultRootElement(); root.dump(System.out,0); System.out.println(”==========”); // Add 3 lines of text, dump again - 644
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Java Swing - O Reilly This is where an (Web hosting mysql)

Tuesday, October 30th, 2007

Java Swing - O Reilly This is where an AbstractDocument’s content is actually stored. The StringContent class showed a default implementation of this interface, currently used by all text components. GapContent provides an alternate, more efficient implementation. AbstractDocument.AttributeContext An interface that allows AttributeSets to be managed across multiple documents for efficiency purposes. Next, we’ll finally take a look at a concrete Document implementation called PlainDocument. 20.1.20 The PlainDocument Class PlainDocument is an extension of AbstractDocument used for simple documents that do not need to manage complex formatting styles. The JTextField, JPasswordField, and JTextArea classes use PlainDocument as their default model. It’s worth noting that PlainDocument provides more power than these components typically need. As an extension of AbstractDocument, it supports the use of AttributeSets, allowing the document to contain different fonts, colors, font styles, etc. These attributes are ignored when rendering the simple text components that use this document type. The Elements that make up a PlainDocument correspond to distinct lines of text that end in new line characters (n). Each line of text maps to a single LeafElement. All of these LeafElements are then contained by a single BranchElement (the document’s root element). 20.1.20.1 Property PlainDocument defines a default property value as shown in Table 20.17. The defaultRootElement created by PlainDocument is an AbstractDocument.BranchElement object containing a single empty AbstractDoc-ument.LeafElement child. Table 20.17, PlainDocument Property Property Data Type get is set bound Default Value defaultRootElement* Element AbstractDocument.BranchElement() Constant Type Description See also properties from the AbstractDocument class (Table 20.10) 20.1.20.2 Constants PlainDocument defines the constants shown in Table 20.18. Table 20.18, PlainDocument Constants Constant Type Description The name of the property used to specify the maximum length of a line, if there is lineLimitAttribute String one tabSizeAttribute String The name of the property used to specify the sizefortabs 20.1.20.3 Constructors public PlainDocument() Calls the protected constructor below, passing in a new instance of StringContent. - 643
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Java Swing - O Reilly Creates a new exception (Jetty web server)

Monday, October 29th, 2007

Java Swing - O Reilly Creates a new exception as a result of an attempt to access the specified offset. 20.1.18.2 Methods public int offsetRequested() Returns the offending offset that caused the exception to be thrown. 20.1.19 Model Summary So Far In this chapter, we’ve defined all of the abstractions that make up the Swing document model. Before going on, let’s briefly review the interfaces and classes we’ve covered so far. Document This is the root of the text model. Each Swing text component references a Document model that stores its text. We also looked at AbstractDocument, an abstract class that implements Document and defines some default behavior, including document locking. Element Documents are partitioned by Elements. An Element contains a pair of offsets into a document, referencing a collection of text with a common set of attributes. AbstractDocument defines three inner classes that implement this interface: AbstractElement, LeafElement, and BranchElement. AttributeSet Each Element of a Document is defined, in part, by an arbitrary set of attributes called an AttributeSet. Attribute values in an AttributeSet are arbitrary Objects, accessible by unique keys (also Objects). MutableAttributeSet An extension of AttributeSet that allows attributes to be added and removed. We also looked at SimpleAttributeSet, a default implementation of MutableAttributeSet that uses a Hashtable to implement the interface. Position A Position is simply an unchanging point within a document. It is more stable than an offset, since it moves with its attached text. Segment A Segment is a simple container for an array of characters used to allow fast access to some segment of text. AbstractDocument.Content - 642
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Java Swing (Florida web design) - O Reilly The methods in AbstractDocument.AbstractElement

Monday, October 29th, 2007

Java Swing - O Reilly The methods in AbstractDocument.AbstractElement which modify an Element’s attribute set use the document’s AttributeContext to make the modifications. 20.1.17.1 Property The AbstractDocument.AttributeContext interface defines the property shown in Table 20.16. The emptySet property consists of an attribute set containing no attributes. Table 20.16, AbstractDocument.AttributeContext Property Property Data Type get is set bound Default Value emptySet AttributeSet 20.1.17.2 Attribute Management The following methods are defined for managing attribute sets: public abstract AttributeSet addAttribute(AttributeSet old, Object name, Object value) Returns a set that contains the attributes of the given set, plus the newly specified attribute. public abstract AttributeSet addAttributes(AttributeSet old, AttributeSet attr) Returns a set that contains the result of adding the second set of attributes to the first. public abstract void reclaim(AttributeSet a) Called to indicate that a set is no longer being used. It may be removed from the context if no one else is using it. public abstract AttributeSet removeAttribute(AttributeSet old, Object name) Returns a set that contains the result of removing the attribute with the specified key from the given set. Equality is defined by the equals() method. public abstract AttributeSet removeAttributes(AttributeSet old, Enumeration names) Returns a set that contains the result of removing all of the given attribute keys from the given set. public abstract AttributeSet removeAttributes(AttributeSet old, AttributeSet attrs) Returns a set that contains the result of removing all attributes in the second set from the first set. Only attributes where the keys and values match should be removed. 20.1.18 The BadLocationException Class This exception is thrown by many of the text classes to indicate that an attempt has been made to access an invalid offset into the document. 20.1.18.1 Constructor public BadLocationException(String s, int offs) - 641
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Personal web server - Java Swing - O Reilly Once we’ve filled the

Sunday, October 28th, 2007

Java Swing - O Reilly Once we’ve filled the gap (line 5), the array must be enlarged (line 6) to hold the entire document. 20.1.16.1 Constructors public GapContent() Creates a content object with an initial array size of 10. public GapContent(int initialLength) Creates a content object with the specified size. 20.1.16.2 AbstractDocument.Content Methods The following methods implement the AbstractDocument.Content interface. public Position createPosition(int offset) throws BadLocationException Creates a Position at the specific document offset. This implementation manages a sorted array of positions to make it easy to find the marks that need to be updated when the gap is shifted. public void getChars(int where, int len, Segment chars) throws BadLocationException Populates the given Segment with the requested text (len characters, starting at where). If the requested text falls entirely on one side of the gap, the whole internal array is returned. If not, a new array is constructed, containing the specified range with the gap removed. public String getString(int where, int len) throws BadLocationException Uses getChars() to return the requested portion of the content. public UndoableEdit insertString(int where, String str) throws BadLocationException Inserts the specified text at the given location. The gap and positions are adjusted as necessary. Currently, this method always returns null. public int length() Returns the length of the content. The gap does not count in this value. public UndoableEdit remove(int where, int nitems) throws BadLocationException Removes the specified number of characters from the content, starting at where. The gap and positions are updated as necessary. 20.1.17 The AbstractDocument.AttributeContext Interface This inner-interface of AbstractDocument defines a set of methods for managing attributes in one or more documents. The idea is that replicating AttributeSets throughout a Document could be very expensive. Implementations of this interface (see the StyleContext class in Chapter 21) are intended to provide intelligent management of attribute sets, eliminating costly duplication. - 640
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

Best web site - Java Swing - O Reilly Obtains a vector of

Sunday, October 28th, 2007

Java Swing - O Reilly Obtains a vector of position references spanning the specified range. The return value can be passed to updateUndoPositions() to update the positions. protected void updateUndoPositions(Vector positions) Updates the position references returned by getPositionsInRange(). 20.1.16 The GapContent Class This implementation of the AbstractDocument.Content interface made its Swing debut in Swing 1.0.2. As of JDK 1.2 beta4, it was not actually used within the Swing text package, possibly because it did not yet support undoable edits. Look for this feature to be added in a future release, but for now you can use it in your own applications if you don’t need undo support. The idea behind GapContent is to take advantage of the fact that text is typically inserted sequentially. In other words, if the user inserts a character at position 10 in the document, chances are good that the next insertion will be at position 11. With StringContent, each insertion results in an array copy to make room for the new text. In contrast, GapContent keeps a “gap” in its character array, located at the current insertion point. When the insertion point changes, the gap is moved to the new input location by shifting the contents of the array to the end. The easiest way to understand this is by example. Figure 20.7 shows how GapContent would manage a 35-character array (you can specify the initial size when you create the content object). Each line represents the state of the array at some point of the insertion process. We show the “gap” by shading that portion of the array. Figure 20.7. GapContent example Initially, the entire array is the gap (actually, GapContent starts with a single n in the array, but we’ll ignore that detail). As text is inserted (lines 1 and 2), the right edge of the gap is shifted, staying just beyond that last inserted character. Line 3 is the most important. This line shows what happens when we go back and insert text into the middle of the document. At this point, everything to the right of the insertion point is copied to the end of the array via a single array copy. We’re now (line 4) able to insert additional data into the middle of the document without having to pay for additional array copies. Contrast this approach with the approach used by StringContentin which the insertion of the letter w would have shifted “anymore.” down one space, the insertion of e would have shifted it down again, and so on. - 639
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

Java Swing - O Reilly Creates an array sized (Web design programs)

Saturday, October 27th, 2007

Java Swing - O Reilly Creates an array sized to hold a default (10) number of characters, initially containing a single newline character. public StringContent(int initialLength) Creates an array of the specified size, initially containing a single newline character. 20.1.15.2 Public Methods public Position createPosition(int offset) throws BadLocationException Creates a Position that will track changes at the specified offset. This is done by keeping a Vector of “marks,” which are updated each time content is inserted or removed. The returned Position is of type StringContent.StickyPosition (a non-public inner class). public void getChars(int where, int len, Segment txt) throws BadLocationException Returns the specified range of text. The entire data array is returned in the Segment with the specified offset and length set. public String getString(int where, int len) throws BadLocationException Creates and returns a new String containing the specified range of text. public UndoableEdit insertString(int where, String str) throws BadLocationException Inserts the specified string at the requested offset in the content. If necessary, the array size is increased (by doubling, or by adding the size of the new string, whichever is larger) to accommodate the new text. All of the Positions previously created by createPosition() calls are updated to reflect the newly added text. An UndoableEdit (a non-public inner class called InsertUndo) is returned, which allows this insert to be undone. public int length() Returns the current length of the content. This is the number of characters in the array, not the total allocated size of the array. public UndoableEdit remove(int where, int nitems) throws BadLocationException Removes the specified number of characters from the content by copying the appropriate segments of the array. All of the Positions previously created by createPosition() calls are updated to reflect the removed text. An UndoableEdit (a non-public inner class called RemoveUndo) is returned, which allows this removal to be undone. 20.1.15.3 Protected Methods The following methods are used by the non-public undoable edit inner classes to provide undo support. protected Vector getPositionsInRange(Vector v, int offset, int length) - 638
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Java Swing - O Reilly 20.1.14 The AbstractDocument.Content Interface

Saturday, October 27th, 2007

Java Swing - O Reilly 20.1.14 The AbstractDocument.Content Interface This interface is used to represent a sequence of data that may be edited. This is where an AbstractDocument actually stores its content. Many of the methods in AbstractDocument simply delegate the call to the document’s Content object. 20.1.14.1 Methods public abstract Position createPosition(int offset) throws BadLocationException Creates a Position that will track changes at the specified offset. public abstract void getChars(int where, int len, Segment txt) throws BadLocationException Returns the specified range of text. The result is passed back in the input Segment object. public abstract String getString(int where, int len) throws BadLocationException Returns a String containing the specified range of text. public abstract UndoableEdit insertString(int where, String str)throws BadLocationException Inserts the specified string at the requested offset in the content. If the Content implementation supports undo, an UndoableEdit object will be returned; otherwise this method returns null. public abstract int length() Returns the current length of the content. public abstract UndoableEdit remove(int where, int nitems) throws BadLocationException Removes the specified number of items (characters, icons, or components) from the document content. If the Content implementation supports undo, an UndoableEdit object will be returned, otherwise this method returns null. 20.1.15 The StringContent Class This is a basic implementation of the AbstractDocument.Content interface. At present, this is the implementation used by all of the Swing text components, though there may be a change to the newer GapContent class in a later release. The content is stored in a character array, and changes are made by modifying the array’s contents and copying the array when necessary to support growth. StringContent supports undoable edits, so the insertString() and remove() methods always return non-null UndoableEdit objects. 20.1.15.1 Constructors public StringContent() - 637
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.