Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!

Bill Stackhouse

+ Follow
since Oct 17, 2003
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bill Stackhouse

I have written a JUnit framework and it has helped to standardize my tests. I use Eclipse as my IDE so I have a template to add prefix lines of code to each test class.

  • ruleClass logs the start/end of the class and creates a directory using the class name that lasts the life of the test class. Annotations allow overriding the location (tmp, desktop, etc.). If a collection of test classes need special setup/teardown, it is easy to extends either RuleGeneralClass or RuleGeneralTest, or use before/after annotated methods.
  • ruleTest logs the start of each test and creates a directory within the ruleClass direcotry using the test name lasting the life of one test. If a test fails it logs the failure with class:method:line number of the assertion failure. The (QA) in the listing below indicates it is using QA preference files.

  • I have a test utility class so I can call private methods, set private fields, call private constructors, reset annotated static variables, select production/test/qa preference files, etc. This is all part of a structured logging system with a log viewer with filters (time, application, level, caller of log system, etc.) so tests can write out plenty of detail without cluttering the console output. It's not something I can share, but I wanted to provide some ideas of things a test framework should help with.
    1 year ago
    I have MouseListeners on components in a JLayeredPane. Clicks on some, such as on a JLabel, seem to be received only by what is underneath it on the JFrame and not by the JLabel. Is there a way to ensure all mouse events for the JLayeredPane are only processed by the JLayeredPane?

    3 years ago
    Brian Cole, thanks for the suggestion. it worked, but of course forced the action everytime the cursor was set. I only wanted it in certain cases. There was just enough to show me how to solve the problem.

    4 years ago
    I would like to set the cursor in a JTextPane that is within a JScrollPane then have it positioned to the top of the visible rectangle. The default behavior is to position it to the bottom. How can I reposition the visible rectangle the correct amount if I know the cursor position in the document?

    4 years ago
    My mistake. checkControlWord should be the following.

    8 years ago
    Bug 4735839 addresses the alignment problem. It is dated 23-AUG-2002. I don't understand why Sun/Oracle is not supporting the javax.swing.text.rtf package but given that no code changes have been made in 1.4, 1.5, or 1.6, if developers are to use that functionality, they need to find solutions.
    10 years ago
    There is an additional change to support StyleConstants.spaceAbove and StyleConstants.spaceBelow. lines 48-51. The "sa" and "sb" are reversed. Lines should be as follows.

    10 years ago

    Writing RTF files from Java Swing DefaultStyledDocuments
    by Bill Stackhouse (

    May be freely reposted in its entirety. All java source contained may be freely used or modified in any products without license.


    Writing RTF files from Swing requires knowledge of RTF, Swing styled documents, and Swing styles. This is an attempt to pull the necessary information into a single tutorial. There are minor bugs in Swing support for RTF that need to be corrected and an explanation of how to do that is provided. Sample classes are provided for inserting styled text into a Swing styled document. Finally, an example is included.


    There are two open source products available to write RTF files - iText and SRW. Neither support integration with Swing styled documents, AWT fonts, or tagging RTF paragraphs with globally defined styles.

    iText in the past supported writing both PDF and RTF files. As of version 5.0.0, all RTF support has been removed.

    SRW provides basic support for writing RTF files.

    RTF Styles in Microsoft Word

    Microsoft Word supports styles defined globally for a document and specifically at the paragraph and character level. The use of globally defined styles allows consistent formatting throughout a document and simplifies changing formatting. These styles are defined and applied using the Format Style menu. They may be applied using the Formatting Toolbar as well. Style changes applied with Format Paragraph and Format Font are unique to the current selection and any text added at that location.

    Resulting RTF files saved by Microsoft Word will define \stylesheet for all globally defined styles. Paragraphs tagged with these styles will have a \pard to associate the style with the paragraph.

    Swing Styled Documents

    Swing text classes such as JTextArea use DefaultStyledDocument to provide formatting of text using the Style class. Portions of text are associated with a style and the document may have multiple styles. There is structure, but no concept of a 'paragraph'.

    Writing an RTF file does not require using JTextArea if the document is not displayed. Instead a DefaultStyledDocument can be created alone.

    Later, classes will be presented for encapsulating the details of inserting styled text into a document.

    Swing RTF Classes

    Swing RTF is often considered 'unusable'. Three problems and limitations minimize the utility of the Swing RTF classes, though there are many features that should be added and bugs fixed. Looking at the Java bug database for RTF bugs, there are many reported.

    1) alignment is always written as ALIGN_LEFT (\ql)
    2) \pard is only written if the Style has a special attribute
    3) no way to insert page breaks

    Comparing javax.swing.text.rtf between Java 1.4, 1.5, and 1.6, only a few changes were made to between 1.4 and 1.5. No changes other than copyright dates in comments were made between 1.5 and 1.6.

    Fixes are made by copying the source files from the package javax.swing.text.rtf into a new package in outside of the JDK, either in a separate project or in the one using the corrected classes. Since little maintenance is made to these files, this should not cause future problems or incompatibilities. All source in these files refer only to public methods in classes in other packages. Files for javax.swing.text.rtf.charsets are necessary. The line numbers below refer to the files from Java 1.6.

    On Mac OS the source is located in the JavaVM.framework/Home/src.jar. The charasets are in JavaVM.framework/Home/Classes/classes.jar.

    Problem 1) Attributes with a constant value are not processed correctly (eg. Alignment=3). This affects both paragraph and tab alignment. JDK bug number 4735839 provide hints, though probably not sufficent. Make the following changes to fully solve the problem. line 231, replace method attrDiff with

    In other references to attrDiff add a null parameter as follows (lines 462, 480, 716, 733, 743), line 473 replace checkControlWord with

    Problem 2) Marking a paragraph with \pard occurs if the attribute {"style:type","paragraph"} is part of the style and the style is applied using setLogicalStyle. Standard support does not provide access to these keywords, nor is this documented. Add a new class RTFStyle in the package with the updated java files.

    Problem 3) RTF uses the \page tag to force a hard page break. Add the following so that the hex character 0x0C in a document will write the \page tag and reading \page will insert 0x0C.

    at line 882 in writeCharacter add

    at after line 97 add

    Styled Text Classes

    Encapsulating the details of inserting styled text into a document will provide cleaner source. Two classes, Paragraph and Chunk are provided as examples. Paragraph will represent a single paragraph from the first character to the NEW LINE character. Chunk will represent a phrase in the paragraph with a single style. One or more chunks exist in a paragraph.

    Putting it all together

    The following provides a simple example of using these concepts.

    1) define styles

    Styles are associated with a StyleContext and should be divided between styles for a full paragraph (font, alignment, first line indent, etc.) and those for pieces of a paragraph (bold, italics, text color, etc.).

    2) Create a document.

    If the document is to be associated with a JTextPane, then use the following.

    3) Add text to document.

    The following will insert "The way to write RTF" with the first "T" in Helvetica-Bold and RTF in the paragraph font but italicized.

    The following will add a page break before the paragraph.


    4) Write document to file

    5) Debugging

    The contents of the RTF file and the document can be written to the console.

    10 years ago
    As part of the build target, I use the ant task propertyfile to write out date, time, buildnumber to a file. I would like to include that information in the output of the ant junitreport task. Can someone provide an example of how to include property values in the <xsl:template name="pageHeader"> ?
    16 years ago